How to Permanently Disable Swap in Linux?
Last Updated :
08 Oct, 2021
Swapping or swapping space is a physical memory page placed at the top of a disk partition or a special disk file that is used to expand a system's RAM as the physical memory fills up. Inactive memory pages are often dumped into the swap area when no RAM is available, using this method of expanding RAM resources. However, swap space is much lower in transfer speeds and access time compared to RAM, due to the spinning speed of standard hard disks.
Reserving a small partition for swapping will significantly increase access time and speed transfer compared to conventional HDD on newer machines with fast SSD hard drives, but the speed is still lower than RAM memory. Some say that it is appropriate to set the swap space to twice the amount of computer RAM. However, swap space should be set to 2 or 4 GB on systems with more than 4 GB of RAM.
If your server has sufficient RAM memory or does not need the use of swap space or your device output is significantly reduced by swapping, you can consider disabling the swap field.
You first need to visualize your degree of memory load before actually disabling swap space, and then identify the partition that holds the swap area by issuing the commands below.
free -h
Look for the Used Swap Space size. If 0B or close to 0 bytes is the size used, it can be assumed that swap space is not used intensively and can be disabled for protection.
Next, check for TYPE="swap" line after the blkid command to define the swap partition, as shown in the screenshot below.
blkid
check swap partition type
Again, issue the following lsblk command to search and identify the [SWAP] partition as shown in the below screenshot.
lsblk
Search Confirm Swap Partition
Run the below command to deactivate the swap area after you have identified the swap partition or file.
swapoff /dev/mapper/centos-swap
Or disable all swaps from /proc/swaps.
swapoff -a
To check if the swap area has been disabled, run the free command.
free -h
Disable Swap Partition
To permanently disable Linux swap space, open the /etc/fstab file, search for a swap line and add a # (hashtag) sign in front of the line to comment on the entire line, as shown in the screenshot below.
vi /etc/fstab
Afterwards, reboot the system in order to apply the new swap setting or issuing mount -a command in some cases might do the trick.
mount -a
After a system reboot, the commands presented at the beginning of this tutorial should be issued to indicate that the swap area in your system has been completely and permanently disabled.
free -h
blkid
lsblk
In simple ways or the other step:
If you really want to disable swapping (note: this is not suggested, even if you're pretty sure there's more than enough physical RAM), follow these steps:
- Run swapoff -a: this will immediately disable the swap.
- Remove any swap entry from /etc/fstab.
- Get the system rebooted. Ok, if the swap is gone. If it's still here for some reason, you have to remove the swap partition.
- Repeat steps 1 and 2 and, after that, use fdisk or parted to delete the (now unused) swap partition. Use great caution here: removing the wrong partition would have devastating results!
Similar Reads
How to Check Swap Space in Linux
Swap space is like an extra space in your computer's memory. When your computer's main memory (RAM) gets full it uses this extra room (swap space) to store things that aren't being used right now. This extra room is located on your computer's hard drive. Keeping an eye on how much of this extra room
5 min read
How to Disable IPv6 in linux?
In this article, we will discuss how to disable IPv6 in Linux. We can achieve this using sysctl. What is IPv6? IPv6 stands for internet protocol version 6, which is the latest version of the internet protocol. What is the Internet protocol? Internet Protocol (IP) is the protocol or set of rules by w
2 min read
How to Block or Disable Normal User Logins in Linux?
Here we will see how to block or disable normal user logins in Linux. This is a good idea to prevent Normal users from connecting to your system. We will see how to block Normal user logins using /etc/nologin file. We are going to tell the users that what is actually happening by showing them a mess
2 min read
How to Create a Swap File on Linux?
Linux Operating System has some different features that are unique and not present in any other major operating system like Windows. The Linux Memory Distribution is one of them. The Memory Space in Linux is designed in such a way that the efficiency of the Memory Management could be the best. The L
4 min read
How to Disable Unlock Login Keyring in Linux
The login keyring in Linux is a security feature that stores sensitive information like passwords and encryption keys. It ensures your system is secure by requiring authentication to access stored credentials. However, it can sometimes prompt users for a password when the desktop environment starts,
5 min read
How to mask a systemd unit in Linux?
Systemd is a system and service manager for Linux operating systems, providing a range of functionalities for managing system processes. In some cases, you may want to hide or disable a systemd unit to prevent it from starting automatically. This process is known as "masking" a systemd unit. In this
6 min read
How To Disable the Running Event in MySQL?
Pre-requisites: create an event in MySQL, Events in MySQL Events in MySQL are tasks that run according to a defined Schedule. events are executed based on schedule so it is also called a Scheduled event. In this article, we will talk about disabling running events. Steps for Disable the Running Even
1 min read
How to Disable Auto Login in Ubuntu
In Ubuntu, when the user logs in, the system can be configured to automatically log in without requiring a password. This feature is convenient for single-user systems or in situations where security is not a primary concern. However, there are cases where you may want to disable auto-login to enhan
5 min read
How to Attach a Swap Partition to Linux?
Virtual memory in Linux OS is RAM + swap space. It is used when memory usage of the system exceeds a certain point then swap space is used all the idle processes are shifted to swap and new processes are assigned to RAM. Now how to allocate swap space ideally swap space should be twice RAM size for
3 min read
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
As you use your Linux system, it constantly manages memory to keep your system running smoothly. Over time, however, certain parts of the memory like cache, buffers, and swap space can build up and slow things down. In this article, weâll explore how to clear RAM Memory Cache, Buffer, and Swap Space
6 min read