Posts Tagged ‘Linux DHCP’

Linux DHCP

Linux DHCP is Dynamic Host Configuration Protocol. It is used to control vital networking parameters of hosts (running clients)with the help of a server. DHCP is backward compatible with BOOTP. For more information see RFC 2131 (old RFC 1541) and other. (See Internet Resources section at the end of the document).

This mini-tutorial covers both the DHCP _SERVER_ daemon as well as DHCP_CLIENT_ daemon. Most people need the client daemon which is used byworkstations to obtain network information from a remote server. The serverdaemon is used by system administrators to distribute network informationto clients so if you are just a regular user you need the _CLIENT_ daemon.

Downloading the client daemon (dhcpcd)

Depending on your distribution you might have to download the DHCP client daemon. If you want to compile it from the source youpackage you need is called dhcpcd and the current version is 1.3.18. It is maintained by Sergei Viznyuk andtoday it comes as a binary package with most distributions.

dhcpcd source can be downloaded from following locations

Then follow the instructions below. They should be the same.

Tying it all together

After your machine reboots your network interface should be configured.Type: ifconfig.

You should get something like this:

          lo        Link encap:Local Loopback            inet addr:127.0.0.1  Bcast:127.255.255.255  Mask:255.0.0.0          UP BROADCAST LOOPBACK RUNNING  MTU:3584  Metric:1          RX packets:302 errors:0 dropped:0 overruns:0 frame:0          TX packets:302 errors:0 dropped:0 overruns:0 carrier:0 coll:0eth0      Link encap:Ethernet  HWaddr 00:20:AF:EE:05:45          inet addr:24.128.53.102  Bcast:24.128.53.255  Mask:255.255.254.0          ^^^^^^^^^^^^^^^^^^^^^^^          UP BROADCAST NOTRAILERS RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:24783 errors:1 dropped:1 overruns:0 frame:1          TX packets:11598 errors:0 dropped:0 overruns:0 carrier:0 coll:96          Interrupt:10 Base address:0x300

If you have some normal number under inet addr you are set. If you see0.0.0.0 don’t despair, it is a temporary setting beforedhcpcd acquiresthe IP address. If even after few minutes you are seeing 0.0.0.0 pleasecheck out Troubleshooting. DHCPcd is a daemon and willstay running as long as you have your machine on. Every three hours itwill contact the DHCP server and try to renew the IP address lease. Itwill log all the messages in the syslog (on Slackware/var/adm/syslog, RedHat/OpenLinux/var/log/syslog).

One final thing. You need to specify your nameservers. There are two ways to doit, you can either ask your provider to provide you with the addresses of yourname server and then put those in the/etc/resolv.conf or DHCPcd will obtainthe list from the linux DHCP server and will build aresolv.conf in /etc/dhcpc.

I decided to use linux DHCPcd’s resolv.conf by doing the following:

Back up your old /etc/resolv.conf: mv /etc/resolv.conf/etc/resolv.conf.OLD

If directory /etc/dhcpc doesn’texist create it: mkdir /etc/dhcpc

Make a link from /etc/dhcpc/resolv.conf to/etc/resolv.conf: ln -s/etc/dhcpc/resolv.conf /etc/resolv.conf

If that doesn’t work try this (fix suggested by with a little amendment by HenrikStoerner):

This last step I had to perform only because my dhcpcd doesn’tcreate an /etc/dhcpc/resolv.conf. In /etc/sysconfig/network-scripts/ifup Imade the following changes (which are a very poor hack, but they workfor me):

elif [ "$BOOTPROTO" = dhcp -a "$ISALIAS" = no ]; then    echo -n "Using DHCP for ${DEVICE}... "    /sbin/dhcpcd -c /etc/sysconfig/network-scripts/ifdhcpc-done ${DEVICE}    echo "echo \$$ > /var/run/dhcp-wait-${DEVICE}.pid; exec sleep 30" | sh    if [ -f /var/run/dhcp-wait-${DEVICE}.pid ]; then^^^^        echo "failed."        exit 1

I changed to:

elif [ "$BOOTPROTO" = dhcp -a "$ISALIAS" = no ]; then    echo -n "Using DHCP for ${DEVICE}... "    /sbin/dhcpcd    echo "echo \$$ > /var/run/dhcp-wait-${DEVICE}.pid; exec sleep 30" | sh    if [ ! -f /var/run/dhcp-wait-${DEVICE}.pid ]; then^^^^^^       echo "failed."       exit 1

Note: Notice the ! (bang) in if [ ! -f/var/run/dhcp-wait-${DEVICE}.pid ];

Now sit back and enjoy :-) .

DHCP server for UNIX

There are several DHCP servers available for U*X-like OSes,both commercial and free. One of the more popular free DHCP serversis Paul Vixie/ISC DHCPd. Currently the latest version is 2.0 (suggestedfor most users) but 3.0 is in beta testing. You can get them from

ftp://ftp.isc.org/isc/dhcp/

Some of the distributions provide binary packages for dhcpd soskip the following section if you got it installed that way.

After you download unpack it. After you do cd into thedistribution directory and type: ./configure

It will take some time to configure the settings. After it is done type:make and make install.

DHCP server configuration

When done with installation type ifconfig -a. Youshould see something like this:

eth0      Link encap:10Mbps Ethernet  HWaddr 00:C0:4F:D3:C4:62          inet addr:183.217.19.43  Bcast:183.217.19.255  Mask:255.255.255.0          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:2875542 errors:0 dropped:0 overruns:0          TX packets:218647 errors:0 dropped:0 overruns:0          Interrupt:11 Base address:0x210

If it doesn’t say MULTICAST you should reconfigure yourkernel and add multicast support. On most systems you will not need to do this.

Next step is to add route for 255.255.255.255. Quoted from DHCPd README:

“In order for dhcpd to work correctly with picky DHCP clients (e.g., Windows 95), it must be able to send packets with an IP destination address of 255.255.255.255. Unfortunately, Linux insists on changing 255.255.255.255 into the local subnet broadcast address (here, that’s 192.5.5.223). This results in a DHCP protocol violation, and while many DHCP clients don’t notice the problem, some (e.g., all MicrosoftDHCP clients) do. Clients that have this problem will appear not to see DHCPOFFER messages from the server.”

Type: route add -host 255.255.255.255 dev eth0

If you get a message “255.255.255.255: Unknown host“,you should try adding the following entry to your/etc/hosts file:

255.255.255.255 all-ones

Then, try:

route add -host all-ones dev eth0

or

route add 255.255.255.0 dev eth0

eth0 is of course the name of the network device youare using. If it differs change appropriately.

Options for DHCPd

