How do I disable swap?

Using

sudo swapoff -a  

is the usual way to turn off swap, with the swapon -a command used to turn it back on. See man swapoff for more information about turning off swap for explicit devices.


You may disable swap after reboot by just commenting out (add # in front of the line) the swap entry in the /etc/fstab file. It will prevent the swap partition from automatically mounting after a reboot. To do this in a single command:

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

Or simply:

sudo sed -i '/ swap / s/^/#/' /etc/fstab

Now your swap entry on /etc/fstab will look similar to this:

#UUID=xxxxxxxx-xxxx-xxxxx-xxx-xxxxxxxxxxx none            swap    sw              0       0

With your own specific numbers and lower case letters instead of the letters "x".


If you're concerned about the content of the swap, you can always turn it off as specified in the usual way with sudo swapoff -a and then fill the swap device with zeros or random data using dd.

First use the content of fstab to find your swapfile or device (less /etc/fstab).

Having located it and double and triple checked its location at say sda5 or /swapfile (swap partitions were replaced by default with a swapfile in Ubuntu 17.04 (Zesty Zapus) and beyond).

In the case of a swap partition or drive (prefaced with /dev): issue the "disk destroyer" command (not to be used lightly),

sudo dd if=/dev/zero of=swap, replacing the word swap with the swap device or file you located in /etc/fstab

to blast it full of zeros or

sudo dd if=/dev/random of=swap again replacing the word swap with the swap device or file you located in /etc/fstab

to blast it full of random data.

In the case of a swapfile (prefaced with only a path):, you can simply delete the file with sudo rm /path/to/swapfile, but it's better to just fill it with garbage as described above, so that the next time you turn swap on with

swapon -a

the system will happily use it again. If you have plenty of RAM you may not need swap at all. Issue the command

free

when the system is under heavy load and see how much is in use to make this determination.

If you determine that you permanently don't need swap (for hibernation or anything else) you can simply comment out that line in fstab as suggested here.

free

Tags:

Swap