How to Add Swap Space on Debian 11

This guide is applicable only for KVM based VPSes and dedicated servers, If you wish to increase swap on your OpenVZ based VPS please contact us via a Support ticket from the client area.

To check swap Information.

Command to check swap size.

swapon --show

Output:

root@server:~# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/vda5 partition 1.9G   0B   -2

Or you can check using free command.

free -m

Output:

root@server:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            3930         200        3104           5         625        3500
Swap:           1951           0        1951

NOTE: If the output is empty, it means that swap space is not enabled yet on your system.

To add SWAP to your VPS.

Command to create a Swap file.

NOTE: We are adding 1GB if you want to add different size then you can replace it in place of 1G

fallocate -l 1G /swapfile

or

dd if=/dev/zero of=/swapfile bs=1024 count=1048576

To set write permission.[Default permissions only enable for root user]

chmod 600 /swapfile

To set up a swap area on the file.

mkswap /swapfile

Output:

root@server:~# mkswap /swapfile 
mkswap: /swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=145336f9-b45a-4bd7-8f26-d972f4df9640

To activate the swap file.

swapon /swapfile

Open the following file and add the given content to change it to permanent.

nano /etc/fstab

Add the following line.

/swapfile swap swap defaults 0 0

Now check the swap size.

swapon --show

or

free -m

Output:

root@server:~# swapon --show
NAME      TYPE       SIZE USED PRIO
/dev/vda5 partition  1.9G   0B   -2
/swapfile file      1024M   0B   -3
root@server:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            3930         202        3099           5         627        3497
Swap:           2975           0        2975

NOTE: Now the added swap space can be seen.

To remove added swap file.

Deactivate the swap space.

swapoff -v /swapfile

Output:

root@vps:~# swapoff -v /swapfile
swapoff /swapfile

Next, remove the following line from the file /etc/fstab

/swapfile swap swap defaults 0 0

Command to remove the file.

rm /swapfile

This concludes our topic of configuring swap in Debian.

If you can not configure and facing technical difficulties, kindly reach out to us via Support Ticket.