DHCP Server on FreeBSD 9.2
I've been meaning to set up a DHCP server on FreeBSD for a while, so these are my notes for a simple configuration. This is based on the following:
- FreeBSD Handbook on DHCP
- Nixcraft - HowTo Change IP Address / Setup New IP Address For Existing Interface
- Forcing a network card to do a DHCP discover
- FreeBSD Man page on dhcp-options
On a new FreeBSD 9.2 installation, make sure you have the ports installed. Install the following packages as dependencies, to make things go a little faster:
pkg_add -r gettext gmake
Then via the ports install the isc-dhcp42-server port:
Note: Make sure to untick all the ldap stuff, we're not going to need it:
cd /usr/ports/net/isc-dhcp42-server
make config-recursive
Now begin the installation:
make install clean
Before we continue you'll probably want to change the IP address of your DHCP server machine to static. Edit the file /etc/rc.conf - you'll probably have a line like this:
ifconfig_em0="DHCP"
Change it to static by replacing it with:
ifconfig_em0="inet 10.0.0.1 netmask 255.255.255.0"
While in the same file make sure you manually add your gateway, in my case it's my internet router which is at 192.168.2.1:
defaultrouter="192.168.2.1"
Also add the following lines so the DHCP server starts on boot:
dhcpd_enable="YES"
dhcpd_ifaces="em0"
The FreeBSD Handbook on DHCP says the following with regards to the configuration file:
Copy /usr/local/etc/dhcpd.conf.example to /usr/local/etc/dhcpd.conf and make any edits to this new file.
However I found that the file /usr/local/etc/dhcpd.conf was already there, so I just went ahead and edited it (the /usr/local/etc/dhcpd.conf.example was there as well) - this is what I ended up with:
option domain-name "weirdbricks.com";
option domain-name-servers 10.0.0.2;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.7 10.0.0.254;
option routers 10.0.0.3;
}
host perkelen {
hardware ethernet 08:00:27:ea:75:34;
option host-name "webserver.weirdbricks.com";
fixed-address 10.0.0.6;
}
For an explanation of the above you can't beat the handbook, I just want to add a quick note that if you want to specify a hostname for a host, you can do it with the following:
option host-name "webserver.weirdbricks.com";
Note: On the target client make sure that you haven't already set a hostname in /etc/rc.conf ! In my case I had:
hostname="freebsd"
and I simply commented it out so it looks like:
#hostname="freebsd"
Once you have the above in place, reboot the server so that the static IP address change and the DHCP server turns on. If you need to simply restart the DHCP server, make sure to stop it first and then start it back up like this:
service isc-dhcpd stop && service isc-dhcpd start
With the above configuration all logs will go under /var/log/messages On the client side, if you need to get a new DHCP address for testing use the following:
service netif restart; dhclient em0
On the client you can check a few things: Check that you got the hostname you want with: hostname Note: If you've changed the hostname on the DHCP server, request a new DHCP IP but you still don't get the new hostname, try restarting the client - I didn't get any problems with the IP,Gateway and DNS entries - as long as the DHCP client was re-run they were changed Check that you got the IP you want with:
ifconfig em0 | grep inet
Check that your gateway is correct with:
netstat -rn | grep default
Check that you're getting the correct DNS Server with:
cat /etc/resolv.conf