#!/bin/bash
#Rocky 9.1 Linux 禁用 ipv6
#2023-05-10
#version 1.2
#by xc
##############################################
GRUPFILE='/etc/default/grub'
NETDIR='/etc/NetworkManager/system-connections'
NETNAME=`ls $NETDIR | grep 'nmconnection'`
TCPSIX=`netstat -luntp |grep tcp6|wc -l`
IPVSIX=`grep -o "ipv6.disable" /etc/default/grub`
clear
date
#判断grub文件中是否有ipv6.disable以及tcp6是否还存在。防止多次执行脚本出现的错误。
if [[ -n $IPVSIX ]] && [[ $TCPSIX -ge 1 ]];then
echo -e "\nIPV6 in grub file has been modified."
echo -e "\nPlease restart the system by yourself."
exit
fi
#检测IPV6是否被禁止
if [ $TCPSIX -eq 0 ];then
echo "IPV6 has been disabled in your system, so you don't need to run this script anymore."
exit
fi
sleep 1
echo -e "Modifying grub file. \n"
#修改grub.cfg文件,添加ipv6.disable=1
ETH_X=`route -n|awk '{print $8}' |grep "eth[0-9]"`
if [[ -z $ETH_X ]]; then
##网卡名称不是ethx 的时候,禁止IPV6
sed -i 's/GRUB_CMDLINE_LINUX="/GRUB_CMDLINE_LINUX="ipv6.disable=1 /' $GRUPFILE
else
##网卡名称已经是ethx 的时候,禁止IPV6
sed -i 's/GRUB_CMDLINE_LINUX="/GRUB_CMDLINE_LINUX="net.ifnames=0 ipv6.disable=1 /' $GRUPFILE
fi
#重新生成grup文件
grub2-mkconfig -o /boot/grub2/grub.cfg
sleep 1
systemctl restart network
if [ $? -ne 0 ];then
echo "Please restart your network serivces."
fi
echo -e "The grub file has been modified. Please choose whether you need to restart the system.\n"
echo -e "You have 7 seconds to choose.\n"
read -p "Please select and press Enter. (y/n): " -t 7 READY
if [[ $READY == 'y' ]];then
echo "System is restarting"
init 6
elif [[ $READY == 'n' ]];then
echo "Please restart the system by yourself."
exit
else
echo -e "\nInput error or no input. \nPlease restart the system by yourself."
exit
fi