CentOS 7 定制手册:打造高效稳定的服务器环境
CentOS 7 作为企业级 Linux 发行版的代表,以其稳定性和安全性深受运维人员青睐,默认安装往往无法完全匹配实际业务需求,本手册旨在提供系统化的定制方案,助您构建更安全、高效且贴合业务的服务器环境。
初始配置:奠定坚实基础
最小化安装原则
安装时务必选择"Minimal Install",仅保留核心组件,冗余软件包不仅占用资源,还可能引入潜在漏洞,通过命令查看已安装包:
yum list installed
网络与主机名优化
- 使用
nmtui配置静态 IP,避免 DHCP 波动影响服务 - 修改
/etc/hostname设置清晰主机名(如 web-prod-01) - 更新
/etc/hosts确保本地解析优先级
- 使用
系统更新与基础工具
yum update -y && yum install -y epel-release yum install -y vim wget curl net-tools lsof bash-completion
系统调优:释放硬件潜力
内核参数调整
编辑/etc/sysctl.conf,根据服务器角色优化:# 提升 TCP 连接性能 net.ipv4.tcp_tw_reuse = 1 net.core.somaxconn = 65535 # 减少 Swap 使用倾向 vm.swappiness = 10
执行
sysctl -p立即生效资源限制管理
修改/etc/security/limits.conf,防止资源耗尽:* soft nofile 65536 * hard nofile 131072 apache soft nproc 2048 # 按服务定制
使用 Tuned 适配场景

yum install tuned -y systemctl enable --now tuned tuned-adm profile throughput-performance # 数据库/计算节点 tuned-adm profile latency-performance # 高响应应用
安全加固:构建防御体系
防火墙策略精细化
firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --reload # 拒绝所有流量后逐步放行 firewall-cmd --set-default-zone=drop
SSH 安全升级
修改/etc/ssh/sshd_config:PermitRootLogin no PasswordAuthentication no AllowUsers deploy@192.168.1.0/24 # IP白名单 Port 62222 # 更换默认端口
重启服务前需测试配置:
sshd -t自动化漏洞管理
yum install yum-plugin-security -y yum updateinfo list sec # 查看安全更新 crontab -e # 添加定时任务: 0 3 * * * /usr/bin/yum update --security -y
服务定制:按需精简与优化
禁用非必要服务
systemctl mask avahi-daemon cups bluetooth systemctl list-unit-files | grep enabled # 审查自启项
日志轮转策略
编辑/etc/logrotate.conf防止日志膨胀:
rotate 7 daily compress missingok size 100M # 按业务调整阈值
时间同步精准化
yum install chrony -y sed -i 's/^pool /# pool /g' /etc/chrony.conf echo "server ntp.aliyun.com iburst" >> /etc/chrony.conf systemctl restart chronyd chronyc sources -v # 验证源状态
开发环境适配
选择软件源策略
- 基础包:官方 Base + EPEL
- 新版软件:酌情添加 SCL(Software Collections)
- 开发工具:
yum groupinstall "Development Tools"
Python 3 生态部署
yum install centos-release-scl -y yum install rh-python38 -y scl enable rh-python38 bash # 临时启用 echo "source /opt/rh/rh-python38/enable" >> /etc/profile.d/python38.sh
容器化支持
yum install -y yum-utils device-mapper-persistent-data yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum install docker-ce -y systemctl enable docker usermod -aG docker deploy_user # 授权非root用户
监控与应急
基础监控部署
yum install sysstat -y sed -i 's/^HISTORY=.*/HISTORY=30/' /etc/sysconfig/sysstat # 延长数据保留 systemctl enable --now sysstat
关键工具:
iostat -x 2监控磁盘 IOpidstat -dru 5进程级资源追踪
内核崩溃捕获
yum install kexec-tools crash -y echo "path /var/crash" > /etc/kdump.conf systemctl enable kdump # 测试:echo c > /proc/sysrq-trigger
快照与回滚方案
使用 LVM 分区时:lvcreate -L 10G -s -n root_snap /dev/centos/root # 故障后还原: umount /dev/centos/root lvconvert --merge /dev/centos/root_snap
定制化是持续过程而非一次性任务。 每次调整后务必记录变更原因与时间,通过 A/B 测试验证效果,定期执行 rpm -Va 检查文件完整性,结合 auditd 监控关键目录,技术决策应基于实际监控数据而非经验推测,这才是专业运维的核心准则。
