在 CentOS(以及大多数 Linux 发行版)上排查 swap 故障,可以按下面思路一步步来。
free -h
或
swapon --show
swap 行大小为 0,说明 swap 未启用dmesg | grep -i swap
journalctl -xe | grep -i swap
常见错误:
swapon failedread-only filesystemdevice or resource busy/etc/fstabcat /etc/fstab
正常应有类似:
/dev/sda2 swap swap defaults 0 0
或
UUID=xxxx swap swap defaults 0 0
手动挂载:
swapon -a
如果报错,记录错误信息继续查。
ls -lh /swapfile
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
并加入 /etc/fstab:
/swapfile swap swap defaults 0 0
lsblk
fdisk -l
mount | grep " / "
如果是 ro,需修复:
fsck -y /dev/sdX
reboot
top
htop
dmesg | grep -i oom
解决方式:
检查:
cat /proc/sys/vm/swappiness
临时修改:
sysctl vm.swappiness=60
永久:
echo "vm.swappiness=60" >> /etc/sysctl.conf
sysctl -p
free -hswapon --showjournalctl -xe | grep swapcat /etc/fstablsblkdmesg | grep -i swapcat /etc/redhat-release)swapon --show 输出我可以帮你精准定位是哪一类 swap 故障。