以下是一个全面的CentOS 7系统优化脚本,包含性能调优、安全加固和常用配置优化:
#!/bin/bash
# CentOS 7 系统优化脚本
# 需要root权限执行
# 检查是否为root用户
if [ "$(id -u)" -ne 0 ]; then
echo "请使用root用户执行此脚本!"
exit 1
fi
# 1. 系统更新
echo "正在更新系统..."
yum update -y
yum upgrade -y
# 2. 安装常用工具
echo "正在安装常用工具..."
yum install -y epel-release
yum install -y htop iftop iotop sysstat net-tools wget curl vim git lsof telnet \
tmux tree unzip ncdu jq bash-completion yum-utils
# 3. 内核参数优化
echo "正在优化内核参数..."
cat > /etc/sysctl.d/99-sysctl.conf <<EOF
# 系统性能优化
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_max_orphans = 3276800
# 内存相关优化
vm.swappiness = 10
vm.overcommit_memory = 1
vm.dirty_ratio = 40
vm.dirty_background_ratio = 10
vm.dirty_expire_centisecs = 500
vm.dirty_writeback_centisecs = 100
# 网络性能优化
net.core.somaxconn = 32768
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1
EOF
# 应用内核参数
sysctl -p /etc/sysctl.d/99-sysctl.conf
# 4. 文件描述符限制
echo "正在优化文件描述符限制..."
cat > /etc/security/limits.d/99-nofile.conf <<EOF
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
EOF
# 5. 服务优化
echo "正在优化系统服务..."
systemctl disable postfix.service
systemctl stop postfix.service
# 6. SSH安全加固
echo "正在加固SSH配置..."
sed -i 's/^#Port 22/Port 2222/' /etc/ssh/sshd_config
sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/^#ClientAliveInterval 0/ClientAliveInterval 300/' /etc/ssh/sshd_config
sed -i 's/^#ClientAliveCountMax 3/ClientAliveCountMax 3/' /etc/ssh/sshd_config
sed -i 's/^#MaxAuthTries 6/MaxAuthTries 3/' /etc/ssh/sshd_config
sed -i 's/^#LoginGraceTime 2m/LoginGraceTime 1m/' /etc/ssh/sshd_config
echo "AllowUsers yourusername" >> /etc/ssh/sshd_config
systemctl restart sshd
# 7. 防火墙配置
echo "正在配置防火墙..."
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --permanent --remove-service=ssh
firewall-cmd --reload
# 8. SELinux配置
echo "正在配置SELinux..."
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
setenforce 0
# 9. 时区与时间同步
echo "正在配置时区和时间同步..."
timedatectl set-timezone Asia/Shanghai
yum install -y chrony
systemctl enable chronyd
systemctl start chronyd
chronyc sources
# 10. 禁用不必要的服务
echo "正在禁用不必要的服务..."
systemctl disable avahi-daemon.service
systemctl disable cups.service
systemctl disable bluetooth.service
# 11. 日志轮转优化
echo "正在优化日志轮转..."
sed -i 's/^#compress/compress/' /etc/logrotate.conf
sed -i 's/^weekly/daily/' /etc/logrotate.conf
# 12. 历史命令优化
echo "正在优化历史命令配置..."
cat >> /etc/profile <<EOF
# 历史命令优化
export HISTTIMEFORMAT="%F %T "
export HISTSIZE=10000
export HISTFILESIZE=10000
export HISTCONTROL=ignoredups
EOF
source /etc/profile
# 13. 自动清理临时文件
echo "正在配置自动清理..."
yum install -y tmpwatch
cat > /etc/cron.daily/tmpclean <<EOF
#!/bin/bash
tmpwatch 24h /tmp
tmpwatch 24h /var/tmp
EOF
chmod +x /etc/cron.daily/tmpclean
# 14. 系统监控配置
echo "正在配置系统监控..."
cat > /etc/cron.d/sysmon <<EOF
# 系统监控任务
*/5 * * * * root /usr/bin/sar -u -r -n DEV 1 5 > /var/log/sa/sar_\$(date +\%Y\%m\%d\%H\%M\%S).log
EOF
# 15. 安装Docker (可选)
read -p "是否安装Docker?(y/n): " install_docker
if [ "$install_docker" = "y" ]; then
echo "正在安装Docker..."
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl enable docker
systemctl start docker
fi
echo ""
echo "============================================"
echo "CentOS 7 系统优化完成!"
echo "请注意以下事项:"
echo "1. SSH端口已改为2222,请使用新端口连接"
echo "2. 已禁用root直接登录,请确保已创建普通用户"
echo "3. 建议重启系统使所有优化生效"
echo "============================================"
centos7_optimize.sh
chmod +x centos7_optimize.sh
./centos7_optimize.sh
根据实际需求,您可以调整脚本中的参数或注释掉不需要的部分。