Linux 系統運維實戰手冊
——從基礎運維到高可用架構的全棧實踐
一、系統初始化與基礎配置
1. 系統初始化腳本(基於CentOS 8)
# 基礎安全加固
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
systemctl stop firewalld && systemctl disable firewalld
yum install -y iptables-services && systemctl enable iptables
基礎環境配置
echo "export HISTTIMEFORMAT=\"%F %T \"" >> /etc/profile
echo "export TMOUT=600" >> /etc/profile
source /etc/profile
內核參數優化
cat << EOF >> /etc/sysctl.conf
net.core.somaxconn = 65535
vm.swappiness = 10
fs.file-max = 6553500
EOF
sysctl -p
核心功能:
SELinux策略調整
命令行歷史記錄增強
內核級TCP連接優化
2. 用户與權限管理
批量創建用户腳本:
bash
Copy Code
# users.csv 示例:
# username,uid,group
devops,2001,admin
webadmin,2002,www
while IFS=, read -r user uid group; do
groupadd -g $uid $group
useradd -u $uid -g $group -s /bin/bash $user
echo "$user:$(openssl rand -base64 12)" | chpasswd
done < users.csv
安全實踐:
使用隨機密碼初始化
UID/GID統一規劃
用户組權限隔離
二、日常運維操作指南
1. 日誌分析黃金命令集
場景 命令組合
實時監控Nginx訪問日誌 tail -f /var/log/nginx/access.log | awk '$9>400{print $1,$7,$9}'
查找大文件 find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null
統計TCP連接狀態 netstat -ant | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
2. 磁盤管理緊急處理
LVM擴容操作流程:
# 查看卷組空間
vgs
# 擴展物理卷
pvcreate /dev/sdb1
vgextend centos /dev/sdb1
# 擴展邏輯卷
lvextend -L +50G /dev/centos/root
xfs_growfs /dev/centos/root # XFS文件系統專用
三、安全加固與入侵檢測
1. SSH安全防護方案
bash
Copy Code
# /etc/ssh/sshd_config 關鍵配置
Port 57222 # 非標準端口
PermitRootLogin no # 禁止root登錄
MaxAuthTries 3 # 最大認證嘗試次數
ClientAliveInterval 300 # 會話超時設置
AllowUsers opsadmin@192.168.1.0/24 # IP白名單限制
生效命令: systemctl restart sshd
2. 入侵檢測系統(IDS)部署
基於Fail2ban的防護配置:
ini
Copy Code
# /etc/fail2ban/jail.d/nginx-cc.conf
[nginx-cc]
enabled = true
port = http,https
filter = nginx-cc
action = iptables-multiport[name=nginx-cc, port="http,https"]
logpath = /var/log/nginx/access.log
maxretry = 50
findtime = 600
bantime = 3600
監控指標:
實時封禁IP列表:fail2ban-client status nginx-cc
四、性能調優與故障排查
1. 系統性能分析工具棧
bash
Copy Code
# CPU瓶頸分析
mpstat -P ALL 1
# 內存泄漏檢測
vmstat 1 10
# IO負載觀測
iostat -xmt 1
# 綜合監控儀表盤
nmon
2. 內核級網絡優化
TCP協議棧調優參數:
bash
Copy Code
# /etc/sysctl.conf
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.core.netdev_max_backlog = 50000
五、自動化運維體系構建
1. Ansible核心場景示例
批量更新系統補丁:
yaml
Copy Code
- name: Security Patch Update
hosts: webservers
become: yes
tasks:
- name: Update security packages
yum:
name: '*'
security: yes
update_cache: yes
register: yum_result
- name: Reboot if kernel updated
reboot:
msg: "Kernel updated, rebooting..."
pre_reboot_delay: 30
when: "'kernel' in yum_result.changes"
2. 監控告警體系架構
text
Copy Code
┌──────────────┐
│ Prometheus │
│ (數據採集存儲) │
└──────┬───────┘
│
┌──────────────┴──────────────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Alertmanager │ │ Grafana │
│ (告警路由管理) │ │ (可視化儀表盤) │
└──────┬───────┘ └──────────────┘
│
▼
┌──────────────┐
│ 企業微信/釘釘 │
│ (告警通知渠道) │
└──────────────┘
六、災備與高可用方案
1. 數據庫主從同步配置
MySQL主從配置要點:
# 主庫配置
[mysqld]
server-id=1
log-bin=mysql-bin
binlog_format=row
# 從庫配置
[mysqld]
server-id=2
relay-log=mysql-relay-bin
read_only=1
同步狀態檢查命令: SHOW SLAVE STATUS\G
2. Keepalived高可用方案
bash
Copy Code
# keepalived.conf 主節點配置
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100/24
}
}
運維能力進階路線:
掌握Linux性能優化聖經:《Systems Performance: Enterprise and the Cloud》
熟練使用eBPF進行深度內核觀測(BCC工具集)
構建完整的CI/CD流水線(Jenkins + GitLab + Artifactory)
學習雲原生運維體系(Kubernetes + Istio + Prometheus)
通過本手冊的系統實踐,可幫助運維工程師構建從基礎維護到架構優化的完整知識體系。建議重點關注自動化運維與可觀測性體系的建設,這是現代雲原生運維的核心競爭力。