LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Linux Answers > Networking
User Name
Password

Notices

By SiegeX at 2004-09-13 14:01
The Dynamic Host Configuration Protocol (DHCP) allows you to specify network parameters on a server and have client computers query the server for their information such as IP, netmask, gateway, DNS, etc. In addition to not having to statically assign network information to numerous clients, you also do not need to specify the IP of the DHCP server as this discovery is done via broadcast packets; the caveat to this is that you must have one DHCP server per broadcast domain. In case it's not blatantly obvious, the power of DHCP is that if anything changes on your network such as the IP of a DNS server, you only need to edit one configuration file even if you have hundreds of clients.

The DHCP server I am using is called 'dhcpd' (oddly enough) and my current version is dhcp-3.0pl2-i386-1. This howto is not meant to be in-depth but rather just a general overview of some of the common features for the dhcpd server. If you need to do more esoteric configurations please man dhcpd.conf for detailed information.

The following is a common dhcpd.conf file. Below I will dissect this file and explain what each line does. Keeping with the convention of my other Linux Answers, all computer-specific information will be highlighted in blue and will most likely need to be changed.

Code:
ddns-update-style none;

subnet 192.168.1.0 netmask 255.255.255.0
{
        range 192.168.1.100 192.168.1.200;
        option subnet-mask 255.255.255.0;
        option broadcast-address 192.168.1.255;
        option domain-name-servers 123.123.123.10, 123.123.123.20;
        option routers 192.168.1.1;

        host slackbox
        {
                hardware ethernet 00:50:AB:AB:AB:AB;
                fixed-address 192.168.1.7;
        }

        host winbox
        {
                hardware ethernet 00:06:CD:CD:CD:CD;
                fixed-address 192.168.1.8;
        }
}
The first thing we need to do is set a Dynamic DNS update style. Since DynDNS is beyond the scope of this howto, I am going to set the style to none but if this is something you want to do, then the man pages have tons of info on it.
Code:
ddns-update-style none;
Next we must specify what subnet and netmask we will be working on. Note that you can have many subnet configurations within the single dhcpd.conf file. Each subnet group is bound together by curly braces { }
Code:
subnet 192.168.1.0 netmask 255.255.255.0
Note that every command from here on will only pertain to the subnet specified above. This will be true until we reach the closing curly brace } as noted above.

Now we will specify what range of IP addresses we want to be made available for clients using DHCP. This option is very handy when used in conjunction with a firewall because you know exactly what IP addresses came from a client using DHCP and you can exercise restrictions upon them as necessary.
Code:
range 192.168.1.100 192.168.1.200;
This next line is going to look a bit redundant because we are setting the netmask again even though we set it in the subnet declaration above, but it's recommended in the man pages so we are going to do it.
Code:
option subnet-mask 255.255.255.0;
Next we specify the broadcast address for our subnet. This address always ends in 255
Code:
option broadcast-address 192.168.1.255;
We will definitely want to tell our clients what servers to use for DNS in order to resolve hostnames to IP addresses
Code:
option domain-name-servers 123.123.123.10, 123.123.123.20;
The next option tells our clients what IP address to use for their gateway. This IP address generally ends in .1 but does not have to. The box with this IP should be configured as a router and be able to forward packets accordingly.
Code:
option routers 192.168.1.1;
If you wanted you could stop here but I thought I would show you a cool little feature that I like to use. Even though DHCP gives out IP address dynamically, it also has the ability to reserve an IP address for a certain computer. In this sense it's almost as if the client computer has a static IP even though it uses DHCP to get it. This is useful if you want to be able to put entries in your /etc/hosts file and not have to worry about the entry becoming invalid over time.

The first thing we must do is to specify a name for the computer as a helpful identifier
Code:
host slackbox
Note that similarly to the subnet grouping, we are now starting a sub-group as seen by the addition of the curly braces. This allows us to have multiple host definitions within one subnet group.

This next line is what allows us to uniquely identify one computer from another. The hardware ethernet address is the same as the MAC address. This information can be found by running the command ifconfig <interface> | grep HWaddr on a client computer for linux and ipconfig /all for a client computer running windows.

Code:
hardware ethernet 00:50:AB:AB:AB:AB;
And finally this next line tells the dhcpd server what IP address you always want to be assigned to this computer. Note that I intentionally make all IP's assigned this way outside of the DHCP range we specified earlier. This is not a must as the dhcp server is smart enough to not give out two IP's simultaneously but it helps in being able to quickly recognize which clients used this feature.
Code:
fixed-address 192.168.1.7;
This concludes this DHCP howto. As an added bonus I have included the init script I made for my Slackware box, however this script should work on many other distros. Please make sure you edit the 4 configuration options between the hashmark lines accordingly.

Code:
#!/bin/sh
#
# /etc/rc.d/rc.dhcpd
#
# Start/stop/restart the DHCP daemon.
#
# To make dhcpd start automatically at boot, make this
# file executable:  chmod 755 /etc/rc.d/rc.dhcpd
#
#############################################

CONFIGFILE="/etc/dhcpd.conf"
LEASEFILE="/var/state/dhcp/dhcpd.leases"
INTERFACES="eth1"
OPTIONS="-q"

#############################################

