By drupalmaster |

When setting up a Linux distro using an installer in your favorite hypervisor of choice, you'll need to decide whether you wish to use a swap partition or a swap file for managing swap. Most installers will give you the option to use a swap partition. In my particular instance, I installed Debian 12 Bookworm, 64-bit using the AMD netinst (net installer). At some point in the installation setup process using the netinst ISO, I indicated that I wanted to have a separate partition for my $HOME  directory, but did not specify a swap partition. This was assumed by the installer and a 1GB swap partition was setup for me by default. This is okay since, in this article, we will be eliminating the swap partition altogether and replacing it with a swap file.

Here are the steps to replace your swap partition in Debian 12 Bookworm with a swap file. Creating a swap file is advantageous over the swap partition as it is much easier to expand and shrink this file than it is the partition. It works just like the swap partition and it can even be on another drive apart from your system drive.

Step 1:

Turn off the current swap partition, if it exists. Run the following command and all future commands in the terminal.

 $ sudo swapon --show

NameTypeSizeUsedPriority
/dev/vda2partition156241880-2

 

Turn off the current swap using the command:

 $ sudo swapoff /dev/vda2

Remember, replace /dev/vda2 with whatever was revealed on your system when you ran the swapon --show command above.

Step 2:

Remove the old swap entry from the /etc/fstab file.

$ sudo nano /etc/fstab

The entry will look something like the following:

UUID=d1b17f9c-9c5e-4471-854a-3ccaf358c30b none swap sw 0 0

Comment out this line by place a # in front of it. Then, hit Ctrl + X and respond with Y for YES and hit Enter to save the file.

Step 3:

Create the Swap File. Let's create that swap file to the desired size that we want. We're going to create the file size as 2 GiB rather than 2GB. I have 4GB allocated to the VM, so a 2GiB swap file should be adequate. The distinction here between GB and GiB is the former is based on a 1024 block size and the latter is based on 1000 block size.

sudo dd if=/dev/zero of=/swap bs=1024 count=2097152

Here, I located the file in the / (root) directory and simply called it swap, hence *of=/swap*.

Step 4:

Set the swap file permission to 600. This means that the permissions on our swap file will be read/write for the owner, and no permissions for anyone else. To accomplish this, run the following command in the terminal.

$ sudo chmod 600 /swap

Step 5:

Format the newly-created swap file as swap.

$ sudo mkswap /swap

Step 6:

Enable the new swap file.

$ sudo swapon /swap

To verify the new swap file is in use run:

$ sudo swapon --show

The following similar output should result:

NameTypeSizeUsedPriority
/swapfile2G0-2

Step 7:

Add the newly-created swap file to the /etc/fstab file.

$ sudo nano /etc/fstab

Underneath the line that you commented out earlier add the following:

/swap   none   swap   sw   0 0

Close and save the file by hitting Ctrl + X, then Y for YES, and Enter to Close the file.

Step 8:

Reboot the system and re-verify that swap is in use.

$ sudo reboot

$ sudo swapon --show

Once re-verified, you should be good to go. If you like, you can open *top* or *htop* and verify visually.