一 DHCP相关知识
动态主机配置协议(Dynamic Host Configuration Protocol, DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途:给内部网络或网络服务供应商自动分配IP地址,给用户或者内部网络管理员作为对所有计算机作中央管理的手段。简而言之,DHCP 是这样的服务器:服务器进行IP地址的分配、客户端自动获取IP。
我们先来看看DNS工作原理,如图一:
图一 DNS工作原理
本文的虚拟机配置见图二
图二 虚拟机配置
二 dhcp——自动分配IP
--第一步,安装dhcp
[root@serv01 ~]# yum install dhcp* -y
[root@serv01 ~]# rpm -qa|grep dhcp
dhcp-4.1.1-19.P1.el6.x86_64
--第二步,修改配置文件
[root@serv01 ~]# vim /etc/dhcp/dhcpd.conf
[root@serv01 ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0{
range192.168.1.100 192.168.1.150;
}
--第三步,Windows环境下测试。
#Windows下注意事项:
#1.网卡:选择DHCP服务器对应的网卡
#2.把360卸载掉
#3.这是情况ip地址的命令:ifconfig/release、ifconfig /new
--第四步,Linux环境下测试,修改配置文件
[root@serv02 ~]# cat/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
HWADDR="00:0C:29:6A:EC:97"
NM_CONTROLLED="yes"
ONBOOT="yes"
BOOTPROTO=dhcp
IPADDR=192.168.1.12
NETMASK=255.255.255.0
#发现IP改变,需要重新登录
[root@larrywen 0807]# ssh 192.168.1.51
The authenticity of host '192.168.1.51(192.168.1.51)' can't be established.
RSA key fingerprint is3e:bd:1e:76:c4:c7:b4:98:dc:95:fc:61:d7:a8:45:71.
Are you sure you want to continueconnecting (yes/no)? yes
Warning: Permanently added '192.168.1.51'(RSA) to the list of known hosts.
root@192.168.1.51's password:
Last login: Wed Aug 7 22:19:19 2013
[root@serv02 ~]#
[root@serv02 ~]# man 5 dhcpd.conf
三 dhcp——DNS和网关配置
--第一步,修改配置文件
[root@serv01 dhcp]# vim dhcpd.conf
[root@serv01 dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0 {
range192.168.1.50 192.168.1.80;
optiondomain-name-servers 8.8.8.8;
optionrouters 192.168.1.1;
}
#检查配置文件是否正确
[root@serv01 dhcp]# /etc/init.d/dhcpdconfigtest
Syntax: OK
--第二步,重启服务
[root@serv01 dhcp]# /etc/init.d/dhcpdrestart
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
--第三步,Linux下测试
#Linux下
#重启服务
[root@serv02 ~]# /etc/init.d/networkrestart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0:
Determining IP information for eth0...done.
[ OK ]
#检查网关
[root@serv02 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
#检查DNS
[root@serv02 ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
nameserver 8.8.8.8
#查看这个配置文件没有任何变化
[root@serv02 ~]# cat/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
HWADDR="00:0C:29:6A:EC:97"
NM_CONTROLLED="yes"
ONBOOT="yes"
BOOTPROTO=dhcp
IPADDR=192.168.1.12
NETMASK=255.255.255.0
测试效果如图三
图三 Linux下dhcp DNS和网关配置
--第四步,Windows环境下测试
这是没有网关和DNS时分配的IP地址:
图四 没有网关和DNS时分配的IP地址
这是添加网关和DNS时分配的IP地址:
图五 添加网关和DNS时分配的IP地址
四 dhcp——最大租约时间
--第一步,修改配置文件
[root@serv01 dhcp]# vim dhcpd.conf
[root@serv01 dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0 {
range192.168.1.50 192.168.1.80;
optiondomain-name-servers 8.8.8.8;
optionrouters 192.168.1.1;
max-lease-time60;
}
--第二步,重启服务
[root@serv01 dhcp]# /etc/init.d/dhcpdrestart
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
--第三步,Windows环境下测试
测试效果如图六:
图六 DHCP 最大租约时间
五 dhcp——domain-name
--第一步,修改配置文件
[root@serv01 dhcp]# vim dhcpd.conf
[root@serv01 dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0 {
range192.168.1.50 192.168.1.80;
optiondomain-name "hongyi.com";
optiondomain-name-servers 8.8.8.8;
optionrouters 192.168.1.1;
max-lease-time60;
}
--第二步,重启服务
[root@serv01 dhcp]# /etc/init.d/dhcpdrestart
Starting dhcpd: [ OK ]
--第三步,Linux环境下测试,重启服务,查看resolv.conf文件,发现自动生成了DNS服务器地址
[root@serv02 ~]# /etc/init.d/networkrestart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0:
Determining IP information for eth0...done.
[ OK ]
[root@serv02 ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search hongyi.com host.com
nameserver 8.8.8.8
六 dhcp——fixed-address
--第一步,修改配置文件
[root@serv01 dhcp]# man 5 dhcpd.conf
[root@serv01 dhcp]# vim dhcpd.conf
[root@serv01 dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0 {
range192.168.1.50 192.168.1.80;
optiondomain-name "hongyi.com";
optiondomain-name-servers 8.8.8.8;
optionrouters 192.168.1.1;
max-lease-time60;
}
host joe {
hardwareethernet 00:0C:29:6A:EC:97;
fixed-address 192.168.1.88;
}
--第二步,重启服务
[root@serv01 dhcp]# /etc/init.d/dhcpdrestart
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
--第三步,Linux环境下测试,如图七
图七 DHCP fixed-address测试
七 dhcp中转
实现的功能:一台机器通过dhcp服务器从另一台机器中获得IP地址
网络拓扑结构图如图八所示:
图八 DHCP 中转 网络拓扑图
第一步,server 01修改配置文件
[root@serv01 dhcp]# vim dhcpd.conf
[root@serv01 dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0 {
range192.168.1.60 192.168.1.65;
optionrouters 192.168.1.1;
optiondomain-name-servers 8.8.8.8;
max-lease-time3600;
}
subnet 172.16.1.0 netmask 255.255.255.0 {
range172.16.1.60 172.16.1.65;
optionrouters 172.16.1.1;
optiondomain-name-servers 8.8.4.4;
max-lease-time3600;
}
第二步,serv01启动服务
[root@serv01 dhcp]# /etc/init.d/dhcpdrestart
Starting dhcpd: [ OK ]
第三步,添加默认路由(网关)
[root@serv01 dhcp]# ip route add defaultvia 192.168.1.13
#查看路由
[root@serv01 dhcp]# ip route
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.11
169.254.0.0/16 dev eth0 scope link metric 1002
default via 192.168.1.13 dev eth0
[root@serv01 dhcp]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.13 0.0.0.0 UG 0 0 0 eth0
第四步,server02测试
测试效果如图九,
图九 DHCP 中转serv02测试
第五步,server03配置
#设置IP
[root@serv03 ~]# ip addr add 172.16.1.13brd 255.255.255.0 dev eth1
#启动网卡
[root@serv03 ~]# ip link set eth1 up
[root@serv03 ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr00:0C:29:BD:08:0F
inet addr:172.16.1.13 Bcast:255.255.255.0 Mask:255.255.255.255
inet6 addr: fe80::20c:29ff:febd:80f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:4247(4.1 KiB)
#编辑文件
[root@serv03 ~]# vim/etc/sysconfig/dhcrelay
[root@serv03 ~]# cat/etc/sysconfig/dhcrelay
# Command line options here
DHCRELAYARGS=""
# DHCPv4 only
INTERFACES="eth0 eth1"
# DHCPv4 only
DHCPSERVERS="192.168.1.11"
第六步,Window 01测试
测试效果如图十
图十 dhcp中转Windows测试
第七步,要想ping通,这样修改
1.serv01修改配置
[root@serv01 dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see 'man 5 dhcpd.conf'
#
subnet 192.168.1.0 netmask 255.255.255.0 {
range192.168.1.60 192.168.1.65;
optionrouters 192.168.1.1;
optiondomain-name-servers 8.8.8.8;
max-lease-time3600;
}
subnet 172.16.1.0 netmask 255.255.255.0 {
range172.16.1.60 172.16.1.65;
optionrouters 172.16.1.13;
optiondomain-name-servers 8.8.4.4;
max-lease-time3600;
}
2.serv01重启服务
[root@serv01 dhcp]# /etc/init.d/dhcpd restart
3.serv01打开IP转发
[root@serv01 dhcp]# sysctl -wnet.ipv4.ip_forward=1
4.serv03添加默认路由
#注意:新添加的IP地址不一定会给你添加默认路由,所以要全方面地检查,台智能化的东西不要太依赖。
[root@serv03 ~]# ip route add 172.16.1.0/24dev eth1
[root@serv03 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
5.测试(serv01 ping Win 01)
#serv01 ping Win 01
[root@serv01 dhcp]# ping 172.16.1.60
PING 172.16.1.60 (172.16.1.60) 56(84) bytesof data.
64 bytes from 172.16.1.60: icmp_seq=1ttl=127 time=0.543 ms
64 bytes from 172.16.1.60: icmp_seq=2ttl=127 time=0.479 ms
64 bytes from 172.16.1.60: icmp_seq=3ttl=127 time=0.657 ms
^C
--- 172.16.1.60 ping statistics ---
3 packets transmitted, 3 received, 0%packet loss, time 2677ms
rtt min/avg/max/mdev =0.479/0.559/0.657/0.078 ms
6.测试(Win01 ping Serv01)
测试效果如图十一:
图十一 Windows01ping通Serv01
7.注意事项:
#还要注意把Linux和Windows的防火墙关闭
#注意:使用ip命令添加的IP地址没有默认路由,使用ifconfig命令才有
八 参考资料
我的邮箱:wgbno27@163.com 新浪微博:@Wentasy27 微信公众平台:JustOracle(微信号:justoracle) 数据库技术交流群:336882565(加群时验证 From CSDN XXX) By Larry Wen
![]() | ![]() ![]() |
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客] |