共计 3978 个字符,预计需要花费 10 分钟才能阅读完成。
因为要在家里的机器上模拟 DHCP 环境,但是受家里机器和网络条件的限制,最后想到是不是可以使用 VirtualBox 来模拟这种情况,说做就做,下面就是我的实践步骤。
1. 首先,为了不影响家里其他机器上网以及其他虚拟机的使用,我们为每台虚拟机都分配两个网卡
第一块网卡使用 NAT 模式,用来连接外网。
第二块网卡使用 Host-Only 模式,用来连接内网。
2. 由于 VirtualBox 会默认创建一个”VirtualBox Host-Only Ethernet Adapter”的网络,并且这个网络默认会开启的 DHCP 服务,为了不影响其它虚拟机,我们新建一个 Host-Only 网络
打开“管理 -> 全局设定 -> 网络 -> 仅主机 (Host-Only) 网络”,然后添加一个,此时会生成一个名为“VirtualBox Host-Only Ethernet Adapter #2″ 的网络,配置信息如下:
主机虚拟网络界面
IPv4 地址:192.168.145.1
IPv4 网络掩码:255.255.255.0
DHCP 服务器
切记不要选中 “ 启用服务器 ”
3. 新建两个 CentOS6 的虚拟机,分别指定两个网卡
第一块网卡使用 NAT 模式,用来连接外网。
第二块网卡使用 Host-Only 模式,这里切记要选择上一步新建的“VirtualBox Host-Only Ethernet Adapter #2″。
4. 启动第一个虚拟机,我们将要这台机器作为 DHCP 服务器
4.1 修改网络配置,由于”VirtualBox Host-Only Ethernet Adapter #2″没有启用 DHCP,所以我们要为 eth2 指定一个静态 IP,这里我们我们使用如下配置
Address: 192.168.145.101
Netmask: 255.255.255.0
Gateway: 192.168.145.1
修改完成后使用“sudo service network restart”重启网络,此时使用 ifconfig 查看网络
…
eth1 Link encap:Ethernet HWaddr 08:00:27:1F:20:36
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe1f:2036/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:18333 errors:0 dropped:0 overruns:0 frame:0
TX packets:11113 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:22103029 (21.0 MiB) TX bytes:605438 (591.2 KiB)
eth2 Link encap:Ethernet HWaddr 08:00:27:06:C0:75
inet addr:192.168.145.101 Bcast:192.168.145.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe06:c075/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:131 errors:0 dropped:0 overruns:0 frame:0
TX packets:112 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:22247 (21.7 KiB) TX bytes:22532 (22.0 KiB)
…
4.2 安装 dhcp 软件包
sudo yum install dhcp
4.3. 编辑 dhcp 配置文件 /etc/dhcp/dhcpd.conf,可以参考 /usr/share/doc/dhcp-*/dhcpd.conf.sample,下面是我使用的配置文件内容
ddns-update-style interim; #表示 dhcp 服务器和 dns 服务器的动态信息更新模式
ignore client-updates; #忽略客户端更新
subnet 192.168.145.0 netmask 255.255.255.0 {#意思是我所分配的 ip 地址所在的网段为 192.168.145.0 子网掩码为 255.255.255.0
range 192.168.145.200 192.168.145.210; #租用 IP 地址的范围
option domain-name-servers ns.example.org;
option domain-name “example.org”;
option routers 192.168.145.101; #路由器地址,这里是当前 dhcp 机器的 IP 地址
option subnet-mask 255.255.255.0; #子网掩码
default-lease-time 600; #默认租约时间
max-lease-time 7200; #最大租约时间
host myhost {#设置主机声明
hardware ethernet 08:00:27:2C:30:8C; #指定 dhcp 客户的 mac 地址
fixed-address 192.168.145.155; #给指定的 mac 地址分配 ip
}
}
4.4. 编辑 /etc/rc.d/init.d/dhcpd 文件,将其中的
user=dhcpd
group=dhcpd
改为
user=root
group=root
注: 如果不做此修改,启动 DHCP 时在“/var/log/messages”文件里会有“Can’t chown new lease file: Operation not permitted”错误。
4.5. 启动 DHCP 服务
sudo service dhcpd start
5. 测试,启动第二台虚拟机器,然后使用 ifconfig 来查看网络情况,可以看到申请到的 ip 地址为 192.168.145.200
…
eth1 Link encap:Ethernet HWaddr 08:00:27:1C:29:8A
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe1c:298a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15 errors:0 dropped:0 overruns:0 frame:0
TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10383 (10.1 KiB) TX bytes:1935 (1.8 KiB)
eth2 Link encap:Ethernet HWaddr 08:00:27:E4:60:1A
inet addr:192.168.145.200 Bcast:192.168.145.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fee4:601a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:53 errors:0 dropped:0 overruns:0 frame:0
TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:7049 (6.8 KiB) TX bytes:2430 (2.3 KiB)
…
Linux 系统下构建 DHCP 服务器 http://www.linuxidc.com/Linux/2013-06/86531.htm
CentOS 下配置主从 DNS 服务器以及 DHCP 下的 DDNS http://www.linuxidc.com/Linux/2013-06/85634.htm
SUSE Linux 11 pxe+DHCP+tftp+ftp 无人值守安装 http://www.linuxidc.com/Linux/2013-06/85481.htm
Linux 下架设 DHCP 服务器过程及 3 种测试 http://www.linuxidc.com/Linux/2013-05/84832.htm
Linux 上一步一步实现 DHCP 服务器 http://www.linuxidc.com/Linux/2013-04/82244.htm
CentOS 6.5 系统下构建 DHCP 服务器 http://www.linuxidc.com/Linux/2014-06/103203.htm
Linux 下 DHCP 服务器的搭建(RHEL5.2)http://www.linuxidc.com/Linux/2014-11/108899.htm
