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)
Using
is the usual way to turn off swap, with the
swapon -a
command used to turn it back on. Seeman 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:Or simply:
Now your swap entry on
/etc/fstab
will look similar to this:With your own specific numbers and lower case letters instead of the letters "x".
Above command disables swap only for a current session, you need to comment out swap partition in /etc/fstab file. To do this you just need to add "#" (comment line) to the beginning of swap line. Steps are as:
sudo gedit /etc/fstab
in terminal.File's contents would look like this:
#/host/ubuntu/disks/swap.disk none swap loop,sw 0 0
It may be lame solution, but I used it often. You can simply type
And add
So, it will be disabled automatically on boot.
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 withthe system will happily use it again. If you have plenty of RAM you may not need swap at all. Issue the command
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.
Go into a terminal and execute this command:
gnome-disks
. It is not installed in all distributions. If so, follow the instructions printed to install it and try again. Look for a device with the word 'swap' on the left pane. It usually has a size the same as your physical memory. Click the square button to disable it. Authenticate this change with your root password.Screenshot:
In the System Monitor application (gnome-system-monitor), you will see it reports the swap area as 'not available'. The swap area is disabled, but not deleted. You can enable it easily again in the future. Deleting the swap space has caused my system no harm, but you will create more work for yourself if you do decide you want it back (hibernation) in the future.
To prevent it from mounting at startup, you should go into the "Edit Mount Options..." in gnome-disks and uncheck "Mount at startup".
Replace the
defaults
withsw,noauto
in the line which consists of the swap entry in the file/etc/fstab
:(The path
/dev/mapper/centos-swap
is probably different for you).Now checkout the output after reboot, your swap never get mounted: