软件:radvd、dhcp
1)启用ipv6
vi /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv6.conf.lo.disable_ipv6 = 0
net.ipv6.conf.all.forwarding = 1
vi /etc/sysconfig/network
NETWORKING_IPV6=yes
IPV6FORWARDING=yes
HOSTNAME=dhcpserver
ONBOOT=yes
使配置生效并查看效果
sysctl -p
cat /proc/sys/net/ipv6/conf/all/forwarding
2)配置网卡
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=no
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=c99f931f-acbe-4769-99de-63625a57ddba
DEVICE=ens33
ONBOOT=yes
#IPADDR=x.x.x.x
#GATEWAY=x.x.x.x
#NETMASK=X.X.X.X
PEERDNS=yes
PEERROUTES=yes
IPV6ADDR=2100:8:8:8::1/64
#IPV6_DEFAULTGW=2100:8:8:8::1000
IPV6FORWARDING=yes
#//IPV6 SLAAC configure client
#IPV6_AUTOCONF=no
#DHCPV6C=yes
vi ifcfg-ens34
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=no
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens34
UUID=d684b872-93b2-44e2-af4e-56e9c0e3ee86
DEVICE=ens34
ONBOOT=yes
PEERDNS=yes
IPV6ADDR=2300:8:8:8::1/64
IPV6FORWARDING=yes
重启网络服务
systemctl restart network
3)安装并配置radvd
yum -y install radvd
vi /etc/radvd.conf
interface ens33
{
AdvSendAdvert on;
AdvManagedFlag off; #dhcpv6 无状态
AdvOtherConfigFlag on; #dhcpv6 无状态 如果AdvManagedFlag和AdvOtherConfigFlag都为on,就是dhcpv6 有状态
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
AdvLinkMTU 1480;
prefix 2100:8:8:8::/64 #dhcpv6 无状态 如果是dhcpv6有状态,需要注释掉
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr off;
};
# RDNSS 2300:8:8:8::1
# {};
};
systemctl enable radvd
systemctl start radvd
4)安装并配置DHCP
yum -y install dhcp
vi /etc/dhcp/dhcpd6.conf
default-lease-time 2592000;
preferred-lifetime 604800;
option dhcp-renewal-time 3600;
option dhcp-rebinding-time 7200;
allow leasequery;
option dhcp6.name-servers 2300:8:8:8::1;
option dhcp6.domain-search "test.example.com","example.com";
option dhcp6.preference 255;
option dhcp6.info-refresh-time 21600;
option dhcp6.rapid-commit;
dhcpv6-lease-file-name "/var/lib/dhcpd/dhcpd6.leases";
# The subnet where the server is attached
subnet6 2100:8:8:8::/64 {
# Range for clients
range6 2100:8:8:8::2 2100:8:8:8::1000; #DHCPV6 有状态时使用,dhcpv6 无状态时要注释掉
# Range for clients requesting a temporary address
#range6 2001:db8:0:1::/64 temporary;
# Additional options
#option dhcp6.name-servers fec0:0:0:1::1;
#option dhcp6.domain-search "domain.example";
# Prefix range for delegation to sub-routers
prefix6 2200:8:8:8:: 2200:8:8:1000:: /64;
}
systemctl enable dhcpd6
systemctl start dhcpd6
5)关闭防火墙
systemctl stop firewalld.service
注意:必须要关闭防火墙否则造成dhcpv6分配地址无法下发。
6)路由配置,可通过如下脚本来配置
cat /etc/pyFile/dhcpd6RoutePolicy.py
#!/usr/bin/python3
import os,re,time
os.system('ip -6 route add 2200:8:8::/48 via fe80::1111 dev ens33')
while 1:
try:
repDate = os.popen("cat /var/log/messages |grep 'Sending Reply'|tail -1").readlines()
repv6ip = re.findall(r'\bfe80[a-f|0-9|:]*\b',repDate[0])
rouDate = os.popen('ip -6 route |grep via').readlines()
if rouDate=='':
os.system('ip -6 route add 2200:8:8::/48 via %s dev ens33'%repv6ip[0])
else:
rouv6ip = re.findall(r'\bfe80[a-f|0-9|:]*\b',rouDate[0])
except Exception as e:
print(f"{e}")
finally:
if repv6ip == rouv6ip:
pass
else:
os.system('ip -6 route del 2200:8:8::/48 via %s dev ens33'%rouv6ip[0])
os.system('ip -6 route add 2200:8:8::/48 via %s dev ens33'%repv6ip[0])
time.sleep(30)
print("checking route of dhcpd6......")
7) Centos7中开机启动
chmod +x /etc/rc.d/rc.local
vi /etc/rc.d/rc.local
chmod +x /etc/pyFile/dhcpd6RoutePolicy.py
/usr/bin/python3 /etc/pyFile/dhcpd6RoutePolicy.py &
[root@localhost ~]# ps aux |grep python3
root 6913 0.0 0.7 134672 7388 ? S 19:42 0:00 /usr/bin/python3 /etc/pyFile/dhcpd6RoutePolicy.py
root 7150 0.0 0.0 112724 988 pts/0 S+ 19:44 0:00 grep --color=auto python3
[root@localhost ~]#
延伸----无状态与有状态之间切换的简易操作
[root@localhost pyFile]# pwd
/etc/pyFile
[root@localhost pyFile]# ls
DHCPD6.py dhcpd6RoutePolicy.py SLAAC.py
[root@localhost pyFile]#
[root@localhost pyFile]# ls /etc/radvd.conf
radvd.conf radvd.conf.bak radvd.conf.bak1 radvd.conf.bak2
[root@localhost pyFile]# ls /etc/dhcp/dhcpd6.conf
dhcpd6.conf dhcpd6.conf.bak dhcpd6.conf.bak1 dhcpd6.conf.bak2 dhcpd6.conf.rpmsave
-------DHCPD6 无状态
cat /etc/radvd.conf.bak1
interface ens33
{
AdvSendAdvert on;
AdvManagedFlag off;
AdvOtherConfigFlag on;
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
prefix 2100:8:8:8::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr off;
};
};
[root@localhost pyFile]# cat /etc/dhcp/dhcpd6.conf.bak1
default-lease-time 2592000;
preferred-lifetime 604800;
option dhcp-renewal-time 3600;
option dhcp-rebinding-time 7200;
allow leasequery;
option dhcp6.name-servers 2300:8:8:8::1;
option dhcp6.domain-search "test.example.com","example.com";
option dhcp6.preference 255;
option dhcp6.info-refresh-time 21600;
option dhcp6.rapid-commit;
dhcpv6-lease-file-name "/var/lib/dhcpd/dhcpd6.leases";
# The subnet where the server is attached
subnet6 2100:8:8:8::/64 {
# Prefix range for delegation to sub-routers
prefix6 2200:8:8:8:: 2200:8:8:1000:: /64;
}
[root@localhost pyFile]# cat SLAAC.py
#!/usr/bin/python3
import os,time
os.system('systemctl stop radvd')
time.sleep(1)
os.system('systemctl stop dhcpd6')
time.sleep(1)
os.system('cp /etc/radvd.conf.bak1 /etc/radvd.conf')
time.sleep(1)
os.system('cp /etc/dhcp/dhcpd6.conf.bak1 /etc/dhcp/dhcpd6.conf')
time.sleep(1)
os.system('systemctl start radvd')
time.sleep(1)
os.system('systemctl start dhcpd6')
time.sleep(1)
[root@localhost pyFile]#
----------DHCPD6 有状态
cat /etc/radvd.conf.bak2
interface ens33
{
AdvSendAdvert on;
AdvManagedFlag on;
AdvOtherConfigFlag on;
MinRtrAdvInterval 30;
MaxRtrAdvInterval 100;
};
[root@localhost pyFile]# cat /etc/dhcp/dhcpd6.conf.bak2
default-lease-time 2592000;
preferred-lifetime 604800;
option dhcp-renewal-time 3600;
option dhcp-rebinding-time 7200;
allow leasequery;
option dhcp6.name-servers 2300:8:8:8::1;
option dhcp6.domain-search "test.example.com","example.com";
option dhcp6.preference 255;
option dhcp6.info-refresh-time 21600;
option dhcp6.rapid-commit;
dhcpv6-lease-file-name "/var/lib/dhcpd/dhcpd6.leases";
# The subnet where the server is attached
subnet6 2100:8:8:8::/64 {
# Range for clients
range6 2100:8:8:8::2 2100:8:8:8::9;
# Prefix range for delegation to sub-routers
prefix6 2200:8:8:8:: 2200:8:8:1000:: /64;
}
cat DHCPD6.py
#!/usr/bin/python3
import os,time
os.system('systemctl stop radvd')
time.sleep(1)
os.system('systemctl stop dhcpd6')
time.sleep(1)
os.system('cp /etc/radvd.conf.bak2 /etc/radvd.conf')
time.sleep(1)
os.system('cp /etc/dhcp/dhcpd6.conf.bak2 /etc/dhcp/dhcpd6.conf')
time.sleep(1)
os.system('systemctl start radvd')
time.sleep(1)
os.system('systemctl start dhcpd6')
time.sleep(1)
[root@localhost pyFile]#
[root@localhost pyFile]# cd /etc/pyFile/
[root@localhost pyFile]# ls -al
总用量 24
drwxr-xr-x. 2 root root 67 3月 27 22:51 .
drwxr-xr-x. 77 root root 8192 3月 27 22:53 ..
-rwxr-xr-x. 1 root root 382 3月 27 22:22 DHCPD6.py
-rwxr-xr-x. 1 root root 1050 3月 27 22:51 dhcpd6RoutePolicy.py
-rwxrwxrwx. 1 root root 382 3月 27 22:22 SLAAC.py
chmod +x DHCPD6.py
chmod +x SLAAC.py
./SLAAC.py
./DHCPD6.py