#!/usr/bin/bash
# DESC:system init
# Usage: sh system_init.sh hostname($1) hostip($2)
a () {
# 主机名设置
# 在main函数中以$1传入
hostnamectl set-hostname $1
echo "主机名已设置为:$(hostname)"
}
b () {
# IP地址设置
# main函数传入的是$2,但是b函数接收后是以$1接收的。
cat > /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
DEVICE=ens33
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.216.xxx
NETMASK=255.255.255.0
GATEWAY=192.168.216.2
DNS1=119.29.29.29
EOF
sed -i "/^IPADDR=/s/=.*/=$1/" /etc/sysconfig/network-scripts/ifcfg-ens33
# systemctl restart network &> /dev/null
ip=`cat /etc/sysconfig/network-scripts/ifcfg-ens33 |grep IPADDR`
echo "IP地址已设置为:$ip"
}
c () {
# 关闭firewalld
systemctl disable firewalld && systemctl stop firewalld
firewall-cmd --state &> /dev/null
if [ $? -eq 252 ];then
echo "firewalld已关闭"
fi
}
d () {
# 关闭SELINUX
if [ -f /etc/selinux/config ];then
sed -i.bak -r ‘/^SELINUX=/c\SELINUX=disabled‘ /etc/selinux/config
fi
echo "SELINUX已关闭,重启操作系统后生效,请稍后重启操作系统。"
}
e () {
# 清华大学开源软件镜像站YUM源配置
#yum.repos.d backup
mkdir -p /root/yum-back
mv /etc/yum.repos.d/* /root/yum-back/
#make tuna.repo
cat > /etc/yum.repos.d/tuna.repo <<EOF
[base]
name=CentOS-\$releasever - Base
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-\$releasever - Updates
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\$releasever - Plus
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
#remove cache
rm -rf /var/cache/yum &> /dev/null
yum clean all &> /dev/null
echo "清华大学开源软件镜像站YUM源已配置"
}
f () {
# 网络时钟源配置
if [ -f /etc/ntp.conf ];then
sed -i.bak -r ‘/^server [1-3]/d‘ /etc/ntp.conf
sed -i -r ‘/^server/c\server time1.aliyun.com‘ /etc/ntp.conf
fi
echo "网络时钟源已配置"
}
g () {
# 系统更新
yum update
echo "系统软件已更新"
}
main () {
cat << EOF
==========Linux System INIT==========
A|a)SET HOSTNAME
B|b)SET IP ADDRESS
C|c)DISABLE FIREWALLD
D|d)DISABLE SELINUX
E|e)SET YUM REPO
F|f)SET NTP SOURCE
G|g)UPDTAE SYSTEM
=====================================
EOF
read -p "请选择相应选项完成系统初始化[输入Q退出本程序]:" choice
while true
do
case $choice in
A|a)
clear
a $1
sleep 2
;;
B|b)
clear
b $2
sleep 2
;;
C|c)
clear
c
sleep 2
;;
D|d)
clear
d
sleep 2
;;
E|e)
clear
e
sleep 2
;;
F|f)
clear
f
sleep 2
;;
G|g)
clear
g
sleep 2
;;
Q|q)
echo "退出系统初始化程序。"
sleep 2
break
;;
*)
echo -e "\t\t\t\033[31m 请按菜单选择 \033[0m"
esac
cat << EOF
==========Linux System INIT==========
A|a)SET HOSTNAME
B|b)SET IP ADDRESS
C|c)DISABLE FIREWALLD
D|d)DISABLE SELINUX
E|e)SET YUM REPO
F|f)SET NTP SOURCE
G|g)UPDTAE SYSTEM
=====================================
EOF
read -p "请选择相应选项完成系统初始化[输入Q退出本程序]:" choice
done
}
# callable
main $1 $2
?