Appearance
Adding swap will allow a Linux system to swap unused or lower priority stuff from RAM to disk. For more info read this guide from itsfoss.com.
Creating the swap file ​
- Creating the file (500 MiB)
bash
$ sudo fallocate -l 500M /swapfile- Applying the correct permissions
bash
$ sudo chmod 600 /swapfile- Formatting the file for use as a linux swap
bash
$ sudo mkswap /swapfileSetting it up ​
This is a two stage process where both parts are important if you want your swap to continue to work after a reboot. We will activate the swap as well as changing the system's swapiness.
The swappiness defines how much your system uses the swap. A very low swappiness lets your system fill up lot's of RAM and only swap if necessarry, whereas a high swappiness value makes the swapping very aggressive.
For servers I like to use a low swappiness value of 3
Temporary Activation ​
This will activate the swap but only for as long as the system stays online.
After a reboot the swap would go back to being disabled.
bash
$ sudo swapon /swapfile
$ sudo sysctl vm.swappiness=3Permanent Activation ​
First open your fstab. This file dictates how your system mounts things (including swap).
Feel free to replace nano with whatever text editor you like best. You can also just open the file at /etc/fstab with any graphical text editor if you like.
bash
$ sudo nano /etc/fstabThen just go to the end of the file and add the following line:/swapfile none swap sw 0 0
Then just do the same for /etc/sysctl.conf and add the line vm.swappiness=3 at the end of the file (or repalce the existing line if it already exists).
bash
$ sudo nano /etc/sysctl.conf