init.sh
#!/bin/bash
echo -e "\n===========\e[32mset hostname\e[0m=============="
# 设置主机名
read -p "Enter hostname(`hostname`):" hostname
if [[ $hostname ]];then
hostnamectl set-hostname $hostname
echo -e "\e[32mset hostname:$hostname success!\e[0m"
else
echo -e "\e[32mset hostname:`hostname` success!\e[0m"
fi
echo -e "\n===========\e[32mset network\e[0m==============="
# 正则
cidr_reg="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])/([1-9]|[1-2][0-9]|3[12])$"
ip_reg="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
# 设置网络ip
read -p "set ip(eg:192.168.1.1/24):" address
address=`echo $address | grep -Eoe ${cidr_reg}`
# 设置网关
read -p "set gateway(eg:10.10.1.254):" gateway
gateway=`echo $gateway | grep -Eoe ${ip_reg}`
if [[ $address && $gateway ]];then
nmcli connection modify eth0 \
connection.autoconnect yes \
ipv4.method manual \
ipv4.addresses $address \
ipv4.gateway $gateway
# 激活网卡
nmcli connection up eth0
else
echo -e "\e[31mInvalid parameter!\e[0m"
fi
# 是否关闭防火墙
echo -e "\n===========\e[32mset firewalld\e[0m============="
read -p "Are you sure you want to close firewalld?[y/n]:" action
case $action in
y|Y|YES|yes)
systemctl disable firewalld && systemctl stop firewalld && echo -e "\e[32msuccess!\e[0m"
;;
n|N|NO|no)
systemctl enable firewalld && systemctl restart firewalld && echo -e "\e[32msuccess!\e[0m"
;;
esac
# 是否关闭selinux
echo -e "\n===========\e[32mset selinux\e[0m==============="
read -p "Are you sure you want to close selinux?[y/n]:" action
case $action in
y|Y|YES|yes)
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && echo -e "\e[32msuccess!\e[0m"
;;
n|N|NO|no)
setenforce 1 && sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config && echo -e "\e[32msuccess!\e[0m"
;;
esac
echo -e "\n=============\e[32mset yum\e[0m================="
read -p "set ip of the mirror repository(default:192.100.3.221):" yum_repository
yum_repository=`echo $yum_repository | grep -Eoe ${ip_reg}`
if [[ ! $yum_repository ]];then
yum_repository=10.10.1.221
fi
if [[ `grep "yum_repository" /etc/hosts` ]];then
sed -ri "/yum_repository/"c"$yum_repository yum_repository" /etc/hosts
else
echo "$yum_repository yum_repository" >> /etc/hosts
fi
echo -e "\n======\e[32mTest the network connectivity of the mirror repository\e[0m======"
ping yum_repository -w 3