Skip to the content.

Dynamic Host Configuration Protocol (DHCP):为客户端自动配置tcpip的一种网络协议。 可以为客户端配置:IP地址,网关,dns服务器等;

1.配置DHCPV4服务器

环境:

[root@xcatmn ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.9 (Santiago)
[root@xcatmn ~]# uname -a
Linux xcatmn 2.6.32-696.el6.x86_64 #1 SMP Tue Feb 21 00:53:17 EST 2017 x86_64 x86_64 x86_64 GNU/Linux

使用root用户进行安装

[root@xcatmn ~]# yum -y install dhcp

安装后生成一个空文件/etc/dhcp/dhcpd.conf

[root@xcatmn ~]# cat /etc/dhcp/dhcpd.conf
[root@xcatmn ~]# ls -l /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample 
-rw-r--r--. 1 root root 3262 Aug 13  2002 /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample

这个文件的模板来自/usr/share/doc/dhcp- <version>/dhcpd.conf.sample/var/lib/dhcpd/dhcpd.leases保存客户端的数据库。

1.1 配置文件/etc/dhcp/dhcpd.conf

每次修改配置文件后,需要重启dhcp服务,当然也可以使用omshell进行在线修改。

例子1:

subnet 192.168.1.0 netmask 255.255.255.0 {
	option routers	192.168.1.254;
	option subnet-mask	255.255.255.0;
	option domain-search	"example.com";
	option domain-name-servers	192.168.1.1;
	option time-offset	-18000;
	range 192.168.1.10 192.168.1.100;
}

例子2:

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-search "example.com";
subnet 192.168.1.0 netmask 255.255.255.0 {
	range 192.168.1.10 192.168.1.100; 
}

例子3:使用固定IP地址

host apex {
	option host-name "apex.example.com"; 
	hardware ethernet 00:A0:78:8E:9E:AA; 
	fixed-address 192.168.1.4;
}

例子4:

shared-network name {
option domain-search		"test.redhat.com"; 
option domain-name-servers		ns1.redhat.com, ns2.redhat.com; 
option routers		192.168.0.254;
#more parameters for EXAMPLE shared-network 
subnet 192.168.1.0 netmask 255.255.252.0 {
	range 192.168.1.1 192.168.1.254; 
	}
subnet 192.168.2.0 netmask 255.255.252.0 { 
	#parameters for subnet
	range 192.168.2.1 192.168.2.254;
	} 
}

例子5:

group {
	option routers		192.168.1.254;
	option subnet-mask		255.255.255.0;
	option domain-search 		"example.com";
	option domain-name-servers 		192.168.1.1;
	option time-offset		-18000; # Eastern Standard Time
	host apex {
		option host-name "apex.example.com"; 
		hardware ethernet 00:A0:78:8E:9E:AA; 
		fixed-address 192.168.1.4;
	}
	host raleigh {
		option host-name "raleigh.example.com"; 
		hardware ethernet 00:A1:DD:74:C3:F2; 
		fixed-address 192.168.1.6;
	} 
}

1.2 租约数据库(Lease Database)

1.3 开启或关闭dhcp服务

使用/sbin/service dhcpd start启动,使用/sbin/service dhcpd stop停止服务。 还有几个注意的地方:

1.4 DHCP轮换客户端

DHCP Relay Agent (dhcrelay)允许将dhcp或bootp请求从一个没有dhcp的子网轮换到一个或多个有dhcp的子网中。 在/etc/sysconfig/dhcrelay中写入INTERFACES参数作为监听接口,想要启动DHCP Relay Agent,使用service dhcrelay start

2.配置DHCPV4客户端

/etc/sysconfig/network-scripts/ifcfg-eth0 写入:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

还有两个参数:

3. 配置多子网dhcp服务器

首先在 /etc/sysconfig/dhcpd指定使用哪个网卡来监听客户端的dhcp请求。

DHCPDARGS="eth0 eth1";  #希望两个网卡都在监听
DHCPDARGS="eth0";			  #只希望一个网卡监听

例子:

default-lease-time 600;
max-lease-time 7200;
subnet 10.0.0.0 netmask 255.255.255.0 {
	option subnet-mask 255.255.255.0; 
	option routers 10.0.0.1;
	range 10.0.0.5 10.0.0.15;
}
subnet 172.16.0.0 netmask 255.255.255.0 {
	option subnet-mask 255.255.255.0; 
	option routers 172.16.0.1;
	range 172.16.0.5 172.16.0.15;
}
host example0 {
	hardware ethernet 00:1A:6B:6A:2E:0B;
	fixed-address 10.0.0.20; 
}
host example1 {
	hardware ethernet 00:1A:6B:6A:2E:0B; 
	fixed-address 172.16.0.20;
}

需要注意的是:

4. 其他