Wednesday, January 4, 2017

How to add swap file in Debian or Ubuntu

Swap is used to allow operating system run in fail-over mode, meaning that if it's running out of RAM, it will start swapping some memory pages from RAM into the swap file, on disk. The best way to create swap - is to mount it into partition. But if you don't have a swap partition, or don't have time to create it, you can also add swap using a regular file. Below I will show how to add swap file in Debian based Linux distros.

1. Check your exisiting swap by running command:
sudo swapon -s
Empty output means there is no swap installed.

2. Allocate an empty file for the swap, for example 2 GB:
sudo fallocate -l 2G /swapfile

3. Change permissions, so that only root can access it. That's is done by security reasons.
sudo chmod 600 /swapfile

4. Format file as swap space:
sudo mkswap /swapfile

5. Turn on the swap file:
sudo swapon /swapfile

Make swapfile permanent


6. For your swapfile to be mounted on every machine boot, add the following line to /etc/fstab:
/swapfile none swap sw 0 0

7. Check /etc/fstab by running the following command:
mount -a
Empty output means that fstab file is valid.

Now you can reboot your machine and your swap file should be automatically mounted.


No comments:

Post a Comment