fbpx

In this article, we’ll take a look at how to add swap space on Debian 11. Debian is a popular operating system that is widely used by developers, system administrators, and hobbyists. One of the critical aspects of a Linux-based operating system is memory management. Linux uses swap space as a way to increase the amount of virtual memory available to a system. Swap space is essentially a portion of the hard drive that is used as an extension of the RAM.

Step 1: Check Your System’s Swap Space on Debian

Before adding more swap space, it’s important to check your system’s current swap space usage. You can do this by running the following command:

sudo swapon --show

This command will display any currently active swap devices on your system. If you see the output, it means that the swap space on Debian is already configured. If not, then you need to add swap space to your system. In the next picture, you will see that we already have 1 swap created.

How to Add Swap Space on Debian 11

Step 2: Create a Swap File

The first step in adding swap space on Debian is to create a swap file. A swap file is essentially a file on your hard drive that is used as swap space. To create a swap file, follow these steps:

Run the following command to create a file with the desired size (replace X with the desired size in gigabytes):

sudo fallocate -l XG /swapfile

Alternatively, you can use the dd command to create a swap file:

sudo dd if=/dev/zero of=/swapfile bs=1G count=X

Note that the fallocate command is faster and more efficient than the dd command.

Next, set the correct permissions for the swap file:

sudo chmod 600 /swapfile

This command sets the permissions of the swap file to read and write for the owner only.

Step 3: Activate the Swap File

Now that you have created a swap file, you need to activate it. To do so, follow these steps:

Run the following command to turn the file into swap space:

sudo mkswap /swapfile

This command initializes the swap file with the correct swap headers.

Next, activate the swap file by running:

sudo swapon /swapfile

This command turns on the swap file and adds it to the active swap devices on your system.

To make sure the swap file is activated on system boot, you need to add it to your `/etc/fstab` file. Run the following command to open the file in a text editor:

sudo nano /etc/fstab

Add the following line to the end of the file:

/swapfile none swap sw 0 0

This line tells your system to activate the swap file at boot time.

Step 4: Verify Swap Space on Debian

Once you have added the swap file to your system, you can verify that it’s working correctly. You can use the following command to check the amount of swap space available on your system:

sudo swapon --show

This command should display the swap file as an active swap device. You can also use the `free` command to check the amount of total and available memory on your system, including the swap space:

Verify Swap Space on Debian

Final Thoughts

Adding swap space on Debian 11 system is a straightforward process. By creating a swap file and activating it, you can increase the amount of virtual memory available to your system. Remember to set the correct permissions for the swap file and add it to your `/etc/fstab` file to ensure that it’s activated on system boot. With these steps, you should have a properly functioning swap space that can help improve your system’s performance.


1 Comment

Juanjo Díaz · December 18, 2023 at 3:11 PM

Great. Simple, straight and clear. Now I have 14 extra GigaBytes on my swapMem and no more NAGIOS alerts.

Thank you very much 🙂

Saludos desde Murcia, España!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.