ubuntu搭建dhcp伺服器
A. ubuntu dhcpv6伺服器怎麼搭建
這個是ipv6環境用的dhcp,還沒普及開,你得用ipv4的,配置文件是/etc/dhcp/dhcpd.conf
B. 在一個區域網中(沒有dhcp服務)用docker搭建一個dhcp伺服器
1、使用的鏡像
sudo docker pull networkboot/dhcpd
2、測試的區域網拓撲結構如下:h為 DHCP伺服器,ovs交換機s1、s2、s3各連著一台等待分配ip地址的主機。
3、h DHCP伺服器通過一對暴露於root namespace 的veth pair相連,具體操作如下
3.1、生成交換機s1、s2、s3
sudo ovs-vsctl add-br s1sudo ovs-vsctl add-br s2sudo ovs-vsctl add-br s3
3.2、創建veth pair 即 v1-v2
sudo ip link add v1 type veth peer name v2
3.3、**up
sudo ip link set dev v1 upsudo ip link set dev v2 up
3.4、給v1配置ip及子網掩碼
sudo ifconfig v1 192.168.1.254 netmask 255.255.255.0
3.5、生成DHCP伺服器,監聽v1 ,掛載本地data文件夾,data裡面中只有dhcpd.conf文件
sudo docker run -it \--rm --net host \--init \--name=dhcpServer1 \-v ~/emxbook/docker-dhcpd/data:/data \networkboot/dhcpd v1
dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {option routers 192.168.1.1;option subnet-mask 255.255.255.0;option domain-name "emx.local";option domain-name-servers 8.8.8.8;range 192.168.1.2 192.168.1.100;}
3.5、將v2連接到交換機上
sudo ovs-vsctl add-port s1 v2
3.6、交換機s1、s2、s3彼此相連
sudo ovs-vsctl add-port s1 s1p1sudo ovs-vsctl set Interface s1p1 type=patchsudo ovs-vsctl set Interface s1p1 options:peer=s2p1sudo ovs-vsctl add-port s2 s2p1sudo ovs-vsctl set Interface s2p1 type=patchsudo ovs-vsctl set Interface s2p1 options:peer=s1p1sudo ovs-vsctl add-port s1 s1p2sudo ovs-vsctl set Interface s1p2 type=patchsudo ovs-vsctl set Interface s1p2 options:peer=s3p1sudo ovs-vsctl add-port s3 s3p1sudo ovs-vsctl set Interface s3p1 type=patchsudo ovs-vsctl set Interface s3p1 options:peer=s1p2
3.7、創建主機h1、h2、h3,並將其連接到交換機上,其中ubunut:5是添加了基本net-tools、isc-dhcp-client,創建的時候記得使用 --cap-add=NET_ADMIN
sudo docker run -itd --network=none --name=h1 --cap-add=NET_ADMIN ubuntu:5sudo docker run -itd --network=none --name=h2 --cap-add=NET_ADMIN ubuntu:5sudo docker run -itd --network=none --name=h3 --cap-add=NET_ADMIN ubuntu:5sudo ovs-docker add-port s1 eth1 h1sudo ovs-docker add-port s2 eth1 h2sudo ovs-docker add-port s3 eth1 h3
3.8、可以使用floot-light控制交換機s1、s2、s3交換機(選擇)
sudo ovs-vsctl set-controller s1 tcp:127.0.0.1:6653sudo ovs-vsctl set-controller s2 tcp:127.0.0.1:6653sudo ovs-vsctl set-controller s3 tcp:127.0.0.1:6653
4、使用dhclient eht1 向DHCP伺服器申請
4.1、申請ip之前
4.2、申請ip(伺服器端界面)
4.3申請ip之後,h1(192.168.1.38):
同理,h2(192.168.1.39):
h3(192.168.1.40):
5、測試
5.1、h1 ping h2(192.168.1.39):
5.2、h2 ping h3(192.168.1.40):
6、最後拓撲結構圖,ip分配之後的拓撲結構:DHCP伺服器(192.168.1.6)、h1(192.168.1.38)、h2(192.168.1.39)、h3(192.168.1.40)
7、代碼匯總參考
sudo ovs-vsctl add-br s1sudo ovs-vsctl add-br s2sudo ovs-vsctl add-br s3sudo ip link add v1 type veth peer name v2sudo ip link set dev v1 upsudo ip link set dev v2 upsudo ifconfig v1 192.168.1.6 netmask 255.255.255.0sudo docker run -it \--rm --net host \--init \--name=dhcpServer \-v ~/emxbook/docker-dhcpd/data:/data \networkboot/dhcpd v1sudo ovs-vsctl add-port s1 v2sudo ovs-vsctl add-port s1 s1p1sudo ovs-vsctl set Interface s1p1 type=patchsudo ovs-vsctl set Interface s1p1 options:peer=s2p1sudo ovs-vsctl add-port s2 s2p1sudo ovs-vsctl set Interface s2p1 type=patchsudo ovs-vsctl set Interface s2p1 options:peer=s1p1sudo ovs-vsctl add-port s1 s1p2sudo ovs-vsctl set Interface s1p2 type=patchsudo ovs-vsctl set Interface s1p2 options:peer=s3p1sudo ovs-vsctl add-port s3 s3p1sudo ovs-vsctl set Interface s3p1 type=patchsudo ovs-vsctl set Interface s3p1 options:peer=s1p2sudo docker run -itd --network=none --name=h1 --cap-add=NET_ADMIN ubuntu:5sudo docker run -itd --network=none --name=h2 --cap-add=NET_ADMIN ubuntu:5sudo docker run -itd --network=none --name=h3 --cap-add=NET_ADMIN ubuntu:5sudo ovs-docker add-port s1 eth1 h1sudo ovs-docker add-port s2 eth1 h2sudo ovs-docker add-port s3 eth1 h3sudo ovs-vsctl set-controller s1 tcp:127.0.0.1:6653sudo ovs-vsctl set-controller s2 tcp:127.0.0.1:6653sudo ovs-vsctl set-controller s3 tcp:127.0.0.1:6653