交换文件
As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).
注意: BTRFS 文件系统暂时不支持交换文件。
建立交换文件
As root use fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd can also be used but will take longer). For example, creating a 512 MB swap file:
# fallocate -l 512M /swapfile
或
# dd if=/dev/zero of=/swapfile bs=1M count=512
Set the right permissions (a world-readable swap file is a huge local vulnerability)
# chmod 600 /swapfile
After creating the correctly-sized file, format it to swap:
# mkswap /swapfile
Activate the swapfile:
# swapon /swapfile
Edit /etc/fstab and add an entry for the swap file:
/swapfile none swap defaults 0 0
删除交换文件
To remove a swap file, the current swap file must be turned off.
As root:
# swapoff -a
Remove swapfile:
# rm -rf /swapfile
Checks current swap space by running free command (It must be around 10GB.).
Checks the swap partition
sudo fdisk -l
/dev/hda8 none swap sw 0 0
Make swap space and enable it.
sudo swapoff -a
sudo /sbin/mkswap /dev/hda8
sudo swapon -a
If your swap disk size is not enough you would like to create swap file and use it. Create swap file.
sudo fallocate -l 10g /mnt/10GB.swap
sudo chmod 600 /mnt/10GB.swap
Mount swap file.
sudo mkswap /mnt/10GB.swap
Enable swap file.
sudo swapon /mnt/10GB.swap