Dorokhov.codes

Setting up swap

Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.

Checking swap

Check if there’s already swap enabled with swapon utility:

swapon -s
swapon --show

If the output is empty, it means that your system does not have swap space enabled.

Another variant to check swap:

free -h

Creating a swap space

Creating an empty file using dd utility (3 Gb):

sudo dd if=/dev/zero of=/swapfile bs=64M count=48

The bs parameter means a block size creating in RAM which will be stored to disk count times.

We should adjust the permissions on our swap file so that it isn’t readable by anyone besides the root account. Allowing other users to read or write to this file would be a huge security risk.

sudo chmod 600 /swapfile

Now that our swap file is more secure, we can tell our system to set up the swap space for use by typing:

sudo mkswap /swapfile

Our swap file is now ready to be used as a swap space. We can begin using it by typing:

sudo swapon /swapfile

Making the swap permanent

sudo vi /etc/fstab

At the bottom of the file, we need to add a line that will tell the operating system to use the swap file we created:

/swapfile   swap    swap    sw  0   0

Or just enter the following command:

sudo sh -c 'echo "/swapfile none swap sw 0 0" >> /etc/fstab'