Creating a swap file

Step-by-step guide to creating a swap file

Swap file is necessary for storing information in cases when the amount of RAM is insufficient. Such memory works much slower than  RAM, therefore active use of such memory is highly undesirable. 

Initially, you should create a file that will be used as swap. In this case, we create a 2 GB file.

fallocate --length 2GiB /swapfile

Set correct file permissions

chmod 600 /swapfile

We create a swap from the specified file, at the output we get the following.

# mkswap /swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=97f81d12-80ca-4a61-ad17-1c094f52e36d

Next, enable the use of swap in this file.

# swapon /swapfile

You can check if swap is working with the free command

# free -h
total used free shared buff/cache available
Mem: 3,8G 96M 3,5G 8,5M 268M 3,5G
Swap: 2,0G 0B 2,0G

As you can see in the screenshot, swap is enabled.

To mount this file as a swap file at system startup, you must make the following entry in the / etc / fstab file

/swapfile swap swap defaults 0 0

Instead of the path to the file, you can specify its UUID, obtained as a result of the mkswap command. In this case, the entry will look like this:

UUID=97f81d12-80ca-4a61-ad17-1c094f52e36d swap swap defaults 0 0