一、搭建环境:
准备三台虚拟机,分别命名为dhcp,dhcprelay(添加三块网卡),client ,Dhcp 连接 vmnet1,
dhcprelay 分别连接 vmnet1,vmnet2,vmnet3.
为三台虚拟机配置IP地址
dhcp-eth0:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=50.1.1.1
NETMASK=255.255.255.0
GATEWAY=50.1.1.254
[root@localhost ~]# ifdown eth0 ; ifup eth0
dhcprelay-eth0:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth
ifcfg-eth0 ifcfg-eth0.bak ifcfg-eth1 ifcfg-eth2
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
IPADDR=50.1.1.254
NETMASK=255.255.255.0
[root@localhost ~]# ifdown eth0 ; ifup eth0
dhcprelay-eth1:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth1
BOOTPROTO=static
ONBOOT=yes
IPADDR=50.2.2.254
NETMASK=255.255.255.0
[root@localhost ~]# ifdown eth1 ; ifup eth1
dhcprelay-eth2:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth2
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth2
BOOTPROTO=static
ONBOOT=yes
IPADDR=50.3.3.254
NETMASK=255.255.255.0
[root@localhost ~]# ifdown eth2 ; ifup eth2
二、在dhcp 和 dhcprelay 上安装dhcp服务
[root@localhost ~]# mount -o loop /dev/cdrom /media/
[root@localhost ~]# cd /media/Server/
[root@localhost Server]# rpm -ivh dhcp-3.0.5-23.el5.i386.rpm
error: failed to stat /media/RHEL_5.5 i386 DVD: 没有那个文件或目录
Preparing... ########################################### [100%]
1:dhcp ########################################### [100%]
三、在dhcp上编辑/etc/dhcpd.conf
[root@localhost Server]# cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
cp:是否覆盖“/etc/dhcpd.conf”? y
[root@localhost Server]# vim /etc/dhcpd.conf
ddns-update-style none;
default-lease-time 21600;
max-lease-time 43200;
option domain-name-servers 202.106.0.20;
subnet 50.1.1.0 netmask 255.255.255.0 {
option routers 50.1.1.254;
option subnet-mask 255.255.255.0;
range dynamic-bootp 50.1.1.2 50.1.1.100;
host srv {
hardware ethernet 12:34:56:78:AB:CD;
fixed-address 50.1.1.50;
}
}
subnet 50.2.2.0 netmask 255.255.255.0{
range dynamic-bootp 50.2.2.2 50.2.2.100;
option subnet-mask 255.255.255.0;
option routers 50.2.2.254;
}
subnet 50.3.3.0 netmask 255.255.255.0{
range dynamic-bootp 50.3.3.2 50.3.3.100;
option subnet-mask 255.255.255.0;
option routers 50.3.3.254;
}
四、在dhcp上启动dhcp 服务
[root@localhost ~]# service dhcpd start
启动 dhcpd: [确定]
五、在dhcprelay 上开启路由转发
[root@localhost Server]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@localhost Server]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
六、在dhcprelay 上配置中继
[root@localhost Server]# vim /etc/sysconfig/dhcrelay
# Command line options here
INTERFACES="eth0 eth1 eth2"
DHCPSERVERS="50.1.1.1"
七、启动dhcprelay 中继服务
[root@localhost Server]# service dhcrelay start
启动 dhcrelay: [确定]
八、验证
1、将client 连接vmnet1上,并设置dhcp自动获取
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
2、重启网卡后查看IP
[root@localhost ~]# ifdown eth0
[root@localhost ~]# ifup eth0
正在决定 eth0 的 IP 信息...完成。
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:B1:17:07
inet addr:50.1.1.100 Bcast:50.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb1:1707/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:221 errors:0 dropped:0 overruns:0 frame:0
TX packets:659 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:43662 (42.6 KiB) TX bytes:133870 (130.7 KiB)
Interrupt:177 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3162 errors:0 dropped:0 overruns:0 frame:0
TX packets:3162 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5332786 (5.0 MiB) TX bytes:5332786 (5.0 MiB)
3、修改网卡使用的MAC地址为 cc-cc-cc-cc-cc-cc 获得ip 地址为保留ip 50.1.1.50
[root@localhost ~]# ifdown eth0
[root@localhost ~]# ifconfig eth0 hw ether cc:cc:cc:cc:cc:cc
[root@localhost ~]# ifup eth0
正在决定 eth0 的 IP 信息...完成。
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr CC:CC:CC:CC:CC:CC
inet addr:50.1.1.50 Bcast:50.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::cecc:ccff:fecc:cccc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:227 errors:0 dropped:0 overruns:0 frame:0
TX packets:693 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:45714 (44.6 KiB) TX bytes:141004 (137.6 KiB)
Interrupt:177 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3162 errors:0 dropped:0 overruns:0 frame:0
TX packets:3162 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5332786 (5.0 MiB) TX bytes:5332786 (5.0 MiB)
4、将client 连接 vmnet2上,并设置重启网卡获得50.2.2.100
[root@localhost ~]# ifdown eth0 ; ifup eth0
正在决定 eth0 的 IP 信息...完成。
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr CC:CC:CC:CC:CC:CC
inet addr:50.2.2.100 Bcast:50.2.2.255 Mask:255.255.255.0
inet6 addr: fe80::cecc:ccff:fecc:cccc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:241 errors:0 dropped:0 overruns:0 frame:0
TX packets:742 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:49190 (48.0 KiB) TX bytes:150619 (147.0 KiB)
Interrupt:177 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3162 errors:0 dropped:0 overruns:0 frame:0
TX packets:3162 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5332786 (5.0 MiB) TX bytes:5332786 (5.0 MiB)
5、将client连接vmnet3上,并设置重启网卡获得50.3.3.100
[root@localhost ~]# ifdown eth0 ; ifup eth0
正在决定 eth0 的 IP 信息...完成。
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr CC:CC:CC:CC:CC:CC
inet addr:50.3.3.100 Bcast:50.3.3.255 Mask:255.255.255.0
inet6 addr: fe80::cecc:ccff:fecc:cccc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:247 errors:0 dropped:0 overruns:0 frame:0
TX packets:791 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:50116 (48.9 KiB) TX bytes:161057 (157.2 KiB)
Interrupt:177 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3162 errors:0 dropped:0 overruns:0 frame:0
TX packets:3162 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5332786 (5.0 MiB) TX bytes:5332786 (5.0 MiB)