The Linux swap file

Today we will be talking about the swap file in Linux. Will tell you what it is, how to create, resize or delete. In General, all you may need when working with swap.

Linux, like other OS, the swap file needed for insurance of RAM. When the installed RAM ends, it uses the selected area from the paging file. This may be necessary in two cases:

  • To full employment and, as a consequence, the lack of free RAM, the computer is not hampered.
  • To prioritize (important processes is given physical memory and secondary paging file Linux).

By itself, a swap is a separate logical partition of your disk or just a file on the system partition. Most often it is created when you install the operating system. But to create a swap linux, and after installing the operating system.

In recent versions of Ubuntu Linux swap file is created automatically regardless of whether you want it or not. While its size is about 1 GB.

How to see the swap in Linux

Before you begin the process of adding, configuring or deleting the swap file, you need to check for it. The easiest way is to use the following command. In our case it looks like this:


Used command displays the size of the paging file of your operating system. However, according to its results we can’t tell if it’s a swap file or swap partition. Therefore, we will use an additional another command to view the linux swap. Enter in terminal the following:

swapon --show

As you can see, in our case, the swap partition is allocated 1.4 GB and it’s file. That is, it is not on a separate partition, but on the same drive where operating system is installed.

If it is a separate logical volume, then under “TYPE” we will see the word “partition”.

If the page file on your operating system were absent, the output of the command free-h would look like this:

That is, instead of any data about the paging file you will see zeros.

Creating Linux swap file

If you have verified that the pagefile is missing, you can move on to its creation. The example below will be shown using the operating system Ubuntu 18.04, but it should work on other Linux distributions.

Step 1: Create swap file

Let’s create the swap file for our Ubuntu. For example, its size will be 1 GB. Launch the terminal and enter the command given below:

sudo fallocate -l 1G /swapfile

To ensure the security of the file, be sure to set him right rights.

sudo chmod 600 /swapfile

Thus, instead of a “swapfile” we can specify any other name.

Step 2: Create file system

Create swap on linux is not yet complete. You now need to specify our operating system that the created object should be used as a swap file. For this we will use the following command:

sudo mkswap /swapfile

If done correctly, the result will look like this:

Step 3: Activate the swap file

Well, now our Linux understands that this is a paging file, but this is not enough. We also need to enable SWAP:

sudo swapon /swapfile

Let’s see if we have done everything correctly:

swapon --show

Well, the operating system recognizes the file swap and start to use it. We see type in the field TYPE and size in the field SIZE.

Step 4. Save changes after reboot

All the operations that we have undertaken above, temporary. As soon as the computer restarts, they will be canceled. To make changes permanent, we need to add some information in /etc/fstab. To do this, proceed as follows:

Before you start to work with the file we’re talking about, be sure to back it up:

sudo cp /etc/fstab /etc/fstab.back

After that you can go directly to adding the data to us. We need to add this line:

/swapfile none swap sw 0 0

This is implemented via a text editor running as root, or inserted in the terminal command:

echo '/swapfile none swap sw 0 0' | sudo tee-a /etc/fstab

Ready. The changes are saved and the page file is not reset after you restart the operating system.

Setting swappiness

Next we will see configure swap on linux. The paging file there are options that inform the OS how often it should be used. This phenomenon is called “swapping” and can have a value in the range 0 – 100. If this value is closer to 100, the kernel will move to swap more information to free the memory. At values close to zero, the system will use swap only when absolutely necessary.

For example, in Ubuntu Linux for your desktop set to 60, and in the server editions of operating systems – 1. To check what parameter is used in our OS, enter in a terminal the following command:

cat /proc/sys/vm/swappiness

As you can see, in our case, the “swap” is set as 60. If you want to change the swappiness, you can use the operator digit at the end of which and will be the new value:

sudo sysctl vm.swappiness=25

As in the case with the configuration of the paging file, this setting will initially be temporary and is reset at the next reboot. In order to make it permanent at the end of the file /etc/sysctl.conf recordable value vm.swappiness=25.

vi /etc/sysctl.conf


For this you can use, for example, a text editor running with administrative permissions.

The size of swap file in Linux

There are several options to increase the linux swap file, or reduce it. However, before you consider them, it is necessary to reiterate one very important point:

When you completely disable the paging file, your operating system only uses RAM, and if it is a lack of it can lead to a complete shutdown of the computer. The system will just hang.

In order to avoid this, in the moment when we delete or change the primary paging file, you must create a second, temporary swap. If you did that or sure that the available RAM provides enough, you can go directly to the process of changing the size of the paging file. To do this, let’s first disable it:

sudo swapoff /swapfile

Second stage change the size of the paging file, using the command shown below. The size of your swap file you need to specify instead of the 2G (2 gigabytes):

sudo fallocate -l 2G /swapfile

Now to format the received file to the file system, swap:

sudo mkswap /swapfile

And then turn it back on:

sudo swapon /swapfile

While Linux allows to use several swap.

How to remove swap file in Linux

If for some reason the swap file is not needed, can remove it. For this you first need to disable swap on linux

sudo swapoff /swapfile

And then delete:

sudo rm /swapfile

Remember to set permissions on the newly created swap file as we described it above.

In conclusion

I hope now the concept of a swap file in Linux clear you 100%. Can go to practice and check the swap on your PC or laptop, set it up, create or even delete. If you still have any questions, suggestions or comments, you can ask them to us using the commenting form below.


(Visited 25 times, 2 visits today)