Best way to disable swap in Linux
- Identify configured swap devices and files with
- Turn off all swap devices and files with
- Remove any matching reference found in
- 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.
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.
See also Set up use of swap partition with systemd.
If you are really sure you want to disable swapping (note: this is not recommended, even where you are pretty sure that physical RAM is more than enough), follow these steps:
swapoff -a: this will immediately disable swap
- remove any swap entry from
rebootthe system. If the swap is gone, good. If, for some reason, it is still here, you had to remove the swap partition. Repeat steps 1 and 2 and, after that, use
partedto remove the (now unused) swap partition. Use great care here: removing the wrong partition will have disastrous effects!
On Raspbian 10 (Buster), the clean answer would be:
To disable it until the next reboot, as stated in
sudo /sbin/dphys-swapfile swapoff
To disable swap on boot:
sudo systemctl disable dphys-swapfile
(It turns out I couldn't find that information anywhere...)
On my Linux Mint box (version 19.3 (Tricia), based on Ubuntu 18.04 (Bionic Beaver)) without a swap partition or without any swap at all, systemctl reported that swapfile.swap failed during every start. It could be disabled with the command:
sudo systemctl disable swapfile.swap
The swapfile.swap is a 'special' part of systemd, which you can read about in man, using the
man systemd.special command.