How to Permanently Disable Swap in Linux?


Linux is a free and open-source operating system that is widely used in the tech industry. It is known for its stability, security, and flexibility. One of the key features of Linux is the ability to manage memory effectively using swap space. Swap space allows the system to use hard disk space as virtual memory, which helps to prevent the system from running out of memory.

In this tutorial, we will discuss how to permanently disable swap space in Linux. Disabling swap space is useful in situations where we have enough physical memory (RAM) and don't want the system to use hard disk space as virtual memory. It can also be useful in situations where the system has limited hard disk space, and we want to reclaim that space by disabling the swap. In the next section of the article, we will explain how to disable swap space temporarily before proceeding to the permanent solution.

How to Permanently Disable Swap in Linux?

Before we proceed with disabling swap space permanently, let's first understand what swap space is and how can we check it in linux.

Swap space is a portion of the hard disk that is used by the operating system as virtual memory. When the system runs out of physical memory (RAM), it moves inactive pages of memory to the swap space to free up space in the RAM for active processes. This helps to prevent the system from crashing due to a lack of memory.

To check the current swap usage, we can use the 'free' command. Open the terminal and type the following command 

$ free -h

The output of this command will show us the current swap usage. The '-h' option is used to display the output in a human-readable format. Here is an example output 

              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.9G        2.0G        221M        2.7G        4.2G
Swap:          2.0G        0B          2.0G

In the above output, the 'Swap' line shows us that the system has a total of 2.0 GB of swap space available. In this case, the 'used' column shows 0B, which means that the system is not currently using any swap space.

Now that we understand the basic of swap space, let’s move to the next section of this article to learn how can we disable the same.

Disabling Swap Usage

There are two ways to disable the usage of swap space in Linux, which are temporary and permanent. Temporarily disabling swap involves using the "swapoff" command to turn off swap space for the current session. While this method can be useful for testing purposes, it has some drawbacks, such as reducing the overall performance of the system and potentially causing crashes in applications that rely heavily on swap space. Therefore, it is recommended to only use this method for temporary testing purposes.

On the other hand, permanently disabling the swap involves editing the "/etc/fstab" file to remove the entry for the swap partition. This method ensures that swap space is disabled every time the system boots up. While it may require a bit more effort to set up, it provides a more reliable and long-term solution for managing memory usage on a Linux system.

To gain a better understanding of how to disable swap space in Linux, let's take a closer look at both methods in more detail.

Disable Swap Space Temporarily

To disable swap space temporarily using the "swapoff" command. This is useful if we need to perform maintenance on the system and want to prevent the system from using swap space during the maintenance.

To disable swap space temporarily, we can use the "swapoff" command followed by the device file of the swap partition. Open the terminal and type the following command −

$ sudo swapoff -a

The "-a" option tells the command to disable all swap devices. If we only want to disable a specific swap device, we can replace "-a" with the device file path (e.g. "/dev/sda3").

Once we have disabled swap space, we can perform our maintenance tasks without the system using swap space. However, there are some drawbacks to disabling swap space temporarily. For example, if the system runs out of physical memory (RAM) while the swap is disabled, the system may crash or become unresponsive. Additionally, disabling swap space can cause performance issues if the system is under a heavy memory load.

Now that we've covered how to temporarily disable swap space, let's move on to disabling it permanently.

Disable Swap Space Permanently

By disabling swap space permanently, you can ensure that the system will never use swap space again, unless you manually re-enable it. This provides a more reliable and long-term solution for managing memory usage on your Linux system.

To permanently disable swap space we need to edit the "/etc/fstab" file. This file contains configuration information for the file systems and swap space on the system.

Open a terminal and type the following command to open the "/etc/fstab" file in a text editor:

$ sudo nano /etc/fstab

Locate the line in the "/etc/fstab" file that specifies the swap partition. This line will include the word "swap" and the device file path for the swap partition.

Comment out this line by adding a "#" character at the beginning of the line. This will disable the line and prevent the system from using the swap partition. The line should now look something like this:

# /dev/sda3  none  swap  sw  0  0

Save the changes to the file and exit the text editor.

Finally, we need to disable the swap space on the system by running the following command:

$ sudo swapoff -a

This will immediately disable all swap space on the system.

To confirm that swap has been permanently disabled, we can run the "free -h" command again and check the "Swap" line. It should show "0B" in the "used" column, indicating that swap is no longer being used by the system.

Here's an example output of the "free -h" command after disabling swap space permanently −

              total        used        free      shared  buff/cache   available
Mem:           7.8G        1.1G        5.5G        108M        1.2G        6.3G
Swap:            0B          0B          0B

As you can see in the above output, the "Swap" line shows "0B" for both the "used" and "free" columns, indicating that swap space is no longer being used by the system. If you see any other value for the "used" column, it means that swap space is still being used and you may need to double-check your configuration to ensure that it has been disabled permanently.

Conclusion

In this tutorial, we discussed how to permanently disable swap space in Linux, an operating system known for its stability, security, and flexibility. Swap space is the portion of the hard disk that is used by the operating system as virtual memory, which helps to prevent the system from running out of memory. We explained how to disable swap space temporarily before proceeding to the permanent solution. By disabling swap space permanently, we can ensure that the system will never use swap space again, providing a more reliable and long-term solution for managing memory usage on a Linux system.

Updated on: 27-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements