centos7 系統初始化腳本

#!/bin/bash
while :
do
cat <<-EOF
+-------------------------------------------------------------------------+
| System_tools V1.0 |
+-------------------------------------------------------------------------+
| a. Stop And Disabled Firewalld. |
| b. 修改主機名. |
| c. 檢查網絡連通性. |
| d. 統一網卡名爲eth. |
| e. 修改ssh配置文件。 |
| f. 配置yum源倉庫。 |
| g. 創建普通用戶並提權。 |
| h. 300秒不操作自動註銷root賬戶。 |
| i. 時間同步。 |
| q. 退出。 |
+-------------------------------------------------------------------------+
EOF
network_dir="/etc/sysconfig/network-scripts/"
sshd_dir="/etc/ssh/"
network_name=ip a | grep '^2:' |awk -F "[ :]" '{print $3}'
stop_firewalld()
{
echo "-----------Stop And Disabled Firewalld and selinux---------"
systemctl stop firewalld
systemctl disable firewalld &> /dev/null
setenforce 0
sed -i "/^\bSELINUX\b/c SELINUX=disabled" /etc/selinux/config
if [ $? -eq 0 ]
then
echo "firewalld and selinux stop successed"
else
echo "firewalld and selinux stop failed"
exit
fi
}

username()
{
read -p "請輸入你要修改的主機名" user
hostnamectl set-hostname $user
echo "你修改的主機名爲 $user"
}

network_tools()
{
ping -c1 www.baidu.com &> /dev/null
if [ $? -eq 0 ]
then
echo "你的網絡狀況良好"
else
echo "你的網絡不好使,需要重新配置"
read -p "請輸入你的網卡名稱" name
read -p "請輸入你的IP地址:" ip1
ip2=echo $ip1 |awk -F"." 'BEGIN{FS="."; OFS="."}{print $1,$2,$3}'
cat > ${network-dir}ifcfg-${name} << EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=$name
DEVICE=$name
ONBOOT=yes
IPADDR=$ip1
GATEWAY=$ip2
EOF
ping c1 www.baidu.com &> /dev/null
if [ $? -eq 0 ]
then
echo "網絡已恢復"
else
echo "沒救了"
fi
fi
}

eth()
{
echo "---------正在配置請稍等----------"
mv ${network_dir}ifcfg-${network_name} ${network_dir}ifcfg-eth0
sed -i '/^NAME/c NAME=eth0' ${network_dir}ifcfg-eth0
sed -i '/^DEVICE/c DEVICE=eth0' ${network_dir}ifcfg-eth0
echo 'GRUB_CMDLINE_LINUX="...... net.ifnames=0"' >> /etc/sysconfig/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
echo "請重啓使網卡名生效"
}
ssh_tools()
{
sed -i '/\bPort\b/c Port=22' ${sshd_dir}sshd_config
while :
do
read -p "確定禁止root用戶遠程登錄 y/n" login
case $login in
y)
sed -i '/#PermitRootLogin/c PermitRootLogin = NO' ${sshd_dir}sshd_config
;;
n)
exit
;;
)
echo "請按照提示輸入內容!!!"
;;
esac
done
}
yum_install()
{
echo "--------正在部署yum源倉庫請喝口水耐心等待---------"
rm -rf /etc/yum.repos.d/

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum -y install wget &> /dev/null
if [ $? -eq 0 ]
then
echo "快要成功了"
else
echo "網絡錯誤,請檢查網絡"
exit
fi
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
echo "---------正在清空緩存,請耐心等待!----------"
yum clean all &> /dev/null
echo "-----------正在重新加載,請耐心等待!-----------"
yum makecache &> /dev/null
echo "------------yum 配置 successed------------"
}
user(){
read -p "請輸入你要創建的用戶名:" n
read -p "請輸入用戶名的密碼" mima
useradd $n && echo "$mima" |passwd --stdin $n &> /dev/null
usermod -aG wheel $n
if [ $? -eq 0 ]
then
echo "用戶創建並提權成功"
else
echo "用戶創建失敗"
exit
fi

}

root_power_off(){
sed -i '/HISTSIZE=/a\TMOUT=300' /etc/profile && echo "-----successful------"
#300s不操作自動註銷root賬戶
}

time_ntp(){
#將/usr/share/zoneinfo/Asia/Shanghai 拷貝到 /etc/localtime
#說有違禁詞不讓我寫拷貝這個命令
#cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum -y install ntpdate &>/dev/null
ntpdate 10.180.4.204
echo "時間同步成功"

}

read -p "請輸入你要選擇的參數:" a
case $a in
a)
stop_firewalld
;;
b)
username
;;
c)
network_tools
;;
d)
eth
;;
e)
ssh_tools
;;
f)
yum_install
;;
g)
user
;;
h)
root_power_off
;;
i)
time_ntp
;;
q)
exit
;;
*)
echo "請按照上方提示輸入!!!"
;;

esac
done

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章