编写一个脚本可以匹配不同系统的服务器(包含7版本与8版本的系统)实现以下需求:
- 所有服务器永久关闭防火墙服务和SELinux
- 关闭7版本系统的命令历史记录,修改8版本的命令历史记录最多保存2000条并加上时间戳
- 关闭8版本系统的交换分区
- 定义root远程登录系统后的ssh保持时间为300秒
- 设置时间同步,ntp服务器地址是192.168.88.240
-
#!/bin/bash #脚本执行完后,用ssh远程登录测试 #可以先手工备份/etc/fstab和/etc/profile #1)判断当前账户身份,并关闭防火墙与selinux [ $UID -ne 0 ] && echo "请使用管理员操作" && exit systemctl stop firewalld systemctl disable firewalld setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #2)根据不同版本的系统执行各自的任务 egrep -q "\s+8\.[0-9]" /etc/redhat-release #判断系统版本 if [ $? -ne 0 ];then sed -ri 's/HISTSIZE=[0-9]+/HISTSIZE=0/' /etc/profile #关闭历史命令 else sed -ri 's/HISTSIZE=[0-9]+/HISTSIZE=2000/' /etc/profile #历史命令2000条 sed -i '/^export /i HISTTIMEFORMAT="%F %T "' /etc/profile #历史命令时间戳 sed -i '/^export /s/$/ HISTTIMEFORMAT/' /etc/profile #找到export开头那行,在最后加 HISTTIMEFORMAT,可以在子进程中也显示时间戳 swap=$(swapon | awk 'NR!=1{print $1}') #找到交换分区 for i in $swap do swapoff $i #循环关闭交换分区 done sed -i '/swap/s/^/#/' /etc/fstab #关闭交换分区自动挂载 fi #3)最后所有机器设置ssh超时时间与时间同步 echo "export TMOUT=300" >> ~/.bash_profile #定义ssh超时退出时间 yum -y install chrony systemctl enable chronyd sed -ri '/^(pool|server).*iburst/s/^/#/' /etc/chrony.conf sed -i '1i server 192.168.88.240 iburst' /etc/chrony.conf systemctl restart chronyd