Setup dhcpd/dhcpdv6 server
搭建DHCP server在许多网络应用中是非常必要的,尤其是测试IPv6相关的功能时,本文就来介绍下针对IPv4和IPv6该如何搭建DHCP server.
Install dhcpd server
首先通过apt
安装isc-dhcp-server
sudo apt install isc-dhcp-server
Setup dhcpd
Config interfaces
通过ifconfig
获取以太网口的接口名称, 下面省略了loopback接口lo
,后面也是一样默认省略。
$ ifconfig
enp4s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 20:6a:8a:6c:0f:da txqueuelen 1000 (Ethernet)
RX packets 154583 bytes 186953698 (186.9 MB)
RX errors 0 dropped 2 overruns 0 frame 0
TX packets 77038 bytes 6244250 (6.2 MB)
Tx erros 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 19
编辑配置文件/etc/default/isc-dhcp-server
,修改最后两行,设置为刚刚获取到的interface
$ tail -n 2 /etc/default/isc-dhcp-server
INTERFACESv4="enp4s0f0"
INTERFACESv6="enp4s0f0"
Config dhcpd server
首先针对IPv4配置dhcpd server,配置文件为/etc/dhcp/dhcpd.conf
, 配置内容包括网段,子网掩码,默认路由,默认DNS
$ tail -n 5 /etc/dhcp/dhcpd.conf
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.10 10.0.0.99;
option routers 10.0.0.138;
option domain-name-servers 10.0.0.138;
}
Add static ipv4
此时只要再给网口设置一个静态IP就可以了
sudo ifconfig enp4s0f0 10.0.0.138 netmask 255.255.255.0
Start isc-dhcp-server
sudo service isc-dhcp-server start
sudo service isc-dhcp-server status
通过第二条指令可以查看当前dhcpd的状态,显示running
表示已经正常工作
$ sudo service isc-dhcp-server status
● isc-dhcp-server.service - ISC DHCP IPv4 server
Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-01-18 04:22:28 CST; 7s ago
Docs: man:dhcpd(8)
Main PID: 13291 (dhcpd)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/isc-dhcp-server.service
└─13291 dhcpd -user dhcpd -group dhcpd -f -4 -pf /run/dhcp-server/dhcpd.pid -cf /etc/dhcp/dhcpd.conf
1月 18 04:22:28 litreily-Aspire-4750 dhcpd[13291]: PID file: /run/dhcp-server/dhcpd.pid
1月 18 04:22:28 litreily-Aspire-4750 sh[13291]: Wrote 1 leases to leases file.
1月 18 04:22:28 litreily-Aspire-4750 dhcpd[13291]: Wrote 1 leases to leases file.
1月 18 04:22:28 litreily-Aspire-4750 dhcpd[13291]: Listening on LPF/enp4s0f0/20:6a:8a:6c:0f:da/10.0.0.0/24
1月 18 04:22:28 litreily-Aspire-4750 dhcpd[13291]: Sending on LPF/enp4s0f0/20:6a:8a:6c:0f:da/10.0.0.0/24
1月 18 04:22:28 litreily-Aspire-4750 sh[13291]: Listening on LPF/enp4s0f0/20:6a:8a:6c:0f:da/10.0.0.0/24
1月 18 04:22:28 litreily-Aspire-4750 sh[13291]: Sending on LPF/enp4s0f0/20:6a:8a:6c:0f:da/10.0.0.0/24
1月 18 04:22:28 litreily-Aspire-4750 sh[13291]: Sending on Socket/fallback/fallback-net
1月 18 04:22:28 litreily-Aspire-4750 dhcpd[13291]: Sending on Socket/fallback/fallback-net
1月 18 04:22:28 litreily-Aspire-4750 dhcpd[13291]: Server starting service.
Setup dhcpdv6
配置IPv6的dhcpd与IPv4类似,上面已经配置过/etc/default/interfaces
,此处省略
config dhcpdv6 server
配置文件/etc/dhcp/dhcpd6.conf
$ tail -n 5 /etc/dhcp/dhcpd6.conf
subnet6 2001:470:19:1316::/64 {
range6 2001:470:19:1316::100 2001:470:19:1316::0fff;
option dhcp6.name-servers fe80::226a:8aff:fe6c:0fda;
prefix6 2001:470:19:1316:2:: 2001:470:19:1316:0fff:: /80;
}
上面配置了IPv6的网段,分配范围,默认dns,以及IPv6前缀
Add static IPv6
对于IPv6,需要添加两个ip,一个以fe80::
开头的本地ip,另外一个静态global ip
sudo ifconfig enp4s0f0 inet6 add 2001:470:19:1316::1/64
sudo ifconfig enp4s0f0 inet6 add fe80::226a:8aff:fe6c:fda/64
Start isc-dhcp-server6
dhcpdv6对应的server名称多了个6,启动方式自然也是一样的
sudo service isc-dhcp-server6 start
sudo service isc-dhcp-server6 status
Notice
在手动配置时,注意在启动服务之前,一定要记得配置好静态IP,否则无法启动server,并在查看状态时显示以下信息
$ sudo service isc-dhcp-server status
● isc-dhcp-server.service - ISC DHCP IPv4 server
Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2020-01-18 04:23:23 CST; 10s ago
Docs: man:dhcpd(8)
Process: 13864 ExecStart=/bin/sh -ec CONFIG_FILE=/etc/dhcp/dhcpd.conf; if [ -f /etc/ltsp/dhcpd.conf ]; then CONFIG_FILE=/etc/ltsp/dhcpd.conf; fi; [ -e /var/lib/dhcp/dhcpd.leases ] || touch /var/lib/dhcp/dhcpd.leases; chown root:dhcpd /var/lib/dhcp /var/lib/dhcp/dhcpd.leases; chmod 775 /var/lib/dhcp ; chmod 664 /var/lib/dhcp/dhcpd.leases; exec dhcpd -user dhcpd -group dhcpd -f -4 -pf /run/dhcp-server/dhcpd.pid -cf $CONFIG_FILE $INTERFACES (code=exited, status=1/FAILURE)
Main PID: 13864 (code=exited, status=1/FAILURE)
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]:
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]: If you think you have received this message due to a bug rather
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]: than a configuration issue please read the section on submitting
1月 18 04:23:23 litreily-Aspire-4750 systemd[1]: isc-dhcp-server.service: Main process exited, code=exited, status=1/FAILURE
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]: bugs on either our web page at www.isc.org or in the README file
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]: before submitting a bug. These pages explain the proper
1月 18 04:23:23 litreily-Aspire-4750 systemd[1]: isc-dhcp-server.service: Failed with result 'exit-code'.
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]: process and the information we find helpful for debugging..
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]:
1月 18 04:23:23 litreily-Aspire-4750 dhcpd[13864]: exiting.
Shell script
为了方便配置server,我写了一个简单的执行脚本,network servers control, 简称nsc
$ cat nsc
#!/bin/zsh
# network servers control
IFCONFIG=1
INTERFACE=enp4s0f0
IPv4=10.0.0.138
IPv6=2001:470:19:1316::1/64
IPv6_local=fe80::226a:8aff:fe6c:fda/64
stop_dhcp() {
# stop services
sudo service isc-dhcp-server stop
sudo service isc-dhcp-server6 stop
# del all default ip
if [ $IFCONFIG -eq 1 ]; then
sudo ifconfig $INTERFACE 0.0.0.0
sudo ifconfig $INTERFACE del $IPv6
sudo ifconfig $INTERFACE del $IPv6_local
else
sudo ip addr delete $IPv4/24 dev enp4s0f0
sudo ip addr delete $IPv6 dev enp4s0f0
sudo ip addr delete $IPv6_local dev enp4s0f0
fi
}
start_dhcp() {
# setup default ip
if [ $IFCONFIG -eq 1 ]; then
sudo ifconfig $INTERFACE $IPv4 netmask 255.255.255.0
sudo ifconfig $INTERFACE inet6 add $IPv6
sudo ifconfig $INTERFACE inet6 add $IPv6_local
else
sudo ip addr add $IPv4/24 dev enp4s0f0
sudo ip addr add $IPv6 dev enp4s0f0
sudo ip addr add $IPv6_local dev enp4s0f0
fi
# start services
sudo service isc-dhcp-server start
[ $? -eq 0 ] && echo "start dhcpd success!"
sudo service isc-dhcp-server6 start
[ $? -eq 0 ] && echo "start dhcpdv6 success!"
}
case "$1" in
"stop")
stop_dhcp
;;
"start")
start_dhcp
;;
esac
脚本中通过$IFCONFIG
变量来选择使用ifconfig
指令还是ip
指令进行配置。使用时只需执行nsc start
打开server, 执行nsc stop
关闭server.
版权声明:本博客所有文章除特殊声明外,均采用 CC BY-NC 4.0 许可协议。转载请注明出处 litreily的博客!