171

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)

4
  • 3
    Even if you have a lot of RAM, it's not the best idea to completely work without swap (see for instance alexonlinux.com/swap-vs-no-swap ). Depending on your requirements you might consider putting the swap partition on a dm-crypt secured device, so that everything written to it will be encrypted. You have to make sure in such a case, that swap is enabled after encrypted volumes get available, and disabled before the device mapping is disabled, of course (I think that's default on Ubuntu, but I'm not sure). Just search the web for "encrypted swap" to find numerous guides.
    – soulsource
    May 13, 2014 at 9:01
  • 2
    Apropos: askubuntu.com/questions/248158/… You don't need swap until you need it. And then you really need it.
    – user459652
    Oct 28, 2015 at 19:19
  • What about a cryptswap ? /dev/mapper/cryptswap1
    – user503925
    Feb 10, 2016 at 4:20
  • similar question on serverfault: serverfault.com/q/684771
    – drammock
    Apr 20, 2020 at 20:35

8 Answers 8

225

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.

6
  • 19
    This doesn't seem to persist across a reboot?
    – ebyrob
    Apr 20, 2013 at 2:31
  • 41
    Permanent changes to swap are usually made in /etc/fstab.
    – ubfan1
    Apr 20, 2013 at 5:51
  • Do we just copy this into /etc/fstab
    – Max
    Mar 5, 2016 at 23:23
  • 17
    No. Instead, you comment out the swap line in /etc/fstab. Jan 3, 2018 at 7:48
  • You can read in other answers how to comment it out.
    – Carolus
    Jan 22, 2018 at 14:46
57

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

4
  • 2
    Why does your quoted UUID have some concrete digits? Should they be identical for all UUIDs? My partitions' UUIDs don't have any coincidence with your specified digits.
    – Ruslan
    May 31, 2016 at 17:51
  • 5
    Your sed commands don't work in case the fields are separated by tabs (not spaces). It is better written as sed -ri '/\sswap\s/s/^#?/#/' /etc/fstab PS: I also added #? to prevent additional changes to /etc/fstab when the line is already disabled.
    – Alek_A
    May 11, 2018 at 21:34
  • How does this address sensitive data that may already exist on disk due to previously enabled swap? I think this is the OPs main concern based on the question. See my answer here.
    – Elder Geek
    Dec 14, 2019 at 18:51
  • 1
    I like this the best. Apr 2, 2021 at 5:06
19
sudo swapoff -a

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:

  1. Open fstab file, type sudo gedit /etc/fstab in terminal.

File's contents would look like this:

proc            /proc           proc    nodev,noexec,nosuid 0       0
/host/ubuntu/disks/root.disk /               ext4    loop,errors=remount-ro 0       1
/host/ubuntu/disks/swap.disk none            swap    loop,sw         0       0
#/dev/sda10 /media/ASD  vfat    defaults    0   0
#/dev/sda1  /media/98   vfat    defaults    0   0
  1. Just add hash (#) to the beginning of the swap partition line, so the line looks as:

#/host/ubuntu/disks/swap.disk none swap loop,sw 0 0

  1. Reboot your PC
1
  • 1
    no need to reboot ... ubuntu 20.04 it happens live to either define a new swap or remove swap Feb 1, 2021 at 19:19
11

It may be lame solution, but I used it often. You can simply type

sudo -s
crontab -e

And add

@reboot sudo swapoff -a  

So, it will be disabled automatically on boot.

5
  • 22
    I hope it goes without saying that this is a very poor method. Why create a boot-time task to turn off another option, when there's another file that can be edited to stop it being turned on in the first place Oct 29, 2015 at 18:18
  • 5
    Perhaps not as aesthetically pleasing, but there's nothing poor about it. If you're already very used to using crontab then this solution will roll of the fingers a bit easier and make more sense than simply blindly copying what other user on S.E. told you to do to /etc/fstab. And, you'll still remember how to do it to a new computer without having to run to google again. I happen to barely even know what crontab is and am much more familiar with disk partitions anyway but the logic still stands. Aug 3, 2019 at 5:26
  • 2
    Actually, this might be the better solution. Because /etc/fstab suggestions only work for swap partitions. This solution applies to linux setups regardless of whether or not they use a partition or a swapfile. Aug 3, 2019 at 5:30
  • 4
    @NicholasPipitone /etc/fstab solutions also work for swap files which are also mounted in /etc/fstab. See my answer here.
    – Elder Geek
    Dec 14, 2019 at 18:44
  • None of the fstab options listed worked for me. This option suffices until I discover what is keeping swap "alive" on my system.
    – James
    Mar 25 at 13:25
8

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

2

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:

Enter image description here

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

3
  • 1
    Do you think that this answers the users concern about having sensitive data on the swap partition? Can you tell us why?
    – Elder Geek
    May 9, 2014 at 2:33
  • 1
    The user's main point was asking how to disable swap. If you have sensitive data, you should be using pre-boot encryption ideally.
    – user170544
    May 9, 2014 at 3:28
  • 1
    I respectfully disagree. When I read the question the first sentence I come across is "I have some sensitive data in ram that i prefer not to be on disk." I believe this was his main concern which is why he started with it. Regardless, I believe I have covered every aspect of his question in my answer. Consider it a helpful example.
    – Elder Geek
    May 9, 2014 at 11:11
1

Replace the defaults with sw,noauto in the line which consists of the swap entry in the file /etc/fstab:

/dev/mapper/centos-swap swap  swap    sw,noauto        0 0

(The path /dev/mapper/centos-swap is probably different for you).

Now checkout the output after reboot, your swap never get mounted:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           3791         100        3408           8         282        3483
**Swap:             0           0           0**
0

Disable Swap

Run the following command to disable Swap:

sudo swapoff -a

Now remove the Swap file:

sudo rm /swap.img

It is safe and does not require a reboot.

You must log in to answer this question.