How to create or enlarge a 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 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 swap file compared to swap partition is that you don’t need to partition your disks, and to enable/disable the swap partition you don’t even need to reboot the system.

Let's start by checking what the size of the swap 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:

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.

Last Updated on

Recommended for you:

Leave a Reply

Your email address will not be published.