TechWhale

Back

How To Add Swap Space on Ubuntu and Debian 12 / 13How To Add Swap Space on Ubuntu and Debian 12 / 13

If you are running a Debian 11 system and are experiencing slow performance or running out of memory, you may need to add swap space to your system. Swap space is a designated area on your hard drive that is used to temporarily store data when your system has run out of physical memory (RAM). In this guide, we will walk you through the steps to add swap space to your Debian 11 system.

Step 1: Checking for Existing Swap Space#

Before adding a new swap file, you should first check if there is any existing swap space on your system. To do this, run the following command in your terminal:

sudo swapon --show
bash

If you get no output, it means there is no existing swap space on your system.

Step 2: Creating a Swap File#

To create a new swap file, we will use the fallocate command. This command creates a file with a specified size. For example, to create a 2GB swap file, run the following command:

sudo fallocate -l 2G /swapfile
bash

Next, we need to restrict access to the swap file to root only. Run the following command:

sudo chmod 600 /swapfile
bash

Step 3: Enabling the Swap File#

Now that we have created the swap file, we need to enable it. Run the following command:

sudo mkswap /swapfile
bash

This command initializes the swap file. Next, we need to enable the swap file with the following command:

sudo swapon /swapfile
bash

To make the swap file permanent, we need to add it to the /etc/fstab file. Open the file with your preferred text editor:

sudo nano /etc/fstab
bash

Add the following line to the file:

/swapfile swap swap defaults 0 0
bash

Save and close the file.

Step 4: Verifying the Swap Space#

To verify that the swap space has been added, you can run the following command:

sudo swapon --show
bash

This command should now display your new swap file.

Tips and Tricks#

Customizing the Size of the Swap File#

You can customize the size of the swap file to your requirements. Just replace “2G” in the fallocate command with the desired size (e.g. 4G, 8G, etc.). However, it is recommended to have a swap space of 2GB or less, depending on your system’s needs.

Swap File vs Swap Partition#

You can also create a swap partition instead of a swap file. However, it is recommended to use a swap file as it is easier to resize and manage.

Adding Too Much Swap Space#

Adding too much swap space can actually slow down your system, as the system will start using the swap space instead of physical memory. It is recommended to have a swap space of 2GB or less, depending on your system’s needs.

Verifying Swap Space Usage#

To verify how much swap space is being used, run the following command:

sudo swapon --summary
bash

This command will show you the amount of swap space being used, as well as the total amount of swap space available.

Removing Swap Space#

If you no longer need the swap space, you can remove it by running the following commands:

sudo swapoff /swapfile
sudo rm /swapfile
bash

Congratulations! You have successfully added swap space to your Debian 11 system.

How To Add Swap Space on Ubuntu and Debian 12 / 13
https://techwhale.in/how-to-add-swap-space-on-ubuntu-22-and-debian-11-12/
Author Mayur Chavhan
Published at July 14, 2023

Related posts