logo

CentOS系统实战指南:从入门到进阶的学习教程

作者:demo2025.09.17 11:11浏览量:0

简介:本文为CentOS系统学习者提供系统性指导,涵盖基础操作、服务管理、安全加固及性能优化等核心模块,通过理论解析与实战案例帮助读者快速掌握企业级Linux运维技能。

一、CentOS系统基础与安装配置

1.1 CentOS版本选择与特性对比

CentOS作为RHEL的开源衍生版,分为CentOS Stream(滚动更新)和CentOS Linux(传统版本)。当前推荐使用CentOS Stream 9(基于RHEL 9源码),其优势在于持续获取最新功能更新,适合开发测试环境。传统企业环境仍可选择CentOS 7(最终支持至2024年6月),但需注意其已停止官方更新。

1.2 最小化安装与配置优化

通过anaconda安装界面选择”Minimal Install”可减少不必要的软件包。安装后需立即执行:

  1. # 更新系统并安装基础工具
  2. dnf update -y && dnf install -y epel-release vim wget curl net-tools
  3. # 配置网络(以静态IP为例)
  4. cat > /etc/sysconfig/network-scripts/ifcfg-ens192 <<EOF
  5. BOOTPROTO=static
  6. IPADDR=192.168.1.100
  7. NETMASK=255.255.255.0
  8. GATEWAY=192.168.1.1
  9. DNS1=8.8.8.8
  10. ONBOOT=yes
  11. EOF
  12. systemctl restart network

1.3 基础环境标准化

创建标准化用户环境:

  1. # 添加sudo权限用户
  2. useradd -m admin && passwd admin
  3. echo "admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/admin
  4. # 配置SSH安全
  5. sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
  6. sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
  7. systemctl restart sshd

二、核心服务管理实战

2.1 Web服务部署(Nginx+PHP)

  1. # 安装Nginx与PHP
  2. dnf install -y nginx php php-fpm
  3. # 配置PHP-FPM
  4. sed -i 's/^user = apache/user = nginx/' /etc/php-fpm.d/www.conf
  5. sed -i 's/^group = apache/group = nginx/' /etc/php-fpm.d/www.conf
  6. systemctl enable --now php-fpm nginx
  7. # 测试页面
  8. echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php

2.2 数据库集群搭建(MySQL 8.0)

  1. # 添加MySQL仓库并安装
  2. dnf install -y https://dev.mysql.com/get/mysql80-community-release-el9-3.noarch.rpm
  3. dnf install -y mysql-community-server
  4. # 主从复制配置
  5. # 主库配置
  6. cat >> /etc/my.cnf <<EOF
  7. [mysqld]
  8. server-id=1
  9. log-bin=mysql-bin
  10. binlog-format=ROW
  11. EOF
  12. systemctl start mysqld
  13. # 获取临时密码并修改
  14. grep 'temporary password' /var/log/mysqld.log
  15. mysql_secure_installation

2.3 自动化运维工具链

  • Ansible:通过dnf install -y ansible-core安装后,创建inventory文件:
    ```ini
    [webservers]
    192.168.1.101
    192.168.1.102

[webservers:vars]
ansible_user=admin
ansible_ssh_private_key_file=~/.ssh/id_rsa

  1. - **Prometheus监控**:使用Podman部署容器化监控:
  2. ```bash
  3. podman run -d --name prometheus -p 9090:9090 \
  4. -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
  5. prom/prometheus

三、安全加固深度实践

3.1 防火墙高级配置

  1. # 启用firewalld并配置规则
  2. systemctl enable --now firewalld
  3. firewall-cmd --permanent --add-service={http,https,ssh}
  4. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="3306" accept'
  5. firewall-cmd --reload

3.2 审计与日志管理

