Linux Swap Basics

I was recently asked about the Linux swap system, and promised a recap here on the blog.  The inquiry was regarding a newly installed Ubuntu on a dual-booting PC, which, by default, created swap space on the primary SSD.  The user realized this was a generally “bad” idea, and asked me how to move the Linux swap partition onto the system’s spare eSATA drive.

Keep in mind that, if you never swap, it is perfectly safe to use an SSD for swap.  And even if you do use a lot of swap, you can use SSD as your swap.  However, if you are going to use SSD for swap, and know that it will be utilized by your system, make your swap partition on a separate physical SSD from the main SSD that your OS is on.  That way, when you have thrashed it to physical death, you can simply replace the seconday SSD without having to replace and reinstall your primary SSD.

Ultimately, I recommend against that entirely, and prefer to use traditional hard drives for swap, and any activity that involves repeat read/write.  Long story short, heavy swapping on an SSD means a shorter life for that more expensive storage, and the time wasted on buying and replacing hardware is even more valuable.  And… on with the show!

Linux Swap Basics:

  • View swap usage using top or free.
  • Examine what swap partitions are in use with swapon -scat /proc/swaps, or systemctl –type swap
  • The file /etc/fstab is used to auto-mount partitions, including swap.

Creating Swap:

There are dozens of tutorials online if you need a step by step.  However, the command line method is to use fdisk to create a partition, then mkswap to format the new partition as a swap partition.  If you are running a window manager, you can go to settings, disks and create the new swap partition with a few clicks.

“Swapping” Swap:

First, a simple script to turn off swap “A”, and turn on swap “B” (must run as root).

swapoff /dev/sda3
swapon /dev/sdb3

The above is a temporary solution, turning off swap on one drive, and turning swap on (using another drive).  If you need more swap, you could use both by simply turning the second swap on.

If you want to permanently swap which swap you use on boot, do the following:

  • Run blkid (as root/sudo) and find the UUID of the swap partition you want to use.
  • Edit /etc/fstab and change the UUID of the line that references swap.
  • If you reboot at this point, on some systems [such as Ubuntu], you will notice that you are now using both old and new swap partitions.  You need to tell your system to disable the old swap partition, using the next two steps:
  • systemctl stop dev-sda3.swap
  • systemctl mask dev-sda3.swap

Now, when you reboot and check things out, you will see that the system is only using the newly created swap partition.

Swapping To File:

I don’t use this often but it did come in handy recently when using an underpowered VM to do some quick and dirty work.  Long story short, I was using the wrong tool for the job because that was all that was available in the critical moment.  Long story short, it was an issue of PHP running out of memory.  Rather than install “the right tool for the job” or build a more powerful VM, I quickly built a swap file and was able to run the job and move on.  This is old hat, and great in a pinch.  Here is what you do:

  • Create a zero-filled file (the following example creates a 1GB file):
  • # dd  if=/dev/zero of=/.swapfile bs=1024 count=1M
  • Format this new swapfile as you would an actual partition, so that it becomes a swap filesystem:
  • # mkswap /.swapfile
  • “Turn it on”:
  • # swapon /.swapfile
  • Verify it is on:
  • # swapon -s