Best way to disable swap in Linux

215,803

Solution 1

  1. Identify configured swap devices and files with cat /proc/swaps.
  2. Turn off all swap devices and files with swapoff -a.
  3. Remove any matching reference found in /etc/fstab.
  4. 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.

man swapoff

Solution 2

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.

Solution 3

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:

  1. run swapoff -a: this will immediately disable swap
  2. remove any swap entry from /etc/fstab
  3. reboot the 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 fdisk or parted to remove the (now unused) swap partition. Use great care here: removing the wrong partition will have disastrous effects!
  4. reboot

Solution 4

On Raspbian 10 (Buster), the clean answer would be:

To disable it until the next reboot, as stated in */etc/fstab*:

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...)

Solution 5

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.

Share:
215,803

Related videos on Youtube

user283167
Author by

user283167

Updated on September 18, 2022

Comments

  • user283167
    user283167 over 1 year

    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.

    • Michael Hampton
      Michael Hampton about 9 years
      Why isn't your swap encrypted?
    • user283167
      user283167 about 9 years
      @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.
    • user283167
      user283167 about 9 years
      @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--
      ott-- about 9 years
      Indeed, he needs super user access, which he could with an exploit e.g.. But are you watching your server 24/7?
    • user283167
      user283167 about 9 years
      @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!
  • user283167
    user283167 about 9 years
    I still use init via openRC, I purposefully purged systemd from the install. And as for the swap question, I was doing a regular install until I made the decision to use cryptsetup/luks to mount encrypted files formatted to ext4 over top of my filesystem. If you could be so kind to touch upon the way to disable swap when using init I would appreciate it.
  • Rolf
    Rolf about 6 years
    Why is it not recommended to disable swap even if you have enough ram?
  • shodanshok
    shodanshok about 6 years
    Because Linux proactively uses the swap partition to free more memory for caching. This can improve performance. Anyway, it is a tunable parameter. For more information, read here. Moreover, an improvvise surge in memory allocation in a swapless system can trigger the kernel OOM killer.
  • Rolf
    Rolf about 6 years
    Thanks, this is reasonable. Nevertheless, I was thinking that if "enough ram" (eg: 8 or 16 GB) is full then it is likely some kind of exceptional situation (application with a memory leak or some other runaway issue, DDOS attack, etc.) in which case even swap would eventually be overwhelmed anyway. Maybe I'm not making a very convincing argument however this should not happen in light-ish desktop use.
  • Ant_222
    Ant_222 over 5 years
    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.
  • dess
    dess over 4 years
    Also, it could be systemctl mask swapfile.swap.
  • Mike Holdsworth
    Mike Holdsworth over 4 years
    What about SSD drives wearing out due to use of swap?
  • shodanshok
    shodanshok over 4 years
    @PeterMortensen while possible, it seems an highly unlikely outcome, especially if using a quality TLC drive.
  • Erik K
    Erik K about 4 years
    Thank you very much. could not work this out for the life of me
  • Ciro Santilli Путлер Капут 六四事
    Ciro Santilli Путлер Капут 六四事 almost 3 years
    Something along sudo sed -i '/\tswap\t/d' /etc/fstab to automate it.
  • Admin
    Admin almost 2 years
    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.
  • Admin
    Admin almost 2 years
    This doesn't solve the problem. Systemctl starts swap anyway even though it's disabled in fstab.
  • Admin
    Admin almost 2 years
    Removing the /etc/fstab isn't sufficient unfortunately. It's been disabled for years on my system, and still every single damn time debian starts up with swap. Systemctl is pissing me off, it just does whatever it wants ignoring the operating system files.