#!/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