BSD DevCenter
oreilly.comSafari Books Online.Conferences.


FreeBSD Basics Introducing DHCP

by Dru Lavigne

In the next few articles, I'll be covering the DHCP protocol: how it works and how to configure a DHCP client and a DHCP server using FreeBSD. If you've ever connected a computer to a network, you've probably heard of DHCP, the Dynamic Host Configuration Protocol. As its name suggests, this protocol is designed to configure a host dynamically with the TCP/IP information it needs in order to communicate on a network.

The alternative to dynamic addressing is static addressing. Static addressing occurs when you manually type in an IP address, subnet mask, and default gateway address. FreeBSD systems support both static and dynamic addressing. Typically, you would use static addressing to set up a small home network and dynamic addressing when you connect to the Internet.

Knowing Your Interfaces

Before using any type of addressing, you need to determine the FreeBSD name for your network interface card. To do that, use the ifconfig command like so:

$ ifconfig -a
rl0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
    ether 00:05:5d:d2:19:b7
    media: Ethernet autoselect
ed0: flags=8843<UP,BROADCAST,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:ba:de:36:33
    media: Ethernet autoselect
lp0: flags=8810<POINTOPOINT,SIMPLEX,MULTICAST> mtu 1500
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    inet netmask 0xff000000

The ifconfig command will display both physical and virtual interfaces. You can recognize the Ethernet interfaces as they will have an "ether" or MAC address. The FreeBSD name for the adapter will have 2 or 3 letters followed by a number. In this example, there are two Ethernet NICs; one is named rl0 and the other ed0. There are also two virtual interfaces, lp0 and lo0. You may recognize as the "loopback address", meaning lo0 is the name of the loopback virtual interface.

Also in FreeBSD Basics:

Fun with Xorg

Sharing Internet Connections

Building a Desktop Firewall

Using DesktopBSD

Using PC-BSD

Most interfaces have an entry in section 4 of the manual. In my case, I could try man 4 rl, man 4 ed, man 4 lp, and man 4 lo for more information about each interface. Note that you don't include the number when specifying the interface name with man. Instead, the interface number indicates how many interfaces of that type are installed; the count starts at 0. For example, if I had had two realtek NICs, they would be called rl0 and rl1. (That's "arr-ell", not "arr-one".)

Static Addresses

To assign a static IP address and subnet mask to the rl0 interface, I'll become the superuser and use ifconfig like so:

# ifconfig rl0

When you try this command, don't forget to specify the correct interface name for your system or you will receive an error message. To see if your command was successful, ask ifconfig to limit its information to one particular interface, rather than using the a switch to see all the interfaces:

# ifconfig rl0

rl0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
	inet netmask 0xffffff00 broadcast
	ether 00:05:5d:d2:19:b7

If your computer also requires a default gateway address, use the route command like so:

# route add default

To confirm your change:

# netstat -rn |grep G
Destination        Gateway            Flags    Refs      Use  Netif Expire
default        UGSc        5        0    rl0          UGHS        0        2    lo0

You'll note that I used grep to search the routing table for the G flag, which represents the gateway flag. The other flag to notice is U, which indicates that the gateway is up. This is always a good thing in a gateway.

This host is now configured, but none of those configurations will survive a reboot. It's inconvenient to retype in your static configurations every time you reboot a computer. Fortunately, you can tell your FreeBSD system to keep these configurations by including them in the system startup configuration file. On this host, I'll add the following lines to /etc/rc.conf:

ifconfig_rl0="inet netmask"

Be careful when making changes to this file, including getting the required quotation marks right. To test your change, make sure this command does not result in any errors:

# /etc/netstart

Dynamic Addresses

It's not a big deal to edit a few files if you only have one computer or a small network with just a few computers. However, if you are an administrator of a larger network, it is more convenient to use DHCP for two reasons. First, as the number of files you edit increases, so does the chance of typos and the possibility of two computers mistakenly being assigned the same IP address. Second, the more computers you have, the more inconvenient it is to have to sit down at each to enter their IP addressing information manually.

If your system is attached to a network that uses DHCP--the Internet, for example--you can take advantage of FreeBSD's built-in DHCP client. In my example, my second NIC (ed0) is attached to my cable modem. To receive an IP address from my ISP's DHCP server, I can use this command:

# dhclient ed0

To see if it worked:

# ifconfig ed0

ed0: flags=8843<UP,BROADCAST,SIMPLEX,MULTICAST> mtu 1500
	inet netmask 0xffffff00 broadcast	
	ether 00:50:ba:de:36:33

To configure this system to use always dynamic addressing, I can add the following line to /etc/rc.conf:


Pages: 1, 2

Next Pagearrow

Sponsored by: