常用Linux系统优化脚本

该脚本是我常用的系统安装后执行脚本,包括开机启动服务、内核、SSH优化。

#!/bin/bash

# 服务优化,(sshd、network、crond、syslog、rsyslog)服务保持默认开机启动
ServiceList=$(chkconfig --list | grep '0' | awk '{print $1}' | grep -Ev 'sshd|network|crond|syslog')

for Service in $ServiceList
do
    /etc/init.d/$Service stop
    chkconfig --level 0123456 $Service off
done

# 内核参数调优
grep -q "vm.swappiness" /etc/sysctl.conf || cat >> /etc/sysctl.conf << EOF
########################################
vm.swappiness = 0
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_max_tw_buckets = 10000
net.ipv4.ip_local_port_range = 1024 65500
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_mem = 786432 1048576 1572864
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.sem = 250 32000 100 128
fs.inotify.max_user_watches = 1048576
EOF
sysctl -p

grep -q "* - nofile" /etc/security/limits.conf || cat >> /etc/security/limits.conf << EOF
########################################
* - nofile 1048576
* - nproc  65536
* - stack  1024
EOF

grep -q "ulimit -n" /etc/profile || cat >> /etc/profile << EOF
########################################
ulimit -n 1048576
ulimit -u 65536
ulimit -s 1024

alias grep='grep --color=auto'
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
EOF

# 禁用并关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

# 优化SSH
sed -i 's/.*UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's/.*GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
/etc/init.d/sshd restart

# 脚本目录加入PATH环境变量
grep -q "/App/script" $HOME/.bash_profile || cat >> $HOME/.bash_profile << EOF
########################################
export PATH=/App/script:\$PATH
EOF

mkdir -p /App/script /App/src

# 挂载tmpfs文件系统
mount --bind /dev/shm /tmp
grep -q "/dev/shm" /etc/rc.local || echo "/bin/mount --bind /dev/shm /tmp" >> /etc/rc.local


本文出自 “松松” 博客,请务必保留此出处常用Linux系统优化脚本_51CTO博客_linux系统优化

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值