IPv6是Linux系统的默认网络协议之一,但在某些情况下可能需要暂时或永久关闭它。以下是几种关闭IPv6的方法:
# 禁用所有接口的IPv6
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
# 或者禁用特定接口(如eth0)
sudo sysctl -w net.ipv6.conf.eth0.disable_ipv6=1
编辑/etc/sysctl.conf
文件:
sudo nano /etc/sysctl.conf
添加以下内容:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
然后应用更改:
sudo sysctl -p
编辑/etc/default/grub
文件:
sudo nano /etc/default/grub
找到GRUB_CMDLINE_LINUX
行,添加ipv6.disable=1
:
GRUB_CMDLINE_LINUX="ipv6.disable=1"
更新GRUB配置:
sudo update-grub # 对于Debian/Ubuntu
sudo grub2-mkconfig -o /boot/grub2/grub.cfg # 对于RHEL/CentOS
然后重启系统。
编辑/etc/ssh/sshd_config
:
AddressFamily inet # 只监听IPv4
然后重启SSH服务:
sudo systemctl restart sshd
ip a | grep inet6 # 如果没有输出,表示IPv6已禁用
需要重新启用IPv6时,只需撤销上述更改并重启系统即可。