Linux下的DHCP配置
1、DHCP工作过程
dhcp discover \\客户端请求服务器器提供地址
dhcp offer \\服务器提供ip地址
dhcp request \\客户端租用ip地址
dhcp ack \\服务器确认客户端的租用
2、安装dhcp软件包
# yum install dhcpd
# cd /etc/dhcpd
# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample ./dhcpd.conf
3、配置DHCP
基本配置内容
确定ip地址所在的网络
设置需要分配的ip地址范围
设置分配ip地址的子网掩码
设置网关ip地址
设置dns地址
设置租期时间(默认租期时间和最大租期时间)
地址绑定(给一台设备分配一个固定的ip地址)
# vim dhcpd.conf
option domain-name "sxjy.com";
option domain-name-servers 8.8.8.8,202.103.96.112;
default-lease-time 3600;
max-lease-time 72000;
log-facility local7;
shared-network sxjy {
subnet 172.16.10.0 netmask 255.255.255.0 {
range 172.16.10.2 172.16.10.100;
range 172.16.10.200 172.16.10.250;
option routers 172.16.10.1;
option broadcast-address 172.16.10.255;
default-lease-time 7200;
max-lease-time 15400;
}
subnet 172.16.20.0 netmask 255.255.255.0 {
range 172.16.20.2 172.16.20.100;
range 172.16.20.200 172.16.20.250;
option routers 172.16.20.1;
option broadcast-address 172.16.20.255;
}
host ftp.sxjy.com {
hardware ethernet 00:0C:29:B6:DF:5E;
fixed-address 172.16.10.10;
fixed-address 172.16.10.10;
}
}
练习:用三台虚拟机,其中一台为服务器,两台为客户机,要求两台客户机能获取到服务器分配的不同网段的地址,例如一台
获取的地址为192.168.1.10/24,另一台为192.168.2.10/24,具体地址段可以自己定义,但不能给其它人分配
ip地址,在服务器上抓包观察ip分配情况
4、DHCP地址绑定
host ftp.sxjy.com {
hardware ethernet 00:0C:29:B6:DF:5E;
fixed-address 192.168.10.10;
}
5、DHCP中继配置
(1)开启中继服务器的路由转发功能
# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# sysctl -p
(2)设置中继选项
# vim /etc/sysconfig/dhcrelay
INTERFACES="eth0 eth1"
DHCPSERVERS="172.16.10.1"
(3)启动dhcp中继服务
# service dhcrelay start
6、windows客户端的使用
ipconfig /renew \\重新获取ip地址
ipconfig /release \\释放ip地址
# dhclinet eth0
DHCP中继实验步骤:
(1)设置单网卡DHCP服务器,其上配置自己所在网段内的地址池,同时也需要配置其它网段地址池(该地址池中必须包含有option routers选项)。
[[email protected]er2 ~]# cat /etc/dhcp/dhcpd.conf
ddns-update-style none;
option domain-name "sxjy.com";
option domain-name-servers 1.1.1.1;
default-lease-time 6000;
max-lease-time 7200;
log-facility local7;
#shared-network haha {
subnet 192.168.3.0 netmask 255.255.255.0 {
range 192.168.3.10 192.168.3.20; //其他网络的地址池
option domain-name-servers 2.2.2.2;
option routers 192.168.3.254; //该网关地址一定要指定
}
subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.10 192.168.2.20; //该地址池为DHCP服务器自己所在网络的地址池
option domain-name-servers 2.2.2.2;
option routers 192.168.2.100;
}
自己的网卡为192.168.2.0网络
[[email protected] ~]# ip add show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:17:18:20 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.100/24 brd 192.168.2.255 scope global eth1\
同时自己的网关还需要指向路由主机:
[[email protected] ~]# route|grep default
default 192.168.2.254 0.0.0.0 UG 0 0 0 eth1
(2)设置路由主机,同时承担DHCP中继的角色
该路由器连接DHCP服务器的网卡(eth0)IP为192.168.2.254,另一张网卡(eth1)必须为192.168.3.254(与步骤1中的地址池设置相匹配)
a、开启中继服务器的路由转发功能
# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# sysctl -p
b、设置中继选项
# vim /etc/sysconfig/dhcrelay
INTERFACES="eth0 eth1"
DHCPSERVERS="172.16.10.1"
启动dhcrelay: service dhcrelay start
(3)设置客户端
该客户端的网卡链接路由器的eth1:
dhclient -r eth0;dhclient eth0
PS:路由器eht1和客户机eth0共用SW1的局域网。路由器eth0和DHCP服务器共用host-only的局域网。
路由器的DHCP
DHCP服务器的配置
Router(config)#interface fastEthernet 0/0
Router(config-if)#no shutdown
Router(config-if)#ip address 192.168.10.1 255.255.255.0
Router(config-if)#exit
Router(config)#ip dhcp pool xixi
Router(dhcp-config)#network 192.168.20.0 255.255.255.0
Router(dhcp-config)#default-router 192.168.20.1
Router(dhcp-config)#dns-server 8.8.8.8
Router(dhcp-config)#exit
Router(dhcp-config)#network 192.168.10.0 255.255.255.0
Router(dhcp-config)#default-router 192.168.10.2
Router(dhcp-config)#dns-server 8.8.8.8
Router(dhcp-config)#exit
Router(config)#ip route 0.0.0.0 0.0.0.0 192.168.10.2
中继代理的配置
Router(config)#interface fastEthernet 0/0
Router(config-if)#no shutdown
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
Router(config-if)#ip address 192.168.10.2 255.255.255.0
Router(config)#interface fastEthernet 0/1
Router(config-if)#no shutdown
Router(config-if)#ip address 192.168.20.1 255.255.255.0
Router(config-if)#ip helper-address 192.168.10.1
测试
主机上
PC>ipconfig /release
IP Address......................: 0.0.0.0
Subnet Mask.....................: 0.0.0.0
Default Gateway.................: 0.0.0.0
DNS Server......................: 0.0.0.0
PC>ipconfig /renew
IP Address......................: 192.168.20.2
Subnet Mask.....................: 255.255.255.0
Default Gateway.................: 192.168.20.1
DNS Server......................: 8.8.8.8