100

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.

user283167
  • 1,003

8 Answers8

140
  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

Aaron Copley
  • 12,954
21

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:

  • Run systemctl list-unit-files to find the responsible swap unit (Source)

  • Mask it:

    systemctl mask <SWAP_UNIT_FILE>
    

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

glacier
  • 123
Gregor
  • 601
14

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
Tom Klino
  • 671
shodanshok
  • 52,255
8

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

2

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.

piotao
  • 121
1

While it doesn't directly solve the author's issue, the comments have evolved into a useful compilation of methods to eliminate swap in various scenarios. As a contribution, I've included one more solution.

I once encountered an issue with systemd attempting to mount encrypted swap partitions during the early boot stage, even though these partitions were not specified in any configuration files like fstab, crypttab or systemd units. I'm not sure about how systemd identified these swap partitions, especially considering they were encrypted with LUKS on LVM.

Regardless, the system couldn't boot because it was waiting for these partitions to become ready. The operating system was installed on a USB flash drive, and the swap partitions were unrelated to it but belonged to the host system.

The only solution that worked was disabling systemd.swap for systemd-gpt-auto-generator by modifying the kernel options in /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="systemd.swap=0 ...."
petr
  • 111
0

I know this is probably not the right answer for THIS question, but to be complete: If you just want to prevent starting swapping on SOME devices at boot, without removing them from fstab, you can add noauto as a flag after sw (sw,noauto).

Camion
  • 97
0

When I view the /etc/fstab file on Raspbian I see a comment saying

a swapfile is not a swap partition, no line here
  use dphys-swapfile swap[on|off] for that

But to completely disable the preconfigured swap file this works perfectly fine:

swapoff -a
chmod -x /etc/init.d/dphys-swapfile
reboot

This solution looks a bit quick and dirty to me, however, you can simply reenable it with:

chmod +x /etc/init.d/dphys-swapfile
reboot
Ilka
  • 1