Creating a swap file instead of a swap partition

Why change something that works?

I try to configure my system as simply as possible, with the greatest possible benefit for me. The reader should be aware that this is a matter of opinion. Since I build my system with only a /boot/efi and a / partition where I encrypt the / partition, the question of a swap area arises. It is possible to do this with another partition that should also be encrypted or with a swap file that is located on the encrypted / partition. Another advantage is that you can change the size of the swap file on the fly, a disadvantage of a swapfile is hybernating, it is quite complicated to configure.
Here I will describe how to create a swapfile.

First we create a file with:

sudo dd if=/dev/zero of=/srv/swapfile bs=1024 count=33554432

This command line creates a swap file in the /svr directory with a file size of 32 GigaByte (the size of the installed RAM).

As a second step, we let Linux make a swap file out of it:

sudo mkswap /srv/swapfile

Now we have to change the rights of the file:

sudo chown root:root /srv/swapfile

and

sudo chmod 0600 /srv/swapfile

The fourth step is to mount the swap file:

sudo swapon /srv/swapfile

To disable it, simply use:

sudo swapoff /srv/swapfile

To make the whole thing available after a reboot, we have to edit a file with the text editor of your choice:

/etc/fstab

here we add the following entry at the bottom:

/srv/swapfile swap swap defaults 0 0

This ensures that the swap file is included after a reboot.