创建空间
$ swapon -s
$ dd if=/dev/zero of=/mnt/swap bs=1024 count=10240
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 0.0270904 s, 387 MB/s
$ mkswap /mnt/swap
Setting up swapspace version 1, size = 10236 KiB
no label, UUID=3ffe08b8-1681-47ae-992c-76108d73a5a2
$ free -h
total used free shared buff/cache available
Mem: 7.6G 3.4G 741M 828K 3.6G 4.0G
Swap: 0B 0B 0B
$ swapon /mnt/swap
swapon: /mnt/swap: insecure permissions 0644, 0600 suggested.
$ chmod 600 /mnt/swap
设置重启之后还会生效
$ vi /etc/fstab
#添加如下行
/mnt/swap swap swap defaults 0 0
#重启系统
$ reboot
扩展空间
$ dd if=/dev/zero of=/mnt/swap bs=1024 count=1024000
1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 9.38812 s, 112 MB/s
$ swapon /mnt/swap
#扩展之后,直接执行报错,需要再添加为swap文件=>mkswap
swapon: /mnt/swap: read swap header failed: Invalid argument
$ mkswap /mnt/swap
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=18139b2d-ce48-41f4-877e-26ce1fe25f35
$ swapon /mnt/swap
$ swapon -s
Filename Type Size Used Priority
/mnt/swap file 1023996 0 -2
$ free -h
total used free shared buff/cache available
Mem: 7.6G 3.3G 2.5G 620K 1.8G 4.1G
Swap: 999M 0B 999M
关闭交换
$ swapoff /mnt/swap
$ free -h
total used free shared buff/cache available
Mem: 7.6G 3.2G 3.6G 620K 855M 4.2G
Swap: 0B 0B 0B