一、检查本地环境
1.检查系统版本
# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
2.检查系统内核版本
# uname -r
5.9.0-rc7-aml-s812
二、检查当前swap分区情况
1.检查swap分区
# free -m
total used free shared buff/cache available
Mem: 989 622 65 57 300 289
Swap: 0 0 0
2.查看磁盘分区情况
# df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 458M 0 458M 0% /dev
tmpfs tmpfs 99M 12M 88M 12% /run
/dev/mmcblk0p2 ext4 6.5G 1.9G 4.6G 30% /
tmpfs tmpfs 495M 0 495M 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 495M 0 495M 0% /sys/fs/cgroup
tmpfs tmpfs 495M 0 495M 0% /tmp
/dev/mmcblk0p1 vfat 488M 51M 437M 11% /boot
armbian-ramlog tmpfs 50M 30M 21M 60% /var/log
/dev/sda1 ext4 458G 132G 303G 31% /data/disk01
tmpfs tmpfs 99M 0 99M 0% /run/user/0
overlay overlay 458G 132G 303G 31% /data/disk01/docker/overlay2/a6012c01a075e6ad71d286849733a0fcec62ee6e6ff4d46d3bd6891cd9d8ca57/merged
overlay overlay 458G 132G 303G 31% /data/disk01/docker/overlay2/6dcb0532e2a77c47eeccc7886ab2091afa3b3ce02ecc5043ea39285243b6b68a/merged
三、配置交换分区
1.创建swap目录
mkdir -p /data/disk01/swap/
2.创建swap文件
root@onecloud:/data/disk01# dd if=/dev/zero of=/data/disk01/swap/myswap01 bs=2M count=1024
1024+0 records in
1024+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 66.0062 s, 32.5 MB/s
3.将生成的文件转换为Swap文件
root@onecloud:/data/disk01/swap# mkswap -f myswap01
mkswap: myswap01: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=bb9a5e22-888d-4f6f-96af-a7933f46561d
4.激活Swap文件
root@onecloud:/data/disk01/swap# swapon myswap01
swapon: /data/disk01/swap/myswap01: insecure permissions 0644, 0600 suggested.
5.查看swap大小
root@onecloud:/data/disk01/swap# free -m
total used free shared buff/cache available
Mem: 989 617 123 55 247 281
Swap: 2047 0 2047
四、配置永久swap
1.修改fastab文件
在/etc/fstab文件永久设置swap挂载
root@onecloud:/data/disk01/swap# grep swap /etc/fstab
/data/disk01/swap/myswap01 swap swap defaults,nofail 0 0
2.检查swap分区
root@onecloud:/data/disk01/swap# swapon -a
root@onecloud:/data/disk01/swap# swapon -s
Filename Type Size Used Priority
/data/disk01/swap/myswap01 file 2097148 0 -2
3.查看交换分区
/data/disk01/swap/myswap01 file 2097148 0 -2
root@onecloud:/data/disk01/swap# free -h
total used free shared buff/cache available
Mem: 989Mi 621Mi 70Mi 55Mi 296Mi 276Mi
Swap: 2.0Gi 0B 2.0Gi
4.关闭交换分区
swapoff -a
五、重启服务器测试
1.重启服务器
root
2.重新检查swap交换分区
# free -h
total used free shared buff/cache available
Mem: 989Mi 244Mi 224Mi 39Mi 520Mi 678Mi
Swap: 2.0Gi 0B 2.0Gi