448 lines
16 KiB
Bash
448 lines
16 KiB
Bash
|
#!/bin/bash
|
||
|
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||
|
|
||
|
key_value_editer()
|
||
|
{
|
||
|
local file=$1
|
||
|
local key=$2
|
||
|
local value=$3
|
||
|
|
||
|
[ ! -f "$file" ] && return
|
||
|
|
||
|
if ! grep -i "^${key}[[:space:]]*=" "$file" &>/dev/null; then
|
||
|
echo "$key=$value" >> "$file"
|
||
|
else
|
||
|
value=${value//\//\\/}
|
||
|
sed -i "s/^${key}[[:space:]]*=.*/$key=$value/" "$file"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
enable_sysctl_conf(){
|
||
|
files="/etc/sysctl.conf /etc/sysctl.conf.first"
|
||
|
for file in $files
|
||
|
do
|
||
|
key_value_editer "$file" net.ipv6.conf.all.disable_ipv6 0
|
||
|
key_value_editer "$file" net.ipv6.conf.default.disable_ipv6 0
|
||
|
key_value_editer "$file" net.ipv6.conf.lo.disable_ipv6 0
|
||
|
done
|
||
|
sysctl -p
|
||
|
}
|
||
|
|
||
|
get_redhat_centos_ver()
|
||
|
{
|
||
|
if [ -f /etc/centos-release ]; then
|
||
|
echo `sed 's/^.*release \([0-9]\).*$/\1/' /etc/centos-release`
|
||
|
elif [ -f /etc/redhat-release ]; then
|
||
|
echo `sed 's/^.*release \([0-9]\).*$/\1/' /etc/redhat-release`
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
get_ubuntu_ver()
|
||
|
{
|
||
|
version=`cat /etc/issue|head -1|awk '{print $2}'|awk -F '.' '{print $1}'`
|
||
|
echo $version
|
||
|
return
|
||
|
}
|
||
|
|
||
|
grub_ipv6_enabled()
|
||
|
{
|
||
|
# check if need to del grub ipv6 disable
|
||
|
if grep -q "ipv6.disable=1" /proc/cmdline; then
|
||
|
# local ipv6_disable='ipv6.disable=1'
|
||
|
# del_params_grub $ipv6_disable
|
||
|
case ${ostype} in
|
||
|
coreos)
|
||
|
sed -i -e "s/set linux_append=\"\$linux_append ipv6.disable=1\"//" /usr/share/oem/grub.cfg
|
||
|
;;
|
||
|
esac
|
||
|
g_need_reboot=1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
check_os_type()
|
||
|
{
|
||
|
# ostype: tlinux|opensuse|suse|centos|redhat|ubuntu|debian|coreos
|
||
|
while [ true ];do
|
||
|
if [ -f /etc/tlinux-release ];then
|
||
|
echo tlinux
|
||
|
return
|
||
|
fi
|
||
|
if [ -f /etc/SuSE-release ];then
|
||
|
grep -i "opensuse" /etc/SuSE-release >/dev/null 2>/dev/null && echo "opensuse" || echo "suse"
|
||
|
return
|
||
|
fi
|
||
|
if [ -f /etc/SUSE-brand ];then
|
||
|
grep -i "opensuse" /etc/SUSE-brand &>/dev/null && echo "opensuse" || echo "suse"
|
||
|
return
|
||
|
fi
|
||
|
if [ -f /etc/centos-release ];then
|
||
|
echo centos
|
||
|
return
|
||
|
fi
|
||
|
#centos5 and redhat5
|
||
|
if [ -f /etc/redhat-release ];then
|
||
|
grep "Red Hat" /etc/redhat-release >/dev/null
|
||
|
if [ $? -eq 0 ];then
|
||
|
echo redhat
|
||
|
return
|
||
|
fi
|
||
|
grep CentOS /etc/redhat-release >/dev/null
|
||
|
if [ $? -eq 0 ];then
|
||
|
echo centos
|
||
|
return
|
||
|
fi
|
||
|
fi
|
||
|
break
|
||
|
done
|
||
|
for os in ubuntu debian coreos;do grep ^ID=${os}$ /etc/os-release >/dev/null 2>/dev/null && echo ${os} && return; done
|
||
|
grep -i =ubuntu /etc/lsb-release >/dev/null 2>/dev/null && echo ubuntu && return
|
||
|
[ -f /etc/freebsd-update.conf ] && echo FreeBSD
|
||
|
}
|
||
|
|
||
|
is_network_static()
|
||
|
{
|
||
|
#if network is static, config ipv6 by manual
|
||
|
|
||
|
case ${ostype} in
|
||
|
centos)
|
||
|
if grep -q "IPADDR=" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg";then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
tlinux)
|
||
|
if grep -q "IPADDR=" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg";then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
ubuntu)
|
||
|
local osver=$(get_ubuntu_ver)
|
||
|
case $osver in
|
||
|
"18"|"20")
|
||
|
if grep -r -q "addresses:" /etc/netplan/ ;then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
"16")
|
||
|
if grep -r -q "inet static" /etc/network/interfaces.d/ ;then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
"14")
|
||
|
if grep -r -q "inet static" /etc/network/interfaces.d/ ;then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
esac
|
||
|
;;
|
||
|
debian)
|
||
|
if grep -q "inet static" /etc/network/interfaces ;then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
opensuse)
|
||
|
if grep -q "static" /etc/sysconfig/network/ifcfg-"$ifcfg" ;then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
coreos)
|
||
|
if grep -q "Address=" /etc/systemd/network/"$ifcfg"-*.network ;then
|
||
|
return 1
|
||
|
fi
|
||
|
;;
|
||
|
FreeBSD)
|
||
|
echo "Please do ipv6 config by manual, refer to the documentation"
|
||
|
exit
|
||
|
;;
|
||
|
esac
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
config_ssh_ipv6()
|
||
|
{
|
||
|
#config ssh ipv6
|
||
|
if grep -q "AddressFamily any" /etc/ssh/sshd_config; then
|
||
|
sed -i -e 's/#AddressFamily any/AddressFamily any/' /etc/ssh/sshd_config
|
||
|
else
|
||
|
sed -i '/^AddressFamily/d' $file
|
||
|
echo "AddressFamily any" >> /etc/ssh/sshd_config
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
config_ipv6()
|
||
|
{
|
||
|
echo "config ipv6 ip/route and ssh_v6"
|
||
|
|
||
|
is_network_static
|
||
|
local res=$(echo $?)
|
||
|
if [ "$res" == "1" ];then
|
||
|
echo "ipv6 has been enabled, please config ipv6 address by manual, because the vpc0 is static network"
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
netip=fe80::feee:ffff:feff:ffff
|
||
|
case ${ostype} in
|
||
|
centos)
|
||
|
if [ "$(get_redhat_centos_ver)" == "8" ];then
|
||
|
dhclient -6
|
||
|
|
||
|
! grep -q "IPV6INIT=yes" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" && echo "IPV6INIT=yes" >> /etc/sysconfig/network-scripts/ifcfg-"$ifcfg"
|
||
|
! grep -q "IPV6_AUTOCONF=yes" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" && echo "IPV6_AUTOCONF=yes" >> /etc/sysconfig/network-scripts/ifcfg-"$ifcfg"
|
||
|
! grep -q "IPV6_DEFROUTE=yes" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" && echo "IPV6_DEFROUTE=yes" >> /etc/sysconfig/network-scripts/ifcfg-"$ifcfg"
|
||
|
! grep -q "IPV6_FAILURE_FATAL=no" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" && echo "IPV6_FAILURE_FATAL=no" >> /etc/sysconfig/network-scripts/ifcfg-"$ifcfg"
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
|
||
|
cat >> /etc/NetworkManager/dispatcher.d/99-eth0-ipv6-add-default-route << EOF
|
||
|
# !/bin/bash
|
||
|
if [[ "\${1}" -eq "eth0" ]] && [[ "\${2}" -eq "up" ]]; then
|
||
|
ip -6 route add default dev "\${1}" via fe80::feee:ffff:feff:ffff
|
||
|
fi
|
||
|
exit 0
|
||
|
EOF
|
||
|
chmod +x /etc/NetworkManager/dispatcher.d/99-eth0-ipv6-add-default-route
|
||
|
else
|
||
|
if [ "$(get_redhat_centos_ver)" == "7" ];then
|
||
|
systemctl stop NetworkManager
|
||
|
systemctl disable NetworkManager
|
||
|
fi
|
||
|
|
||
|
#config DHCPV6C
|
||
|
dhclient -6
|
||
|
|
||
|
! grep -q "DHCPV6C=yes" /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" && echo "DHCPV6C=yes" >> /etc/sysconfig/network-scripts/ifcfg-"$ifcfg"
|
||
|
|
||
|
#netip=$(ip -6 address show | grep inet6 | awk '{print $2}' | cut -d'/' -f1 | sed -n '2,2p' | awk -F":" '{print $1":"$2":"$3":"$4}')
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
|
||
|
ipv6_route_file="/etc/sysconfig/network-scripts/route6-$ifcfg"
|
||
|
if [ ! -f "$ipv6_route_file" ];
|
||
|
then
|
||
|
touch "$ipv6_route_file"
|
||
|
fi
|
||
|
|
||
|
grep -q "default dev $ifcfg" "$ipv6_route_file"
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" "$ipv6_route_file"
|
||
|
fi
|
||
|
# add new
|
||
|
echo "default dev $ifcfg via $netip" >> "$ipv6_route_file"
|
||
|
fi
|
||
|
config_ssh_ipv6
|
||
|
service sshd reload
|
||
|
;;
|
||
|
debian)
|
||
|
dhclient -6
|
||
|
! grep -q "iface $ifcfg inet6 dhcp" /etc/network/interfaces && echo "iface $ifcfg inet6 dhcp" >> /etc/network/interfaces
|
||
|
|
||
|
#netip=$(ip -6 address show | grep inet6 | awk '{print $2}' | cut -d'/' -f1 | sed -n '2,2p' | awk -F":" '{print $1":"$2":"$3":"$4}')
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
|
||
|
grep -q "up ip -6 route add default dev $ifcfg" /etc/network/interfaces
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" /etc/network/interfaces
|
||
|
fi
|
||
|
echo "up ip -6 route add default dev $ifcfg via $netip" >> /etc/network/interfaces
|
||
|
|
||
|
config_ssh_ipv6
|
||
|
service ssh reload
|
||
|
;;
|
||
|
opensuse)
|
||
|
local sure=""
|
||
|
while [ "$sure" != "N" ] && [ "$sure" != "n" ] && [ "$sure" != "Y" ] && [ "$sure" != "y" ]
|
||
|
do
|
||
|
read -p "We will restart network, are you sure?(Y/N) :" sure
|
||
|
if [ "$sure" == "N" ] || [ "$sure" == "n" ];then
|
||
|
exit
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
local ifcfg_file="/etc/sysconfig/network/ifcfg-$ifcfg"
|
||
|
|
||
|
systemctl restart network # default use wickedd-dhcp6 service
|
||
|
|
||
|
#netip=$(ip -6 address show | grep inet6 | awk '{print $2}' | cut -d'/' -f1 | sed -n '2,2p' | awk -F":" '{print $1":"$2":"$3":"$4}')
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
|
||
|
grep -q "ip -6 route add default dev $ifcfg" /etc/rc.d/after.local
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" /etc/rc.d/after.local
|
||
|
fi
|
||
|
echo "ip -6 route add default dev $ifcfg via $netip" >> /etc/rc.d/after.local
|
||
|
|
||
|
! grep -q "default $netip - $ifcfg" /etc/sysconfig/network/routes && echo "default $netip - $ifcfg" >> /etc/sysconfig/network/routes # avoid restart network to lose ipv6 address
|
||
|
;;
|
||
|
suse)
|
||
|
echo "ipv6 has been enabled, please do ipv6 config by manual, refer to the documentation "
|
||
|
;;
|
||
|
ubuntu)
|
||
|
dhclient -6
|
||
|
#netip=$(ip -6 address show | grep inet6 | awk '{print $2}' | cut -d'/' -f1 | sed -n '2,2p' | awk -F":" '{print $1":"$2":"$3":"$4}')
|
||
|
local osver=$(get_ubuntu_ver)
|
||
|
case $osver in
|
||
|
"18"|"20")
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
|
||
|
# ! grep -q "dhcp6:[[:space:]]*yes" /etc/netplan/50-cloud-init.yaml && echo "dhcp6: yes" >> /etc/netplan/50-cloud-init.yaml
|
||
|
sed -i "/$ifcfg:/a\ dhcp6: yes" /etc/netplan/50-cloud-init.yaml
|
||
|
|
||
|
# refer to https://netplan.io/faq/#use-pre-up-post-up-etc-hook-scripts
|
||
|
echo '#!/bin/bash' > /etc/networkd-dispatcher/routable.d/50-ifup-hooks
|
||
|
echo "ip -6 route add default dev $ifcfg via $netip" >> /etc/networkd-dispatcher/routable.d/50-ifup-hooks
|
||
|
echo "exit 0" >> /etc/networkd-dispatcher/routable.d/50-ifup-hooks
|
||
|
|
||
|
chmod +x /etc/networkd-dispatcher/routable.d/50-ifup-hooks
|
||
|
;;
|
||
|
"16")
|
||
|
! grep -q "iface $ifcfg inet6 dhcp" /etc/network/interfaces && echo "iface $ifcfg inet6 dhcp" >> /etc/network/interfaces
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
grep -q "up ip -6 route add default dev $ifcfg" /etc/network/interfaces
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" /etc/network/interfaces
|
||
|
fi
|
||
|
echo "up ip -6 route add default dev $ifcfg via $netip" >> /etc/network/interfaces
|
||
|
;;
|
||
|
"14")
|
||
|
! grep -q "iface $ifcfg inet6 dhcp" /etc/network/interfaces && echo "iface $ifcfg inet6 dhcp" >> /etc/network/interfaces
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
grep -q "up ip -6 route add default dev $ifcfg" /etc/network/interfaces
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" /etc/network/interfaces
|
||
|
fi
|
||
|
echo "up ip -6 route add default dev $ifcfg via $netip" >> /etc/network/interfaces
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
config_ssh_ipv6
|
||
|
service ssh reload
|
||
|
;;
|
||
|
tlinux)
|
||
|
dhclient -6
|
||
|
# netip=$(ip -6 address show | grep inet6 | awk '{print $2}' | cut -d'/' -f1 | sed -n '2,2p' | awk -F":" '{print $1":"$2":"$3":"$4}')
|
||
|
local version=$(head -1 /etc/issue | awk '{print $4}' 2>/dev/null)
|
||
|
case ${version} in
|
||
|
"1.2")
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" IPV6INIT "'yes'"
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" PERSISTENT_DHCLIENT "'yes'"
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" DHCPV6C "'yes'"
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
ipv6_route_file="/etc/sysconfig/network-scripts/route6-$ifcfg"
|
||
|
if [ ! -f "$ipv6_route_file" ];
|
||
|
then
|
||
|
touch $ipv6_route_file
|
||
|
fi
|
||
|
|
||
|
grep -q "default dev $ifcfg" "$ipv6_route_file"
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" "$ipv6_route_file"
|
||
|
fi
|
||
|
echo "default dev $ifcfg via $netip" >> "$ipv6_route_file"
|
||
|
|
||
|
sed -i 's/ListenAddress/# ListenAddress/' /etc/ssh/sshd_config
|
||
|
;;
|
||
|
"2.2")
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" DHCPV6C yes
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
ipv6_route_file="/etc/sysconfig/network-scripts/route6-$ifcfg"
|
||
|
if [ ! -f "$ipv6_route_file" ];
|
||
|
then
|
||
|
touch $ipv6_route_file
|
||
|
fi
|
||
|
|
||
|
grep -q "default dev $ifcfg" "$ipv6_route_file"
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" "$ipv6_route_file"
|
||
|
fi
|
||
|
echo "default dev $ifcfg via $netip" >> "$ipv6_route_file"
|
||
|
;;
|
||
|
"2.4")
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" DHCPV6C yes
|
||
|
;;
|
||
|
*)
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" IPV6INIT "'yes'"
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" PERSISTENT_DHCLIENT "'yes'"
|
||
|
key_value_editer /etc/sysconfig/network-scripts/ifcfg-"$ifcfg" DHCPV6C "'yes'"
|
||
|
|
||
|
ip -6 route delete default dev "$ifcfg"
|
||
|
ip -6 route add default dev "$ifcfg" via "$netip"
|
||
|
ipv6_route_file="/etc/sysconfig/network-scripts/route6-$ifcfg"
|
||
|
if [ ! -f "$ipv6_route_file" ];
|
||
|
then
|
||
|
touch $ipv6_route_file
|
||
|
fi
|
||
|
|
||
|
grep -q "default dev $ifcfg" "$ipv6_route_file"
|
||
|
if [ $? -eq 0 ];then
|
||
|
# delete origin
|
||
|
sed -i "/default dev $ifcfg/d" "$ipv6_route_file"
|
||
|
fi
|
||
|
echo "default dev $ifcfg via $netip" >> "$ipv6_route_file"
|
||
|
|
||
|
sed -i 's/ListenAddress/# ListenAddress/' /etc/ssh/sshd_config
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
config_ssh_ipv6
|
||
|
service sshd reload
|
||
|
;;
|
||
|
coreos)
|
||
|
grub_ipv6_enabled
|
||
|
enable_sysctl_conf
|
||
|
! grep -q "ip -6 route add default dev $ifcfg" /etc/rc.d/rc.local && echo "ip -6 route add default dev $ifcfg via $netip" >> /etc/rc.d/rc.local
|
||
|
g_need_reboot=1
|
||
|
;;
|
||
|
FreeBSD)
|
||
|
echo "Please do ipv6 config by manual, refer to the documentation"
|
||
|
;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
# ostype: tlinux|opensuse|suse|centos|redhat|ubuntu|debian|freebsd|coreos
|
||
|
if [ "$#" == "0" ];then
|
||
|
echo "please execute like: ./config_ipv6.sh eth0"
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
echo "Note: Please make sure your ipv6 address has been assigned at web console, Otherwise this process will fail"
|
||
|
echo " if you have not assign a ipv6 address, please refer to https://cloud.tencent.com/document/product/1142/38130"
|
||
|
|
||
|
ifcfg=$1
|
||
|
sure=""
|
||
|
while [ "$sure" != "N" ] && [ "$sure" != "n" ] && [ "$sure" != "Y" ] && [ "$sure" != "y" ]
|
||
|
do
|
||
|
read -p "We will config network on $ifcfg and it cloud not be revoke, are you sure?(Y/N) :" sure
|
||
|
if [ "$sure" == "N" ] || [ "$sure" == "n" ];then
|
||
|
exit
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
ostype=$(check_os_type)
|
||
|
g_need_reboot=0
|
||
|
|
||
|
[ "$ostype" == "FreeBSD" ] || [ "$ostype" == "suse" ] && echo "For FreeBSD/SUSE, please do ipv6 config by manual, refer to the documentation" && exit
|
||
|
config_ipv6
|
||
|
|
||
|
[ $g_need_reboot -eq 1 ] && echo "need to reboot"
|
||
|
echo "config ipv6 finished"
|