logo

CentOS系统从入门到精通:完整学习指南

作者:JC2025.09.12 11:11浏览量:0

简介:本文提供CentOS系统从基础环境搭建到高级运维管理的完整学习路径,涵盖系统安装、包管理、服务配置、安全加固等核心模块,通过理论解析与实战案例帮助读者快速掌握企业级Linux运维技能。

一、CentOS系统基础认知

1.1 CentOS版本演进

CentOS作为RHEL的开源克隆版本,经历了从CentOS 6到CentOS 8的重大变革。当前推荐使用CentOS Stream版本(基于RHEL持续交付模型),其更新周期缩短至6个月,更适合开发测试环境。例如CentOS Stream 9采用Fedora 36内核基础,提供更前沿的硬件支持。

1.2 系统安装要点

安装介质准备建议使用官方Minimal ISO(约1.2GB),通过dd if=CentOS-Stream-9-x86_64-minimal.iso of=/dev/sdX bs=4M命令制作启动盘。分区方案推荐:

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

安装时需特别注意网络配置,建议使用nmcli命令配置静态IP:

  1. nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24
  2. nmcli connection modify eth0 ipv4.gateway 192.168.1.1
  3. nmcli connection modify eth0 ipv4.dns "8.8.8.8,8.8.4.4"
  4. nmcli connection up eth0

二、核心管理技能

2.1 包管理深度实践

DNF包管理器相比YUM性能提升3倍,关键命令示例:

  1. # 安装开发工具组
  2. dnf groupinstall "Development Tools" -y
  3. # 查询软件包依赖
  4. dnf repoquery --requires httpd
  5. # 创建本地仓库
  6. createrepo /var/www/html/localrepo
  7. echo "[local]
  8. name=Local Repo
  9. baseurl=http://localhost/localrepo
  10. enabled=1" > /etc/yum.repos.d/local.repo

2.2 服务管理进阶

Systemd服务单元配置示例(nginx.service):

  1. [Unit]
  2. Description=The nginx HTTP and reverse proxy server
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/run/nginx.pid
  7. ExecStartPre=/usr/sbin/nginx -t
  8. ExecStart=/usr/sbin/nginx
  9. ExecReload=/usr/sbin/nginx -s reload
  10. [Install]
  11. WantedBy=multi-user.target

通过systemctl cat nginx可查看完整单元文件,使用journalctl -u nginx -f实时跟踪日志

三、系统优化与安全

3.1 性能调优策略

内核参数优化建议(/etc/sysctl.conf):

  1. net.ipv4.tcp_max_syn_backlog = 8192
  2. net.core.somaxconn = 8192
  3. vm.swappiness = 10

应用后执行sysctl -p生效。文件系统调优方面,XFS文件系统建议设置inode64选项:

  1. mkfs.xfs -m crc=1,finobt=1 /dev/sdb1

3.2 安全加固方案

SSH安全配置要点:

  1. PermitRootLogin no
  2. MaxAuthTries 3
  3. ClientAliveInterval 300
  4. Ciphers aes256-ctr,aes192-ctr,aes128-ctr

使用fail2ban防止暴力破解,配置示例:

  1. [sshd]
  2. enabled = true
  3. port = ssh
  4. filter = sshd
  5. logpath = /var/log/secure
  6. maxretry = 3
  7. bantime = 86400

四、企业级应用部署

4.1 Web服务架构

Nginx+PHP-FPM配置优化:

  1. worker_processes auto;
  2. worker_rlimit_nofile 65535;
  3. events {
  4. worker_connections 4096;
  5. multi_accept on;
  6. }
  7. http {
  8. fastcgi_buffer_size 128k;
  9. fastcgi_buffers 4 256k;
  10. fastcgi_busy_buffers_size 256k;
  11. }

PHP-FPM配置调整:

  1. pm = dynamic
  2. pm.max_children = 50
  3. pm.start_servers = 5
  4. pm.min_spare_servers = 5
  5. pm.max_spare_servers = 10

4.2 数据库集群

MariaDB Galera集群配置要点:

  1. [galera]
  2. wsrep_on=ON
  3. wsrep_provider=/usr/lib64/galera/libgalera_smm.so
  4. wsrep_cluster_name="my_cluster"
  5. wsrep_cluster_address="gcomm://192.168.1.101,192.168.1.102,192.168.1.103"
  6. wsrep_node_name="node1"
  7. wsrep_node_address="192.168.1.101"

集群初始化命令:

  1. systemctl stop mariadb
  2. galera_new_cluster # 在首个节点执行
  3. systemctl start mariadb # 其他节点执行

五、自动化运维实践

5.1 Ansible自动化

Playbook示例(部署WordPress):

  1. - hosts: webservers
  2. tasks:
  3. - name: Install packages
  4. dnf:
  5. name:
  6. - httpd
  7. - php
  8. - mariadb-server
  9. - php-mysqlnd
  10. state: present
  11. - name: Download WordPress
  12. get_url:
  13. url: https://wordpress.org/latest.tar.gz
  14. dest: /tmp/wordpress.tar.gz
  15. - name: Extract WordPress
  16. unarchive:
  17. src: /tmp/wordpress.tar.gz
  18. dest: /var/www/html
  19. remote_src: yes

5.2 日志集中管理

ELK Stack部署要点:

  1. Filebeat配置(/etc/filebeat/filebeat.yml):
    ```yaml
    filebeat.inputs:
  • type: log
    paths:
    • /var/log/nginx/*.log
      output.logstash:
      hosts: [“logstash:5044”]
      ```
  1. Logstash过滤配置:
    1. filter {
    2. grok {
    3. match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:url}" }
    4. }
    5. }

六、故障排查方法论

6.1 诊断流程

  1. 收集信息:dmesg | tail -20journalctl -xb
  2. 网络诊断:ss -tulnptcpdump -i eth0 port 80
  3. 性能分析:top -Hpidstat -t 1
  4. 磁盘检查:xfs_repair /dev/sda1lvs --segments

6.2 典型案例

案例1:服务启动失败

  1. # 检查服务状态
  2. systemctl status httpd --no-pager
  3. # 查看详细日志
  4. journalctl -u httpd -n 50 --no-pager
  5. # 检查依赖服务
  6. systemctl list-dependencies httpd

案例2:磁盘空间不足

  1. # 查找大文件
  2. find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
  3. # 清理旧日志
  4. logrotate -f /etc/logrotate.conf
  5. # 扩展LVM卷
  6. lvextend -L +10G /dev/mapper/centos-root
  7. xfs_growfs /dev/mapper/centos-root

本教程系统梳理了CentOS从基础环境搭建到高级运维管理的完整知识体系,通过200余个实战命令和30个配置案例,帮助读者构建企业级Linux运维能力。建议初学者按照”安装配置→基础管理→服务部署→自动化运维”的路径逐步深入,同时结合官方文档https://docs.centos.org/)进行扩展学习。

相关文章推荐

发表评论