- Best way to disable swap in Linux
- 7 Answers 7
- How to Permanently Disable Swap Partition in Linux
- How to Check Swap Space in Linux
- How to Check Swap Partition in Linux
- How to Disable Swap in Linux
- How to Disable Swap Permanently in Linux
- Related Posts
- 8 thoughts on “How to Permanently Disable Swap Partition in Linux”
- How can I turn off swap permanently?
- 6 Answers 6
- How to completely disable swap?
Best way to disable swap in Linux
I am running a custom compiled 3.18.9 kernel and I am wondering about the best way to disable swap on the system. I also use init if it makes a difference. Is it enough to comment or remove the swap line in /etc/fstab to prevent swap from working/mounting at boot or should I recompile the kernel without Support for paging of anonymous memory (swap) to be 100% sure it does not get enabled? I run encrypted partitions and want to prevent accidental leakage to the hard disk. My system specifications are also great enough that I can survive in a swap-less environment.
@MichaelHampton I didn’t see a need at the point in time until I realized what was eventually going to happen. Plus my system doesn’t reach a point in any time that the swap was used, so I feel I am free to remove it.
I’d remove the feature from the kernel then. Otherwise someone can plug an USB-stick and start swapping to it again.
@ott Doesn’t the user require super user access to use the swapon/swapoff executable? I might disable it again just in case, but I am uncertain a unprivileged user can create swap files.
@ott— this is for a laptop so I will be in control of it 24/7. If someone has gotten physical access to it or managed to exploit it to gain access to my unprivileged users there would be worse problems than worrying about a swap file being created. Thanks for the clarification though!
7 Answers 7
- Identify configured swap devices and files with cat /proc/swaps .
- Turn off all swap devices and files with swapoff -a .
- Remove any matching reference found in /etc/fstab .
- Optional: Destroy any swap devices or files found in step 1 to prevent their reuse. Due to your concerns about leaking sensitive information, you may wish to consider performing some sort of secure wipe.
On some systems you must also rebuild the initrd archive, e.g. with dracut —regenerate-all —force or mkinitrd , or the system will not boot. Thanks to J.O. Aho and Carlos E.R. on alt.os.linux.suse .
This doesn’t solve it for me, the system boots every time with swap enabled, even though there’s no fstab entry for it. It’s incredibly annoying.
It used to be that only swap partitions in /etc/fstab were used automatically, however, systemd may be changing that slightly. You might need to do:
systemctl mask dev-sdXX.swap
(change sdXX) to your real formatted swap partition, which, begs the question of why you have a swap partition if you don’t want it used.
If you are not using systemd, then, removing the swap entries from /etc/fstab should be sufficient (as far as I know).
Maybe the real solution is to get rid of the swap partitions, so they won’t be used accidentally. To remove the swap partitions, I would use fdisk to change the partition type from swap to something else, and then reformat the partition or use: dd if=/dev/zero of=/dev/old-swap-partition in order to zero it out and prevent its use.
How to Permanently Disable Swap Partition in Linux
Swapping or swap space represents a physical memory page that lives on top of a disk partition or a special disk file used for extending the RAM memory of a system when the physical memory fills up.
Using this method of extending RAM resources, inactive memory pages are frequently dumped into the swap area when no RAM is available. However, due to the spinning speed of classical hard disks, swap space is way lower in transfer speeds and access time compared to RAM.
On newer machines with fast SSD hard disks, reserving a small partition for swapping can greatly improve access time and speed transfer compared to classical HDD, but the speed is still magnitudes lower than RAM memory.
Some suggest that the swap space should be set as twice the amount of machine RAM. However, on systems with more than 4 GB of RAM, swap space should be set between 2 or 4 GB.
In case your server has sufficient RAM memory or does not require the use of swap space or the swapping greatly decreases your system performance, you should consider disabling the swap area.
How to Check Swap Space in Linux
Before actually disabling swap space, first, you need to visualize your memory load degree and then identify the partition that holds the swap area, by issuing the below free command.
Look for the Swap space used size. If the used size is 0B or close to 0 bytes, it can be assumed that swap space is not used intensively and can be safely disabled.
How to Check Swap Partition in Linux
Next, issue following the blkid command, look for TYPE=»swap» line in order to identify the swap partition, as shown in the below screenshot.
Again, issue the following lsblk command to search and identify the [SWAP] partition as shown in the below screenshot.
How to Disable Swap in Linux
After you’ve identified the swap partition or file, execute the below command to deactivate the swap area.
# swapoff /dev/mapper/centos-swap
Or disable all swaps from /proc/swaps, which provides a snapshot of the swap file name.
Run free command in order to check if the swap area has been disabled.
How to Disable Swap Permanently in Linux
In order to permanently disable swap space in Linux, open /etc/fstab file, search for the swap line and comment on the entire line by adding a # (hashtag) sign in front of the line, as shown in the below screenshot.
Afterward, reboot the system in order to apply the new swap setting or issue mount -a command in some cases might do the trick.
After the system reboot, issuing the commands presented at the beginning of this tutorial should reflect that the swap area has been completely and permanently disabled in your system.
You might also like:
I’am a computer addicted guy, a fan of open source and linux based system software, have about 4 years experience with Linux distributions desktop, servers and bash scripting.
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
8 thoughts on “How to Permanently Disable Swap Partition in Linux”
I’ve been told that “swap space” is used during SUSPEND operations. I welcome someone to offer a robust explanation. It is important for road warriors and any who rely on laptop portability. During “sleep” [suspend to ram] current runtime details get written to swap before putting things on hold. During “hibernate” [suspend to disk] the majority of the system state gets written to swap before power off. Reply
The “free -h” commands must be executed when the system is under heavy load. Otherwise, it does not provide the information necessary to determine whether a swap partition or space is needed. When executed on an idling system, the command will only tell you the size of the swap partition and that it is not being used. It’s like running the “top” command on an idling system to see the CPU Load. It will show the CPU Load to be negligible. I agree with S. Daniels on the use of GParted. It not only provides a visual representation of your disks but also can delete your swap partition and recover that space by moving and/or resizing other partitions. Using the command line to accomplish your tasks may be ‘leet’ and cool but it is also dangerous to the health of your system. Using GParted prevents misspelling or use of incorrect CLI commands, as well as the use of incorrect options which can result in making the drive unusable. Reply
These instructions are rather outdated for any linux with a desktop:MATE, xfce, Gnome or KDE/Plasma. Firstly, the program Gparted very nicely displays swap size, location and UUID. “blkid”, especially on a dual-boot or system with multiple OSes is just going to display a lot of confusing and unhelpful entries. The KDE partitioner is inferior, but adequate. The centos-swap is fairly specific to CentOS Linux, this should be explained in the article. A swap file can also be used in place of a dedicated partition, this should also be mentioned. “Vi” is very much overkill for editing fstab. “Vim” is easier than “vi” for newbies in every case, and “pico /etc/fstab” or “nano /etc/fstab” are more appropriate here. Finally, for José , 30GB is seriously wrong. I suspect he somehow changed a data or system partition into a swap partition. He can “manage flags in Gparted” to inspect it for contents. I’m just a hobbyist, but have nearly 20 years experience, since a Debian bootable CD, the first Linux installation disk that did not need a floppy to boot, came out in 2000. Reply
How can I turn off swap permanently?
Swap makes my system all sluggish and turning it off makes everything smoother. I have 3.5 GB of RAM. I know how to turn swap off by entering sudo swapoff -a in the terminal, but that’s just for the current session, because after a reboot swap turns itself back on. Is there any way to turn swap off permanently?
Yes it is, but it still can be used. I have a little script that mounts the swap partition when the system goes into hibernation and un-mounts it when it comes back. The swap file doesn’t need to be active in the meantime.
@user264467: it is quite counterintuitive to leave swap enabled just to disable it on every reboot, it is much cleaner to disable it permamently.
6 Answers 6
May I suggest a safer approach? You never know when swap can save you from crashing. While swapping does indeed slow down your computer, if you use a program that eats up all your RAM, having the ability to swap can save you from a hard reboot. When the computer starts swapping, you’ll notice and be able to kill the offending application.
So, instead of disabling swap altogether, just make sure your OS swaps very rarely. This is controlled by the vm.swappiness setting in /etc/sysctl.conf . Ubuntu’s default setting is 60 if I remember correctly which is too much for most situations and will cause you to start swapping even while RAM is still available. If you reduce this value, you will be able to keep the safety line of swap while only using it for emergencies. So, open the file:
If that is still too much, change the 10 to 1. Now, after you restart, you will only swap when absolutely necessary and you can simply forget about it.
How to completely disable swap?
I am using Debian sid, hard drive formatted with ext4, running on linux 3.1 I remember on previous linux versions (maybe before 3.0), if I run out of memory, and swap is not enabled, programs will usually crash. This is perfect for my environment: simple web browsing with no critical operations. That is, if I accidentally run across a bad website which uses up too much memory, it just crashes without rendering my terminal unusable. But in my current setup, the computer hangs with violent I/O throughput in the background. iotop reveals kswapd0 to be the culprit, which means it is due to swapping. After using swapon -s to determine any swaps that were enabled, I used swapoff -a to disable all swaps and swapon -s again to confirm that all swaps were disabled. Then I tried maximizing my memory usage again. Alas, the behavior I expected didn’t happen. Instead, kswapd0 tries over and over to swap out the RAM and fails as there is no swap space. Because it never gives up, my computer is locked in eternal I/O heavy freeze, bad for my disk’s health. Am I doing something wrong in trying to swapoff -a ? Why is the behavior different than what it used to be (probably pre-3.0 times)?
That doesn’t really make sense. Doing the swapoff -a itself, if there was stuff in the swap, will generate a lot of I/O (and can result in processes getting killed if there is not enough real RAM availabe). Are you sure it’s not the swapoff -a that caused the I/O «storm»?
@Mat swapoff -a should disable swap permanently, meaning it should stay disabled after next reboot. I confirmed this. Yet, I/O «storm» still happens during the session after next reboot. For the record, I/O «storm» didn’t happen at the moment I did swapoff -a because swap was 0 at that time.
database loading got to about 15% in 14 hours. Turned off swap, and on the next attempt it’s gotten to 40% in 4 hours. admittedly, the server is under-powered and lown on ram, but without swap turned on OpenSuSE works much faster for this one process. The OS’s opinion of «better» and mine differ dramatically during a simple mysql db load. commented out the swap drive in /etc/fstab and rebooted.