How do I disable swap?

I have some sensitive data in RAM that I prefer not to be on disk. How do I disable swap?

I have more than enough RAM. If RAM consumption gets too high I have no problems with processes being terminated. How do I disable swap?

Note: I do not have a swap partition and this is running in a VM (VMware)


Solution 1:

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.

Solution 2:

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