How to disable SWAP partition permanently on Linux

by time news

2023-05-29 08:30:05

Swapping or swap space (Swap) represents a page of physical memory that lives on top of a disk partition or a special disk file that is used to expand a system’s RAM when physical memory becomes full.

With this method of expanding RAM resources, idle memory pages are frequently flushed to the swap area when RAM is not available. However, due to the spin speed of classic hard drives, swap space is much lower in terms of transfer speeds and access time compared to RAM.

On newer machines with fast SSD hard drives, reserving a small partition for swapping can greatly improve access time and transfer speed compared to classic HDD, but the speed is still less than memory RAM.

Some suggest that swap space should be set to twice the amount of RAM on the machine. However, on systems with more than 4 GB of RAM, the swap space must be set between 2 o 4 GB.

In case your server has enough RAM or does not require the use of swap space or swapping significantly slows down your system performance, you should consider disabling swap.

How to Check Swap Space on Linux

Before disabling swap, you must first view how loaded your memory is and then identify the partition that contains the swap area by issuing the following free command.

# free -h 

Look for the used size of swap space. If the size used is 0B or seek a 0 bytes, it can be assumed that swap space is not heavily used and can be safely disabled.

Check swap space

How to Check Swap Partition in Linux

Then output following the blkid command seek TYPE="swap"the line to identify the swap partition, as shown in the following screenshot.

# blkid 

Check swap partition type

Again, issue the following lsblk command to find and identify the [SWAP]partition as shown in the following screenshot.

# lsblk

Search Confirm swap partition

How to disable sharing on Linux

Once you have identified the swap file or partition, run the following command to disable swap.

# swapoff /dev/mapper/centos-swap  

Or disable all sharing from /proc/swaps which provides a snapshot of the swap file name.

# swap -a 

Run the free command to check if the swap area has been disabled.

# free -h

Disable swap partition

How to disable swap permanently on Linux

To permanently disable swap space on Linux, open the file /etc/fstab find the swap line and comment out the entire line by adding a ‘ sign#' (hashtag) in front of the line, as shown in the following screenshot.

vi /etc/fstab

Disable swap partition permanently

Then, restart the system to apply the new swap settings or type “mount -a” a command in some cases could do the trick.

# mount -a

After rebooting the system, issuing the commands presented at the beginning of this tutorial should reflect that swapping has been completely and permanently disabled on your system.

# free -h
# blkid
# lsblk

That’s it, we hope you found this trick useful for your Linux system. On production servers, it is recommended to disable the swap partition.

#disable #SWAP #partition #permanently #Linux

You may also like

Leave a Comment