Configuring the DHCP Server
To start the configuration, let’s look at the important files that handle the options for the DHCP service:
配置文件
配置文件 | 作用 |
---|---|
/etc/dhcp/dhcpd.conf | Main config file for the DHCP service using IPv4 addresses |
/etc/dhcp/dhcpd6.conf | Main config file for the DHCP service using IPv6 addresses |
/var/lib/dhcpd/dhcpd.leases | IPv4 client lease file |
/var/lib/dhcpd/dhcpd6.leases | IPv6 client lease file |
The main config file is usually empty aside from a comment or two. The good news is that the package does provide a sample config file for you to use. This sample file provides examples and comments on how you can configure options for your DHCP server.
To copy the sample file, use the following command:
复制dhcp参考样例
# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
DHCP 服务器配置样例
Here is the sample DHCP server config file:
# Global Options
ddns-update-style none;
authoritative;
# Subnet definition
subnet 172.168.1.0 netmask 255.255.255.0 {
# Parameters for the local subnet
option routers 172.168.1.1;
option subnet-mask 255.255.255.0;
option domain-name “example.com”;
option domain-name-servers 172.168.1.1;
default-lease-time 21600;
max-lease-time 43200;
# Client IP range
range dynamic-bootp 172.168.1.100 172.168.1.200;
}
global options
Let’s break down this file into sections. The first section contains two options for the DHCP server itself, also called global options:
参数 | 作用 |
---|---|
ddns-update-style: none | This means that the DHCP server won’t update client DNS records. |
authoritative | This informs the client that the DHCP server contains legitimate information. |
REAL-WORLD TIP
If the server doesn’t contain an authoritative option and the client switches sub-nets,
it is not able to obtain a new IP address until its old lease has fully expired.
There are also security benefits of sending a DHCPNAK to incorrectly configured
clients.
subnet
The next section defines a subnet. Any options that you list in a subnet section are specific to the subnet for which you define them. If you want to set global options (such as authoritative ), you need to define them outside the subnet section. In this section, the following options are used:
参数 | 含义 |
---|---|
option routers | Defines the default gateway to the subnet |
option subnet-mask | Defines the subnet mask for the subnet |
option domain-name | Defines the name of the domain |
option domain-name-servers | Defines the DNS server for the subnet |
default-lease-time | Specifies how long each client keeps its lease until a renewal is requested (in seconds) |
max-lease-time | Specifies the maximum amount of time a client can keep a lease(in seconds) |
range dynamic-bootp | Specifies the range of IP addresses that can be given out to clients |
man dhcp相关
Here are the three man pages you should know:
# man dhcpd.conf
# man dhcpd.leases
# man dhcp-options
If you want to have multiple subnets, you can just define a new subnet section with its own set of options. There is no limit to the number of sections you can have.
REAL-WORLD TIP
If you want to set up multiple subnets, you usually have multiple interfaces on the server. If this is the case and you would like to service multiple subnets of clients, you need to adjust the /etc/sysconfig/dhcpd file. In this case, you need to edit the
following daemon option:
DHCPDARGS=”eth0”
Change this option to include all the interfaces for which you want to offer clients leases in different subnets. If you have two interfaces in two different subnets, for example, your option might look like this:
DHCPDARGS=”eth0 eth1”
固定分配IP地址
Reservations are common when dealing with printers on networks, but they can be used for clients, too. Here is how you define a reservation for a client. In your /etc/dhcpd.conf file, do the following:
host client01 {
option host-name “client01.example.com”;
hardware ethernet 02:B4:7C:43:DD:FF;
fixed-address 172.168.1.50;
}
Again, let’s look at each of these options and what they do.
参数 | 作用 |
---|---|
option host-name | Defines the fully qualified domain name of the client |
hardware ethernet | Defines the MAC address of the client |
fixed-address | Specifies the IP address that you want the client to receive |
核查配置文件
Check the config file for any errors:
# service dhcpd configtest
Syntax: OK
移走主配置文件sub-net末尾的"}"
If the DHCP service does find errors, it attempts to tell you where in the config file the error exists. Open your config file and remove the brace ( } ) that ends the sub-net section. Save your config file and run the syntax check on your config file again:
# service dhcpd configtest
Internet Systems Consortium DHCP Server V3.0.5-RedHat
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
/etc/dhcpd.conf line 18: unexpected end of file
^
Configuration file errors encountered -- exiting
将"}"放回原来的位置
Here, the service points out that the } is missing from the config file by giving you the line where the issue occurs as well as a general description of what the problem is. Now replace the } again and restart the DHCP service:
# service dhcpd start
Starting dhcpd: [ OK ]
Verify that the service is running:
# service dhcpd status
dhcpd (pid 3366) is running...