Now you need to configure DHCPd. In order to do this youwill have to create or edit /etc/dhcpd.conf. Thereis a graphical interface for dhcpd configuration under KDE ( http://www.kde.org/ ) called kcmdhcpdthat is very similar to the DHCP configurator on Windows NT. When KDE2.0 comes out it should come with kcmdhcpd or you could get it directlyfrom linux ftp server at:

ftp://ftp.us.kde.org/pub/kde/unstable/apps/network/

If you want to configure it by hand follow instructions below.

Most commonly what you want to do is assign IP addresses randomly. Thiscan be done with settings as follows:

# Sample /etc/dhcpd.conf# (add your comments here) default-lease-time 600;max-lease-time 7200;option subnet-mask 255.255.255.0;option broadcast-address 192.168.1.255;option routers 192.168.1.254;option domain-name-servers 192.168.1.1, 192.168.1.2;option domain-name "mydomain.org";subnet 192.168.1.0 netmask 255.255.255.0 {   range 192.168.1.10 192.168.1.100;   range 192.168.1.150 192.168.1.200;}

This will result in linux DHCP server giving a client an IP addressfrom the range 192.168.1.10-192.168.1.100 or 192.168.1.150-192.168.1.200. It will lease an IP address for600 seconds if the client doesn’t ask for specific timeframe. Otherwise the maximum (allowed) lease will be 7200 seconds. Theserver will also “advise” the client that it should use 255.255.255.0 asits subnet mask, 192.168.1.255 as its broadcast address, 192.168.1.254 asthe router/gateway and 192.168.1.1 and 192.168.1.2 as its DNS servers.

If you need to specify a WINS server for your Windows clients youwill need to include the netbios-name-servers option e.g.

option netbios-name-servers 192.168.1.1;

You can also assign specific IP addresses based on clientsethernet address e.g.

host haagen {   hardware ethernet 08:00:2b:4c:59:23;   fixed-address 192.168.1.222;}

This will assign IP address 192.168.1.222 to a client with ethernetaddress 08:00:2b:4c:59:23.

You can also mix and match e.g. you can have certain clients getting”static” IP addresses (e.g. servers) and others being alloteddynamic IPs (e.g. mobile users with laptops). There are a number of otheroptions e.g. nis server addresses, time server addresses etc., if youneed any of those options please read thedhcpd.conf man page.

Starting the server

There is only one thing to do before starting the server. In most casesDHCP installation doesn’t create a dhcpd.leasesfiles. This file is used by DHCPd to store information about currentleases. It is in the plain text form so you can view it during theoperation of DHCPd. To create dhcpd.leases type:

touch /var/state/dhcp/dhcpd.leases

This will create an empty file (file size = 0).Some of the older version of dhcpd 2.0 placed the file in/etc/dhcpd.leases. You do not need to make anychanges to the leases file it will be manipulated by the dhcpd. If youget a message saying that file exists simply ignore it and go to thenext step.

You can now invoke the Linux DHCP server. Simply type (or include in the bootupscripts)

/usr/sbin/dhcpd

This will invoke dhcpd on eth0 device. If you want to invoke iton another device simply supply it on the command line e.g.

/usr/sbin/dhcpd eth1

To verify that everything is working fine youshould first turn on the debugging mode and put the serverin foreground. You can do this by typing

/usr/sbin/dhcpd -d -f

Then boot up one of your clients and check out the console of your server. You will see a number of debugging messages come up. If everything works outfine you are done :-) . Quit dhcpd and start it without the-d -f and arguments. If you wantdhcpd to start at boot-up include dhcpd in e.g.

/etc/rc.d/rc.local

Now you can maintain your network systems by using Linux DHCP Server and you must not worry about giving DHCP manually to each client computer.

Other related articles:

  • PXE Server for Clonezilla/ESX 4 – Alright, this blog is as much about me as a DJ as it is about what’s going on in my life, so in following with the open-source I help you if you help me movement, I have a post that’s a how-to for getting a PXE server running with support for Clonezilla and ESX (vSphere) 4. (For those of you who read this on facebook, it gets auto-uploaded there after I post it on my main site at blog. Also keeping in mind that the steps presented could be modified for just about anything except a Windows boot but there’s plenty of resources for THAT out there as well (if you’re interested, look into WinPE it’s designed for Windows booting over PXE).
  • LTSP Linux Thin Client School Project Update | | IT Infusion in … – I just got back from my son’s school where we’ve been rolling out LTSP thin clients into the classrooms. Eventually we’ll be converting almost all of the computers in both buildings (200+ PCs) into thin clients. There are no other words to describe it.
  • Red Hat Magazine | Linux DHCP server: Static IPs are gone in 60 … – Linux DHCP is a low-cost alternative to proprietary network servers that depend on the user doing a series of GUI clicks. On the surface it may seem convenient to have a “wizard” setup your network for you. But it’s often better to tap …
  • Linux DHCP Server | Adam Palmer, Linux, PHP Programmer, MySQL … – Linux DHCP Server.

Technorati Tags: , , ,