dhcpd_start() {
  if [ -x /usr/sbin/dhcpd -a -r $CONFIGFILE ]; then
    echo "Starting DHCPD..."
     /usr/sbin/dhcpd -cf $CONFIGFILE -lf $LEASEFILE $OPTIONS $INTERFACES
#     /usr/sbin/dhcpd -q $INTERFACES
  fi
}

dhcpd_stop() {
  killall dhcpd
}

dhcpd_restart() {
  dhcpd_stop
  sleep 2
  dhcpd_start
}

case "$1" in
'start')
  dhcpd_start
  ;;
'stop')
  dhcpd_stop
  ;;
'restart')
  dhcpd_restart
  ;;
*)
  # Default is "start", for backwards compatibility with previous
  # Slackware versions.  This may change to a 'usage' error someday.
  dhcpd_start
esac
To start up your brand new dhcpd server simply run the command
Code:
/etc/rc.d/rc.dhcpd start
As always, questions and comments are welcome. Enjoy!

by LinuxRam on Fri, 2004-10-29 15:38
It is good, but I think u should also take some time and write DHCP client how to also.


-Akshat

by mdkelly on Fri, 2004-10-29 18:28
Hi and thanks for the great how-to

I am setting up a DHCP server that will assign all of its IP address via MAC address and your post will come in very handy for this.

I do have one question though.

If I have the subnet 192.168.0.0/24, with all IPs served out as static from the DHCP server, how do I stop someone from just plugging in and entering in there own static IP address and getting on to the network. I am setting up the server for a small office and I want to be able to restrict what gets plugged in and gets an IP address and also to stop people from hooking up devices that have not been approved for use in the office.

Thanks for any points you may have to offer
mdkelly

by egag on Mon, 2004-11-15 19:59
hi there,

thank you for the great "howto", ( very clear ! ) i've set up my own dhcp-server. only one thing kept me busy
for more then 14 days: you need a #$%%@! CROSSOVER-cable to connect 2 pc's nic to nic.
i found out late at night, so with a little cutting-and -soldering i made it from a patch-cable.
now all works fine...

egag

by abhijeetudas on Tue, 2004-12-07 07:59
How does one configure DHCP
so that only a pool of addresses
having mac address say 00-11--XX-XX-XX
are assigned IP addresses as we have all ethernet cards with 1 starting address
and plan to have same in future..
so i have seen hardware-address option
but that doesnt help if a new machine comes in..
any solution.???

by Ron_shyen on Mon, 2005-01-03 22:32
Thanks to this How-to, I'm able to setup the static DHCP server using the methods provided. However, I'm more concerning about the dynamic IP that I configure, it still doesn't work. My network has 3 dhcp servers. Despite the pcs that I appointed as host (thru mac address and static IP), the rest pcs that logon thru the access point I setup still get its IP from other dhcp server. Any idea why is this happening? I'm ready to explain in more details if anyone here can give his/her help.

by abhijeetudas on Tue, 2005-01-04 15:45
If you could be a bit more precise..
it would help..

by Ron_shyen on Wed, 2005-01-05 01:17
Here's my story: I'm setting up a wireless access point in my school (it's a project) which already has 3 access points cum dhcp server.

AP1 (D-Link DWL-900AP+):
IP:10.107.100.200
IP range: 10.107.100.201---254
Netmask: 255.255.0.0
Gateway: 10.107.1.1

AP2(D-Link DWL-714P+):
IP: 192.168.0.1
Range: 192.168.0.100---199
Netmask: 255.255.255.0

AP3(D-Link DWL-900AP+)
IP:10.107.100.100
Range: 10.107.101---151
Netmask: 255.255.0.0
Gateway: 10.107.1.1

My access point is configured to
IP:10.107.200.1
Range: 10.107.200.11---19 (for testing purpose only)
Subnet:10.107.200.0
Netmask:255.255.0.0
Gateway:10.107.1.1

For your information, AP1 and AP3 is the default DHCP server for the wired pcs in school. And my access point is also a wired desktop with an extra wireless interface.

so after I configured the dhcpd.conf following the how-to, the host that access thru my AP still getting the IP from AP1 or AP3. However, I'm pretty sure that my dhcp server is running, coz when i try it with the method assigning the host MAC address with IP, it works.

some said it is the netmask of AP1 or AP3 that is too wide in the range. What say you?

by Ron_shyen on Thu, 2005-01-06 01:32
Some updates here: The DHCP server that I configure is in fact running. Some of the desktop units (with wired connection) nearby can obtain IP from it rather than the other 2 dhcp server (AP1 & AP3) within the same netmask (255.255.0.0). Now the major problem is: wireless hosts may not necessarily obtain its IP from this server, even though they logon to the internet thru my access-point-cum-dhcp-server, which is against the major objective of my project. Hmmm.....

by abhijeetudas on Thu, 2005-01-06 07:40
Ideally
you could block dhcp relay's requests that are passed through from
your "localised network" to the othr DHCP server's
that way only your DHCP server can assign Dynamic IP's
as per your criteria..

by Ron_shyen on Mon, 2005-01-10 12:35
Quote:
Originally posted by abhijeetudas
Ideally
you could block dhcp relay's requests that are passed through from
your "localised network" to the othr DHCP server's
that way only your DHCP server can assign Dynamic IP's
as per your criteria..
can you share with me how to configure the relay?


  



All times are GMT -5. The time now is 05:44 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration