在企业级 Linux 环境中,AlmaLinux 作为 RHEL 的完美替代品,继承了强大的稳定性和丰富的功能。然而,任何系统都可能遇到故障。本文将为您提供一份全面的故障排查指南,涵盖从基础命令到高级诊断技巧,重点解决启动、网络和存储等常见问题。
目录
系统启动故障排查
网络连接问题诊断
存储与文件系统故障
高级诊断技巧与工具
日志分析与监控
实战案例分析
系统启动故障排查
1. 理解 Linux 启动过程
Linux 系统启动分为四个主要阶段:
BIOS/UEFI 初始化
:硬件自检,加载引导设备
引导加载器
:GRUB2 加载内核和 initramfs
内核初始化
:硬件检测,驱动加载,systemd 启动
用户空间
:systemd 启动服务,进入多用户模式
2. 基础启动问题诊断
问题:系统卡在 GRUB 阶段
症状
:开机后显示 GRUB 命令行界面,无法进入系统
排查步骤
:
# 在 GRUB 命令行中,首先查找引导分区
ls
# 假设找到 (hd0,msdos1) 是 boot 分区
set root=(hd0,msdos1)
# 加载内核
linux /vmlinuz-5.14.0-70.13.1.el9_0.x86_64 root=/dev/mapper/rl-root ro
# 加载 initramfs
initrd /initramfs-5.14.0-70.13.1.el9_0.x86_64.img
# 启动
boot
永久修复
:
# 进入系统后重新安装 GRUB
grub2-install /dev/sda
# 重建 GRUB 配置
grub2-mkconfig -o /boot/grub2/grub.cfg
# 对于 UEFI 系统
grub2-mkconfig -o /boot/efi/EFI/almalinux/grub.cfg
问题:内核恐慌(Kernel Panic)
症状
:屏幕显示 “Kernel panic – not syncing” 错误
诊断方法
:
# 1. 检查内核日志(如果系统能短暂启动)
journalctl -k -b -1
# 2. 查看崩溃信息
dmesg | grep -i “panic\|oops\|bug”
# 3. 检查硬件问题
# 内存测试
memtest86+
# CPU 温度监控
sensors
常见原因和解决方案
:
内存故障
:运行
memtest86+
检测,替换故障内存条
驱动冲突
:在 GRUB 中添加
modprobe.blacklist=冲突模块名
文件系统损坏
:见存储故障部分
3. systemd 服务启动失败
症状
:系统能进入救援模式,但某些服务无法启动
诊断命令
:
# 查看失败的服务
systemctl –failed
# 查看特定服务的详细日志
journalctl -u sshd.service -b
# 查看服务启动时间
systemd-analyze blame
# 查看服务依赖关系
systemctl list-dependencies sshd.service
# 尝试手动启动服务并查看实时日志
journalctl -f -u nginx.service &
systemctl start nginx.service
实战案例:Nginx 服务启动失败
# 1. 检查配置文件语法
nginx -t
# 2. 查看详细错误
journalctl -u nginx.service -n 50 –no-pager
# 3. 常见错误示例:
# 错误:nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
# 解决方案:检查端口是否被占用或需要 root 权限
sudo ss -tlnp | grep :80
# 4. 如果是配置问题,使用 systemd 的临时覆盖
systemctl edit nginx.service
# 添加:
# [Service]
# ExecStartPre=/usr/bin/nginx -t
网络连接问题诊断
1. 基础网络状态检查
网络接口状态
# 查看所有网络接口
ip link show
# 查看 IP 地址分配
ip addr show
# 查看路由表
ip route show
# 查看网络接口统计
ip -s link show eth0
连通性测试
# 测试本地回环
ping -c 4 127.0.0.1
# 测试网关连通性
ping -c 4 192.168.1.1
# 测试 DNS 解析
nslookup example.com
dig example.com
# 测试到目标的完整路径
traceroute example.com
# 测试端口连通性
nc -zv example.com 443
telnet example.com 80
2. NetworkManager 问题排查
AlmaLinux 9 默认使用 NetworkManager,但传统 network.service 仍可用。
# 查看 NetworkManager 状态
systemctl status NetworkManager
# 查看连接列表
nmcli connection show
# 查看设备状态
nmcli device status
# 查看特定连接详情
nmcli connection show “System eth0”
# 重新加载配置
nmcli connection reload
# 激活连接
nmcli connection up “System eth0”
# 查看 NetworkManager 日志
journalctl -u NetworkManager -f
实战案例:有线连接无法获取 IP
# 1. 检查 DHCP 客户端是否运行
ps aux | grep dhclient
# 2. 手动请求 IP
nmcli device reapply eth0
# 3. 检查 DHCP 日志
journalctl -u NetworkManager -f | grep DHCP
# 4. 如果失败,检查网卡配置
cat /etc/NetworkManager/system-connections/eth0.nmconnection
# 5. 手动设置静态 IP(如果需要)
nmcli connection modify “System eth0” \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns “8.8.8.8,8.8.4.4” \
ipv4.method manual
nmcli connection up “System eth0”
3. DNS 解析问题
症状
:能 ping 通 IP 但无法访问域名
# 1. 检查 DNS 配置
cat /etc/resolv.conf
# 2. 检查 systemd-resolved 状态
systemctl status systemd-resolved
# 3. 测试 DNS 解析
dig @8.8.8.8 example.com
# 4. 检查 hosts 文件
cat /etc/hosts
# 5. 检查 DNS 缓存
systemd-resolve –statistics
# 6. 临时清除 DNS 缓存
sudo resolvectl flush-caches
4. 防火墙问题
# 查看防火墙状态
systemctl status firewalld
# 查看当前区域
firewall-cmd –get-active-zones
# 查看开放端口
firewall-cmd –list-ports
# 临时允许服务
firewall-cmd –add-service=http –permanent
firewall-cmd –reload
# 查看防火墙日志
journalctl -u firewalld -f
# 临时禁用防火墙测试(生产环境谨慎)
systemctl stop firewalld
# 测试后记得重新启用
systemctl start firewalld
5. 高级网络诊断工具
使用 ss 和 netstat
# 查看所有监听端口
ss -tlnp
# 查看所有 TCP 连接
ss -tan
# 查看进程名和端口
ss -tlnp | grep nginx
# 显示定时器信息
ss -to
使用 tcpdump
# 捕获 eth0 的所有流量
sudo tcpdump -i eth0 -w capture.pcap
# 只捕获 80 端口的流量
sudo tcpdump -i eth0 port 80
# 捕获特定主机的流量
sudo tcpdump -i eth0 host 192.168.1.100
# 实时显示 DNS 查询
sudo tcpdump -i eth0 port 53 -n
使用 nmap 进行端口扫描
# 扫描本机开放端口
nmap -sT -p- localhost
# 扫描网段
nmap -sP 192.168.1.0/24
# 检测远程主机端口
nmap -sV -p 1-1000 192.168.1.100
存储与文件系统故障
1. 磁盘空间问题
快速定位大文件
# 查看文件系统使用情况
df -h
# 查看 inode 使用情况
df -i
# 查找大于 100MB 的文件
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# 查找大目录
du -h /var | sort -rh | head -20
# 查找日志文件占用
journalctl –disk-usage
# 清理旧日志
journalctl –vacuum-time=7d
实战案例:/var 占满导致系统异常
# 1. 检查具体占用
du -sh /var/*
# 2. 发现 /var/log 占用过大
du -sh /var/log/*
# 3. 检查日志轮转配置
ls -la /etc/logrotate.d/
# 4. 手动清理(谨慎操作)
# 方法1:清空特定日志(如果不需要)
truncate -s 0 /var/log/messages
# 方法2:压缩旧日志
find /var/log -name “*.log” -mtime +7 -exec gzip {} \;
# 方法3:使用 logrotate 强制轮转
logrotate -f /etc/logrotate.d/syslog
2. 文件系统损坏
症状
:系统启动时提示 “Give root password for maintenance” 或 “File system check failed”
# 1. 进入救援模式后,卸载文件系统
umount /dev/mapper/rl-root
# 2. 运行 fsck(注意:必须卸载后运行)
fsck -y /dev/mapper/rl-root
# 3. 对于 XFS 文件系统
xfs_repair /dev/mapper/rl-root
# 4. 检查引导分区
fsck -y /dev/sda1
# 5. 重新挂载并检查
mount /dev/mapper/rl-root /mnt
xfs_repair -n /dev/mapper/rl-root # 只检查不修复
3. LVM 故障排查
AlmaLinux 默认使用 LVM,常见问题包括卷组丢失、逻辑卷无法激活等。
# 1. 检查卷组状态
vgdisplay
# 2. 检查逻辑卷状态
lvdisplay
# 3. 检查物理卷
pvdisplay
# 4. 如果卷组未激活
vgchange -ay
# 5. 扫描所有物理卷
pvscan
# 6. 重建卷组元数据(危险操作)
vgcfgrestore –list /dev/rl
vgcfgrestore /dev/rl
# 7. 激活特定逻辑卷
lvchange -ay /dev/rl/root
实战案例:LVM 物理卷丢失
# 场景:/dev/sdb1 被误删,导致卷组不完整
# 1. 检查当前状态
vgdisplay -v
# 输出显示 PV 数量减少
# 2. 尝试恢复
# 如果只是分区表丢失,可以重建分区
parted /dev/sdb
# (parted) print
# (parted) mkpart primary 1MiB 100%
# 3. 重新创建物理卷
pvcreate /dev/sdb1 –restorefile /etc/lvm/backup/rl –uuid <原UUID>
# 4. 恢复卷组
vgcfgrestore rl
# 5. 激活
vgchange -ay rl
4. RAID 故障(如果使用)
# 检查 RAID 状态
cat /proc/mdstat
# 查看详细信息
mdadm –detail /dev/md0
# 模拟故障(测试用)
mdadm –manage /dev/md0 –set-faulty /dev/sdb1
# 移除故障磁盘
mdadm –manage /dev/md0 –remove /dev/sdb1
# 添加新磁盘
mdadm –manage /dev/md0 –add /dev/sdc1
# 监控 RAID 状态
mdadm –monitor –scan –daemonise
5. NFS 存储问题
# 1. 检查 NFS 服务状态
systemctl status nfs-server
# 2. 检查导出目录
showmount -e localhost
# 3. 检查挂载点
df -h | grep nfs
# 4. 查看 NFS 挂载选项
cat /proc/mounts | grep nfs
# 5. 检查 NFS 连接
ss -tan | grep 2049
# 6. 调试挂载(添加 verbose 选项)
mount -t nfs -o vvv server:/export /mnt/nfs
# 7. 检查 NFS 统计信息
nfsstat -c
高级诊断技巧与工具
1. 系统性能分析
使用 systemd-analyze
# 查看启动时间详情
systemd-analyze
# 查看各服务启动时间
systemd-analyze blame
# 查看关键链
systemd-analyze critical-chain
# 绘制启动图表
systemd-analyze plot > boot.svg
使用 sar(系统活动报告)
# 安装 sysstat
sudo dnf install sysstat
# 启用服务
systemctl enable sysstat
# 查看 CPU 使用率
sar -u 1 5
# 查看内存使用
sar -r 1 5
# 查看 I/O 统计
sar -b 1 5
# 查看历史数据
sar -f /var/log/sa/sa15 # 查看15号的数据
使用 iostat
# 安装 sysstat
sudo dnf install sysstat
# 查看 I/O 统计(每秒刷新)
iostat -xz 1
# 查看设备详细信息
iostat -d -x /dev/sda 1
2. 进程和资源监控
使用 top/htop
# 安装 htop
sudo dnf install htop
# 运行 htop
htop
# 在 top 中:
# Shift + P:按 CPU 使用率排序
# Shift + M:按内存使用率排序
# Shift + K:杀死进程
# f:添加/移除显示字段
使用 ps 和 pgrep
# 查找特定进程
ps aux | grep nginx
# 查找进程 ID
pgrep nginx
# 查看进程树
pstree -p
# 查看进程打开的文件
lsof -p
# 查看端口对应的进程
lsof -i :80
使用 kill 和 pkill
# 正常终止进程
kill -15
# 强制终止
kill -9
# 按名称终止
pkill nginx
# 终止所有同名进程
killall nginx
3. 内核和模块诊断
检查内核模块
# 查看已加载模块
lsmod
# 查看模块信息
modinfo e1000e
# 加载模块
modprobe e1000e
# 卸载模块
modprobe -r e1000e
# 查看模块依赖
modprobe –show-depends e1000e
内核参数调整
# 查看当前参数
sysctl -a | grep net.ipv4.ip_forward
# 临时修改
sysctl -w net.ipv4.ip_forward=1
# 永久修改
echo ‘net.ipv4.ip_forward = 1’ >> /etc/sysctl.conf
sysctl -p
# 查看网络相关参数
sysctl -a | grep net.core
4. 使用 strace 和 ltrace
strace:跟踪系统调用
# 跟踪进程的系统调用
strace -p
# 跟踪新进程
strace ls
# 只跟踪特定系统调用
strace -e trace=open,read ls
# 跟踪并输出到文件
strace -o trace.log -p
# 统计系统调用时间
strace -c -p
ltrace:跟踪库函数调用
# 跟踪动态库调用
ltrace ls
# 跟踪特定库
ltrace -l /lib64/libc.so.6 ls
5. 使用 perf 进行性能分析
# 安装 perf
sudo dnf install perf
# 记录 CPU 性能数据
perf record -g -p
# 生成报告
perf report
# 统计系统调用
perf stat -e syscalls:sys_enter_* ls
# 监控 CPU 周期
perf stat -e cycles ls
6. 使用 eBPF 工具
AlmaLinux 9 支持 eBPF,可以使用 modern 工具进行诊断。
# 安装 bpftrace
sudo dnf install bpftrace
# 跟踪 open 系统调用
bpftrace -e ‘tracepoint:syscalls:sys_enter_open { printf(“%s: %s\n”, comm, str(args->filename)); }’
# 监控文件读写
bpftrace -e ‘tracepoint:syscalls:sys_exit_read /pid == 1234/ { printf(“read %d bytes\n”, args->ret); }’
日志分析与监控
1. systemd journal 分析
# 查看本次启动日志
journalctl -b
# 查看上次启动日志
journalctl -b -1
# 查看特定服务日志
journalctl -u sshd.service
# 实时跟踪日志
journalctl -f
# 查看指定时间范围
journalctl –since “2024-01-15 09:00:00” –until “2024-01-15 10:00:00”
# 按优先级过滤
journalctl -p err
# 查看占用磁盘空间
journalctl –disk-usage
# 清理旧日志
journalctl –vacuum-time=7d
journalctl –vacuum-size=500M
2. 传统日志文件
# 查看系统日志
tail -f /var/log/messages
# 查看安全日志
tail -f /var/log/secure
# 查看启动日志
cat /var/log/boot.log
# 查看邮件日志
tail -f /var/log/maillog
# 使用 logrotate 管理日志
logrotate -d /etc/logrotate.conf # 调试模式
logrotate -f /etc/logrotate.conf # 强制轮转
3. 集中式日志管理
使用 rsyslog
# 配置 rsyslog 转发
cat >> /etc/rsyslog.conf <
# 检查内存
free | awk ‘NR==2{printf “Memory Usage: %.1f%%\n”, $3/$2*100}’
# 检查负载
uptime | awk -F’load average:’ ‘{print $2}’ | awk ‘{print $1}’
# 检查服务状态
systemctl is-active nginx || echo “Nginx is down!”
# 添加到 crontab 每 5 分钟执行
# */5 * * * * /usr/local/bin/system-monitor.sh >> /var/log/monitor.log 2>&1
通过本指南,您应该能够系统地排查 AlmaLinux 的各种故障。记住,故障排查是一个经验积累的过程,多实践、多总结,您将成为 Linux 系统管理的专家。