设置主机名、网络、防火墙、selunx、镜像仓库地址vim init.sh
#!/bin/bash
echo -e "\n===========\e[32m设置hostname\e[0m=============="
# 设置主机名
read -p "Enter hostname(`hostname`):" hostname
if [[ $hostname ]];then
hostnamectl set-hostname $hostname
echo -e "\e[32m设置主机名:$hostname 成功!\e[0m"
else
echo -e "\e[32m设置主机名:`hostname` 成功!\e[0m"
fi
echo -e "\n===========\e[32m设置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 "设置网络ip(eg:192.168.1.1/24):" address
address=`echo $address | grep -Eoe ${cidr_reg}`
# 设置网关
read -p "设置网关(eg:192.168.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[31m输入的参数无效!\e[0m"
fi
# 是否关闭防火墙
echo -e "\n===========\e[32m设置firewalld\e[0m============="
read -p "是否关闭防火墙?[y/n]:" action
case $action in
y|Y|YES|yes)
systemctl disable firewalld && systemctl stop firewalld && echo -e "\e[32m防火墙关闭成功!\e[0m"
;;
n|N|NO|no)
systemctl enable firewalld && systemctl restart firewalld && echo -e "\e[32m防火墙启动成功!\e[0m"
;;
esac
# 是否关闭selinux
echo -e "\n===========\e[32m设置selinux\e[0m==============="
read -p "是否关闭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[32mselinux关闭成功!\e[0m"
;;
n|N|NO|no)
setenforce 1 && sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config && echo -e "\e[32mselinux开启成功!\e[0m"
;;
esac
echo -e "\n=============\e[32m设置yum\e[0m================="
read -p "设置yum镜像仓库(default:192.100.3.221):" yum_repository
yum_repository=`echo $yum_repository | grep -Eoe ${ip_reg}`
if [[ ! $yum_repository ]];then
yum_repository=192.100.3.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[32m测试镜像仓库的网络连通性\e[0m======"
ping yum_repository -w 3