CentOS系统管理实战:从基础到企业级服务部署

一、CentOS系统安装与初始化配置

1.1 安装方式选择与镜像准备

CentOS作为企业级Linux发行版,支持多种安装方式:物理机裸金属安装、虚拟机环境部署以及PXE网络安装。对于生产环境,推荐使用ISO镜像文件进行本地安装,镜像版本需根据业务需求选择:

  • CentOS Stream:滚动更新版本,适合开发测试环境
  • CentOS Linux:稳定版本,企业生产环境首选

安装前需完成磁盘分区规划,典型生产环境分区方案如下:

  1. /boot 1GB ext4
  2. / 100GB xfs
  3. /var 50GB xfs
  4. /home 剩余空间 xfs
  5. swap 内存的1.5

1.2 基础环境初始化

安装完成后需执行关键初始化操作:

  1. 网络配置:通过nmcli命令配置静态IP

    1. nmcli con mod eth0 ipv4.addresses 192.168.1.100/24
    2. nmcli con mod eth0 ipv4.gateway 192.168.1.1
    3. nmcli con mod eth0 ipv4.dns "8.8.8.8"
    4. nmcli con up eth0
  2. 时区设置:使用timedatectl命令

    1. timedatectl set-timezone Asia/Shanghai
  3. 主机名配置

    1. hostnamectl set-hostname server01

二、系统核心管理技术

2.1 文件系统管理

CentOS默认使用XFS文件系统,关键管理命令包括:

  • 磁盘空间监控

    1. df -hT # 查看文件系统使用情况
    2. du -sh /var/log # 统计目录占用空间
  • 文件权限控制

    1. chmod 750 /app # 设置目录权限
    2. chown web:web /var/www # 修改所有者

2.2 用户与权限体系

企业环境需建立完善的用户管理体系:

  1. 用户创建

    1. useradd -G developers -s /bin/bash tom
  2. sudo权限配置

    1. echo "tom ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  3. SSH密钥认证

    1. mkdir -p /home/tom/.ssh
    2. chmod 700 /home/tom/.ssh
    3. echo "public_key_content" >> /home/tom/.ssh/authorized_keys

2.3 进程与服务管理

关键监控工具使用示例:

  • 系统监控

    1. top -p $(pgrep -d',' nginx) # 监控特定进程
    2. htop # 交互式监控
  • 服务管理

    1. systemctl enable nginx # 设置开机启动
    2. systemctl restart nginx # 重启服务
    3. journalctl -u nginx --no-pager # 查看服务日志

三、企业级服务部署方案

3.1 Web服务架构

典型LNMP环境部署流程:

  1. Nginx配置

    1. server {
    2. listen 80;
    3. server_name example.com;
    4. root /var/www/html;
    5. index index.php;
    6. location ~ \.php$ {
    7. fastcgi_pass unix:/run/php-fpm.sock;
    8. include fastcgi_params;
    9. }
    10. }
  2. PHP-FPM优化

    1. ; /etc/php-fpm.d/www.conf
    2. pm = dynamic
    3. pm.max_children = 50
    4. pm.start_servers = 5
    5. pm.min_spare_servers = 5
    6. pm.max_spare_servers = 35

3.2 数据库服务

MySQL 8.0企业级配置要点:

  1. 配置文件优化

    1. [mysqld]
    2. innodb_buffer_pool_size = 4G
    3. innodb_log_file_size = 512M
    4. max_connections = 500
  2. 用户权限管理

    1. CREATE USER 'appuser'@'192.168.1.%' IDENTIFIED BY 'SecurePass123!';
    2. GRANT SELECT,INSERT,UPDATE,DELETE ON appdb.* TO 'appuser'@'192.168.1.%';
    3. FLUSH PRIVILEGES;

3.3 文件共享服务

Samba配置企业文件服务器示例:

  1. [global]
  2. workgroup = WORKGROUP
  3. security = user
  4. map to guest = Bad User
  5. [shared]
  6. path = /data/shared
  7. browsable = yes
  8. writable = yes
  9. valid users = @developers
  10. force create mode = 0660
  11. force directory mode = 2770

四、安全加固最佳实践

4.1 防火墙配置

使用firewalld管理网络访问:

  1. firewall-cmd --permanent --add-service=http
  2. firewall-cmd --permanent --add-port=443/tcp
  3. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="22" protocol="tcp" accept'
  4. firewall-cmd --reload

4.2 审计与日志管理

关键日志配置方案:

  1. 日志轮转

    1. /var/log/nginx/*.log {
    2. daily
    3. missingok
    4. rotate 30
    5. compress
    6. delaycompress
    7. notifempty
    8. create 0640 www-data adm
    9. sharedscripts
    10. postrotate
    11. [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    12. endscript
    13. }
  2. 失败登录监控

    1. grep "Failed password" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr

4.3 定期维护任务

建议配置的cron任务示例:

  1. # 每周清理临时文件
  2. 0 3 * * 0 /usr/bin/find /tmp -type f -mtime +7 -delete
  3. # 每日更新病毒库
  4. 30 2 * * * /usr/bin/freshclam --quiet
  5. # 每小时检查磁盘空间
  6. 0 * * * * /usr/bin/df -h | /usr/bin/mail -s "Disk Report" admin@example.com

五、性能优化与故障排查

5.1 系统性能监控

关键监控指标与工具:

  • CPU负载uptime, mpstat 1 5
  • 内存使用free -m, vmstat 1 5
  • I/O性能iostat -x 1, iotop
  • 网络性能nload, iftop

5.2 常见故障处理

典型问题解决方案:

  1. 服务无法启动

    1. systemctl status nginx --no-pager
    2. journalctl -xe | grep nginx
    3. strace -f /usr/sbin/nginx 2>&1 | grep -i error
  2. 磁盘空间不足
    ```bash

    查找大文件

    find / -type f -size +1G -exec ls -lh {} \; 2>/dev/null

清理旧日志

find /var/log -type f -name “*.log” -mtime +30 -delete

  1. 3. **网络连接问题**:
  2. ```bash
  3. # 测试连通性
  4. traceroute example.com
  5. mtr --report example.com
  6. # 检查端口状态
  7. ss -tulnp | grep 80
  8. nc -zv example.com 443

本文通过系统化的知识梳理和实战案例解析,完整呈现了CentOS系统管理的技术体系。从基础环境搭建到企业级服务部署,再到安全加固与性能优化,每个环节都提供了可落地的解决方案。对于运维工程师而言,掌握这些核心技能不仅能够提升日常工作效率,更能为构建稳定可靠的企业IT基础设施奠定坚实基础。建议读者结合实际环境进行实践验证,逐步积累运维经验,最终达到自动化运维的高级水平。