How to Set up a Swap Space Using a File¶
This tutorial is suitable for any Linux distribution.
This procedure adds 6GB of swap to your server. If you want to add a different amount, replace
6G with the size you require.
Keep in mind that
swap size should be set to either
total_mem/3 or 16GB - lower of the two.
total_mem is the total size of the nodes memory.
If the node
swapsize should be set to 6GB.
If the node
swapsize should be set to 16GB.
Create a file that will be used for swap.
sudo fallocate -l 6G /swapfile
Change the permissions setting on the file so that only the root user is be able to write and read the swap file.
sudo chmod 600 /swapfile
Run the Use mkswap utility to set up the file as Linux swap area.
sudo mkswap /swapfile
Enable the swap.
sudo swapon /swapfile
To make the change sustainable, open the
/etc/fstabfile and append the following:
/swapfile swap swap defaults 0 0
Verify that swap is active.
sudo swapon --show NAME TYPE SIZE USED PRIO /swapfile file 6024M 507.4M -1
Remove a Swap File¶
sudo swapoff -v /swapfile
Remove the sap file entry by editing the
/etc/fstabfile and removing
/swapfile swap swap defaults 0 0.
Delete the swap file.
sudo rm /swapfile