配置auditd监控关键文件:

  1. # 添加监控规则
  2. cat > /etc/audit/rules.d/sshd.rules <<EOF
  3. -w /etc/ssh/sshd_config -p wa -k sshd_config
  4. -w /etc/passwd -p wa -k passwd_changes
  5. EOF
  6. systemctl restart auditd
  7. # 日志轮转配置
  8. cat > /etc/logrotate.d/nginx <<EOF
  9. /var/log/nginx/*.log {
  10. daily
  11. missingok
  12. rotate 14
  13. compress
  14. delaycompress
  15. notifempty
  16. create 0640 nginx adm
  17. sharedscripts
  18. postrotate
  19. [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
  20. endscript
  21. }
  22. EOF

3.3 漏洞修复流程

建立月度更新机制:

  1. # 创建更新脚本
  2. cat > /usr/local/bin/update-system.sh <<'EOF'
  3. #!/bin/bash
  4. dnf update -y --security
  5. if dnf updateinfo list --security | grep -q "CVE"; then
  6. echo "Security updates applied" | mail -s "System Update Report" admin@example.com
  7. fi
  8. EOF
  9. chmod +x /usr/local/bin/update-system.sh
  10. echo "0 3 * * 1 /usr/local/bin/update-system.sh" | crontab -

四、性能调优方法论

4.1 内核参数优化

  1. # 配置sysctl参数
  2. cat > /etc/sysctl.d/99-network.conf <<EOF
  3. net.ipv4.tcp_fin_timeout = 30
  4. net.ipv4.tcp_tw_reuse = 1
  5. net.core.somaxconn = 4096
  6. vm.swappiness = 10
  7. EOF
  8. sysctl -p /etc/sysctl.d/99-network.conf

4.2 存储性能优化

  • XFS文件系统调优
    ```bash

    创建LVM卷并格式化

    pvcreate /dev/sdb
    vgcreate vg00 /dev/sdb
    lvcreate -l 100%FREE -n lv_data vg00
    mkfs.xfs -n ftype=1 /dev/vg00/lv_data

挂载时启用性能选项

mount -o noatime,nobarrier /dev/vg00/lv_data /data

  1. #### 4.3 监控告警体系
  2. 部署Node Exporter+Grafana监控:
  3. ```bash
  4. # 安装Node Exporter
  5. podman run -d --name node-exporter -p 9100:9100 \
  6. -v "/:/host:ro,rslave" \
  7. prom/node-exporter --path.rootfs=/host
  8. # Grafana配置(需单独安装)
  9. cat > /etc/grafana/provisioning/datasources/prometheus.yaml <<EOF
  10. apiVersion: 1
  11. datasources:
  12. - name: Prometheus
  13. type: prometheus
  14. url: http://localhost:9090
  15. access: proxy
  16. isDefault: true
  17. EOF

五、进阶技能拓展

5.1 容器化技术集成

  1. # 安装Podman并运行容器
  2. dnf install -y podman
  3. podman run -d --name web -p 8080:80 docker.io/library/nginx:alpine
  4. # 构建自定义镜像
  5. cat > Dockerfile <<EOF
  6. FROM alpine:latest
  7. RUN apk add --no-cache nginx
  8. COPY index.html /var/www/html/
  9. CMD ["nginx", "-g", "daemon off;"]
  10. EOF
  11. podman build -t my-nginx .

5.2 自动化编排(Kubernetes基础)

  1. # 安装Minikube
  2. curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  3. install minikube-linux-amd64 /usr/local/bin/minikube
  4. minikube start --driver=podman
  5. # 部署测试应用
  6. kubectl create deployment nginx --image=nginx:alpine
  7. kubectl expose deployment nginx --port=80 --type=NodePort

5.3 灾备方案设计

实施全量+增量备份策略:

  1. # 使用rsync进行增量备份
  2. cat > /usr/local/bin/backup.sh <<'EOF'
  3. #!/bin/bash
  4. TARGET_DIR="/backup/$(date +%Y%m%d)"
  5. mkdir -p "$TARGET_DIR"
  6. rsync -avz --delete --link-dest=/backup/latest /etc /home /var/www "$TARGET_DIR"
  7. ln -sfn "$TARGET_DIR" /backup/latest
  8. find /backup -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;
  9. EOF

六、学习资源推荐

  1. 官方文档:Red Hat官方文档(需注意CentOS Stream与RHEL的差异)
  2. 实践平台:使用Vagrant创建多节点测试环境:
    1. Vagrant.configure("2") do |config|
    2. config.vm.box = "generic/centos9s"
    3. config.vm.provider "virtualbox" do |vb|
    4. vb.memory = "2048"
    5. end
    6. config.vm.provision "shell", path: "bootstrap.sh"
    7. end
  3. 社区支持:CentOS官方论坛、Stack Overflow的Linux标签

本教程通过系统性知识框架与可复用的配置模板,帮助读者构建从基础运维到高级架构设计的完整能力体系。建议结合实际业务场景进行模块化学习,每个章节完成后通过systemctl statusjournalctl -xe等命令验证配置效果,逐步积累企业级Linux系统管理经验。

相关文章推荐

发表评论