1.8. CONFIGURING A STATIC IP
It is important for a server to have a static IP or permanent address for much the same reason a person needs a phone number: It allows callers or clients to reach the recipient. FreeBSD uses a file named rc.conf to establish the system's IP address, among other settings, during system startup. In this section we'll customize the rc.conf file to reflect your server's intended configuration.
The rc.conf file contains configuration settings for the computer's hostname, network interface cards, and which services to start at boot time. It is important that the settings in this file are correct; a typo here could hamper the system's functionality.
Note: The following discussion assumes that you're building your FreeBSD system to function as an Internet server. If this is not the case, then the options we selected during installation should be sufficient and you can skip this section.
We will cover two scenarios for configuring a static IP address:
-
Server behind a NAT (Network Address Translation) router
-
Server directly connected to the Internet
1.8.1. A. FreeBSD Server Behind a NAT Router
A small office or home network commonly has one Internet connection that needs to be shared by multiple computers. A NAT router allows the sharing of a single Internet connection within the local (private) network. The router functions as a firewall, creating a protected zone in the private network by allowing all traffic out, but only allowing known or solicited traffic in.
Port Forwarding
Most NAT routers provide a port-forward function that forwards traffic received at the router to a computer with a static IP address inside the private network. If, for example, you were hosting a web server, you would need to forward TCP port 80 (the IANA standard for HTTP) to the IP address of your FreeBSD server. (See your router's documentation for details on port forwarding.)
Most NAT routers that support port forwarding have built-in DHCP servers that assign computers in the private network a dynamic IP address, one that may change each time the computer logs on to the network.
DHCP works when machines simply need to connect to a network and get the first available IP address, but it's no help to you if you want to use your FreeBSD system as a server. You'll need a static (permanent) IP address so that information destined for your server will arrive.
Modifying rc.conf to Specify a Static IP Address
To specify your server's static IP address you'll modify rc.conf. But first you need to tell the DHCP server to assign IP addresses in a range that doesn't conflict with the server's IP address.
Your router's DHCP options should allow you to set the starting IP address (consult your router's documentation for details). For this example, we'll use 192.168.1.12 as the starting address for the range of addresses that can be assigned to machines, knowing that numbers are assigned from this address up (.13, .14, .15, and so on). We'll assign 192.168.1.11 as your server's static IP address since it is outside the range of the DHCP server.
Now let's set this in rc.conf. Open rc.conf:
# ee /etc/rc.conf
You should see something like the following in your rc.conf file (~7). Your FQDN should be here if specified during setup; the xl0 may be different.
hostname="host.example.com" ifconfig_xl0="DHCP"
Note: If you don't already have the hostname set, be sure to set it correctly. The hostname should be your system's fully qualified domain name; host is the name of the machine and example.com is your registered domain name.
Insert your router's IP address in the defaultrouter statement as shown below (~7). Using our example scenario above, the hostname, ifconfig, and defaultrouter statements should now look like this:
hostname="host.example.com" ifconfig_xl0="inet 192.168.1.11 netmask 255.255.255.0" defaultrouter="192.168.1.1"
Notice that we have replaced "DHCP" with our static IP address and added the netmask address (255.255.255.0 is the default netmask address in most configurations).
We've also added a defaultrouter line which points to the NAT router's IP address. This address, 192.168.1.1, will be the IP address you enter into your web browser to access the router web configuration; this is also called the default gateway.
Now save and exit. (Skip to "Dynamic DNS".)
1.8.2. B. FreeBSD Server Connected Directly to the Internet
If your FreeBSD system is connected directly to a cable or DSL modem and you have correctly entered your FQDN during configuration step 4 above, no further configuration is necessary. However, if you did not enter a hostname during the DHCP configuration, then you will need to edit /etc/rc.conf to include your FQDN.
Open rc.conf with Easy Editor:
# ee /etc/rc.conf
rc.conf (~7) should look like this (replace host.example.com with your FQDN):
hostname="host.example.com"
1.8.3. Dynamic DNS
Dynamic DNS is a service provided by third-party companies that keeps track of a computer's public IP address. These providers automatically update your domain name's associated IP address if it changes for any reason. Most Internet service providers use DHCP servers to assign public IP addresses to their customers dynamically. Unless you pay for a static IP address, this dynamically assigned address may change from time to time.
When you register your domain name, you can specify the target IP address of your server if you wish to host your own services. Many people mistakenly assume that their current, dynamic IP address will be theirs indefinitely. When your dynamic IP address changes (which may happen frequently or once every few months), you appear to "drop off " the Internet since your domain registrar's records point to the previous IP address, which is no longer valid. You would then have to go back to your domain registrar and notify them of your new IP address to regain your Internet presence (this is usually accomplished through a web-based control panel).
This is where dynamic DNS service providers become useful. These third-party companies allow you to keep your IP address updated by using a client program on your server to detect when the IP changes. When it does, the client program automatically contacts the dynamic DNS service to update your DNS record so you stay "live." When using these services you need to point your domain registrar to your dynamic DNS service's servers, which then point to your updated IP address. Most dynamic DNS providers charge a fee for their services, though there are a few free ones, like ZoneEdit (http://zoneedit.com). By combining a dynamic DNS service provider with a dynamic DNS updating client like ddclient, you can provide a static IP-like Internet presence. See "ddclient 3.7.3" for information on ddclient.