How to create or enlarge a Swap file in Kali Linux

How to add Swap file in Kali Linux

If you do not have enough RAM, you can add a Swap file. If you do not have enough RAM with the existing swap file/partition, you can

  • create a larger swap file and use only it
  • create an additional swap file and use it together with the existing swap partition

The advantage of a swap file over a swap partition is that you don't have to partition drives, and you don't even have to reboot your system to enable/disable a swap file.

Let's start by checking what the size of the swap file/partition is for the system at the moment:

swapon --show

So, there is a swap partition /dev/sda5 with a size of 4.9G.

Suppose this is not enough for me and I want a swap file to be 10 GB in size. This file will be located in the root of the file system - that is, there should be enough space to accommodate this file.

I create the file itself that will be used as a swap file:

sudo fallocate -l 10G /swapfile

Set the correct permitions:

sudo chmod 600 /swapfile

Format this file in swap:

sudo mkswap /swapfile

And we enable the new swap file:

sudo swapon /swapfile

Check again:

swapon --show

We still have the swap partition /dev/sda5, and the swap file /swapfile 10G in size appeared.

The PRIO field is a priority, and it shows that the swap partition /dev/sda5 will be used first, and if it is full, the /swapfile will be used — this behavior can be changed.

To use the new paging file every time the computer is turned on, you need to open the /etc/fstab file:

sudo gedit /etc/fstab

And add a line here:

/swapfile none swap defaults 0 0

Please note that there is already a line in my file:

UUID=176a8bbf-394e-457f-9d37-8c4edb8064af none swap sw 0 0

If you have a swap partition, then you will have the same string, only the UUID value will be different. If you do not want the original swap partition to be used, simply delete this line.

Or you can leave both partitions/swap files, but change their priority.

To do this, use the pri= parameter, for example:

/swapfile none swap defaults,pri=100 0 0
/dev/sda1 none swap defaults,pri=10 0 0

Or set the priority values in the command line:

sudo swapoff /swapfile
sudo swapon --priority 100 /swapfile

The result is:

How to remove Swap file in Kali Linux

To delete a swap file, you need to disable and then it can be deleted:

sudo swapoff /swapfile
sudo rm -f /swapfile

Next, delete the corresponding line from the /etc/fstab file.

How to check Swap file usage in Linux

The swap file will be used when the physical memory runs out. In order to check the operation of the Swap file, you need to artificially use up all the RAM. You don’t need to run many applications and open dozens of tabs in your web browser to do this, instead you can use stress testing tools like stress-ng.

Install stress-ng:

sudo apt install stress-ng

An example of a command that will consume 10% more RAM than system has free RAM:

stress-ng --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 1.1;}' < /proc/meminfo)k --vm-keep -m 1

After that, watch the memory consumption – you will see how the Swap file starts to be used.

On the command line, Swap file usage can be checked with the command:

swapon --show

Dynamic swap files are created only when needed

Swapspace is an excellent dynamic swap space manager

The disadvantage of large swap files is that they take up a lot of disk space even during those periods when programs have enough RAM and swap files are not used.

The Swapspace program solves this problem: swap files are created only when they are really needed, that is, when the operating system runs out of RAM. If the created Swap file runs out of space, another one is created. If it is not enough, then as many swap files are created as necessary. This prevents applications that need RAM from crashing. After the need for Swap files disappears, they are automatically deleted.

If you often need large swap files, then create a static large file without using Swapspace. Or, as a better option, increase the amount of RAM in your computer or server.

The Swapspace service can be used in conjunction with a fixed size swap file, in which case it will be taken into account when creating additional swap files.

Install Swapspace:

sudo apt install swapspace

Setting up swapspace

The swapspace service does not need to be configured, it is easy enough to start!

Swapspace already has balanced settings for creating and deleting swap files. If desired, you can change the settings in the /etc/swapspace.conf file:

sudo gedit /etc/swapspace.conf

You can change the time after which swap files will be deleted after they are freed, you can set the maximum size of swap files created, you can change the location of swap files.

By the way, you can check the available disk space with the following command:

df -h /

Keep in mind that the swap file should only be readable by the root user, otherwise it would be a serious security hole.

As already mentioned, there is no need to configure anything, you can proceed to start the service.

Managing the swapspace service

Starting the swapspace service:

sudo systemctl start swapspace.service

Checking the status of the swapspace service:

systemctl status swapspace.service

Adding the swapspace service to autoload:

sudo systemctl enable swapspace.service

To stop and remove the swapspace service from startup, use the following commands:

sudo systemctl stop swapspace.service
sudo systemctl disable swapspace.service

Checking if swapspace works

To fill all available RAM, use the following command:

stress-ng --vm-bytes $(awk '/MemAvailable/{printf "%d\n", $2 * 1.1;}' < /proc/meminfo)k --vm-keep -m 1

As you can see, the swap file is being used, even though there was no swap file on the system before swapspace was started.

Check swap file usage with swapon:

swapon --show

It turns out that three swap files have been created, one of which is completely filled, the other is partially filled, and the third, apparently, has been prepared in advance:

NAME                 TYPE   SIZE   USED PRIO
/var/lib/swapspace/1 file 809,6M 808,6M   -2
/var/lib/swapspace/2 file 665,1M  90,7M   -3
/var/lib/swapspace/3 file 823,6M     0B   -4

Stop the stress test:

Some time after the end of the stress test (the specific time can be configured), the swap files are deleted automatically:

Recommended for you:

4 Comments to How to create or enlarge a Swap file in Kali Linux

  1. ninineshark says:

    Thanks for article. But how after delete old swap file on /dev/sda5 ?

    • Alex says:

      Hello! During installation, I always choose “Manual” partition method and do not create a swap partition so that I do not think about the problem you are asking about in the future.

      If you have already created the partition, you can use disk partitioning tools such as GParted (a GUI program). In it, you can delete an unnecessary partition and use the freed space to expand the remaining partitions. I think there will be no problems, but you need to remember that operations related to disk partitioning are always a little risky.

  2. rcfa says:

    Useful article, however, if one already has a swap partition, and simply wants to have insurance against system crashes due to running out of swap space, the swapspace package is rather convenient. It will create and delete swapfiles dynamically, without the need of messing around with /etc/fstab.

    Also, if someone has a lot of RAM and almost never needs swap, then one can save the diskspace wasted for the swap partition, and simply use swapspace just in case.

    It might thus be useful if you appended your article with instructions for using swapspace

    • Alex says:

      Hello! Thanks for the idea – I liked this dynamic swap space manager and added a section about swapspace to this article.

Leave a Reply to ninineshark Cancel reply

Your email address will not be published. Required fields are marked *