背景:为了保证服务器安装,在进行系统安装都是采取最小安装,在业务部署之前,需要对服务器的系统进行初始化,这里例举需要优化的参考项,仅供参考
一、ssh设置
二、时间同步设置
三、最大并发数设置(打开的最大文件数)
四、字符集设置
五、selinux设置
六、防火墙设置
七、 内核优化
八、网络yum源配置
九、常用命令下载配置
这里仅仅配置了常用的优化项,具体根据业务需求。比如开机自启动项等也可以进行调整
#同步时间,依据网络时间服务器来同步
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 1>/dev/null
if ! crontab -l |grep ntpdate &>/dev/null ;then
(crontab -l;echo "* * * * 1 ntpdate time.windows.com &>/dev/null") | crontab
fi
#禁用selinux规则
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
#关闭防火墙
if egrep "7.[0-9]" /etc/redhat-release &>/dev/null;then
systemctl stop firewalld.service
systemctl disable firewalld.service
elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null;then
service iptables stop
chkconfig iptables off
fi
#设置历史命令的操作时间和操作人,方便留痕
if grep HISTTIMEFORMAT /etc/bashrc;then
echo 'export HISTTIMEFORMAT="%F %T `whoami`"' >> /etc/bashrc
source /etc/bashrc
fi
#设置远程超时时间
if ! grep "TMOUT" /etc/profile &>/dev/null ;then
echo 'export TMOUT=600' >> /etc/profile
source /etc/profile
fi
#禁用root用户远程登录,最大限度保证安全,这里还可以修改ssh默认端口等其他选项可以优化
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
# 修改自动定时任务中邮箱选项,节省空间
sed -i 's/MAILTO=root/MIALTO=""/g' /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 65535
eof
fi
#更新内核,这里是部分优化选项,仅供参考,具体根据服务器的实际用途和压力做调整
cat >>/etc/sysctl.conf<<-eof
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
eof
sysctl -p
#修改swap虚拟内存的使用规则,今年使用物理内存,这里设置为10,说明当内存使用量超过90%才会使用swap空间
echo "10" >/proc/sys/vm/swappiness
#配置网络yum源,并安装系统常用工具。可以选择阿里,网易等大公司的网络yum仓库
if ping -c3 www.baidu.com &>/dev/null;then
cd /etc/yum.repos.d/ || exit 1
mv CentOS-Base.repo CentOS-Base.repo.bak
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo &>/dev/null
yum clean all &>/dev/null
yum makecache &>/dev/null
yum instlal make gcc vim sysstat net-tools iftop rrzsz -y &>/dev/null
fi