背景
公司新买了十台服务器并且已安装了Linux操作系统
需求
1.设置时区并同步时间
2.禁用selinux
3.清空防火墙默认策略
4.历史命令显示时间
5.禁止 root远程登录
6.禁止定时任务发送邮件
7.设置最大打开文件数
8.减少Swap使用
9.系统内核参数优化
10.安装系统性能分析工具及其他常用工具
#设置时间同步#!/bin/bashln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeif ! crontab -l|grep ntpdate &> /dev/null;then (echo "* 1 * * * ntpdate time.windows.com > /dev/null 2>&1";crontab -l)|crontabfi#设置软链接#如果 没有 设置时间同步定时命令 (将输入的内容移除)# 添加时间同步命令#关闭selinuxsed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config#直接编辑文件 将SELINUX这一行中的enforcing 替换为 disabled#关闭防火墙if egrep "7.[0-9]" /etc/redhat-release &> /dev/null;then systemctl stop firewalld systemctl disable firewalldelif egrep "7.[0-9]" /etc/redhat-release &> /dev/null;then service iptables stop chkconfig iptables offfi#如果是 7. 版本 # 执行7版本的关闭防火墙#如果是 6. 版本# 执行6版本的防火墙#历史命令显示操作时间if ! grep HISTTIMEFORMAT /etc/bashrc;then echo 'export HISTTIMEFORMAT="%F%T `whoami`"' >> /etc/bashrcfi#/etc/bashrc为每一个运行bash shell的用户执行此文件,当bash shell被打开时,该文件被读取。#如果 /etc/bashrc中没有HISTTIMEFORMAT# 追加变量到该文件中,%F年月日,%T时分秒 和 执行查看当前用户命令#SSH超时时间if ! grep "TMOUT=600" /etc/profile &> /dev/null;then echo "export TMOUT=600" >> /etc/profilefi#如果配置文件中没有TMOUT变量# 追加TMOUT全局变量到环境变量配置文件中#禁止root远程登录sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config#修改ssh配置文件,将注释的yes改为no#禁止定时任务向发送邮件sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab#将定时文件中的指定字符修改#设置最大打开文件数if ! grep "* soft nofile 65535" /etc/security/limits.conf &> /dev/null;then cat >> /etc/security/limits.conf << EOF * soft nofile 65535 * hard nofile 65535EOFfi#如果 文件中没有指定字符# 在文件末尾添加以下内容#系统内核优化cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_tw_buckets = 20480net.ipv4.tcp_max_syn_backlog= 20480net.core.netdev_max_backlog =262144net.ipv4.tcp_fin_timeout= 20EOF#直接添加优化参数到系统内核配置文件中#减少SWAP的使用echo "0" > /proc/sys/vm/swappiness#安装系统性能分析工具以及其他yum install -y gcc make autoconf vim systat net-tools htop tree mlocate ntpdate