How to add Swap on your Debian/Ubuntu server

Written by
Written on
Tutorial Category Linux
Experience level Beginner

Introduction

A swap file is a space on your server’s hard disk that extends the virtual memory (an extension itself of the server’s real memory). Swap basically gives your server some extra headroom to process memory-intensive workloads by pretending that the machine has more memory than it actually does. This could be beneficial on a cheaper server.

Bear in mind though, Swap is not an instant fix. Swap memory is a lot slower than normal memory due to the underlying hardware (the hard drive) not being designed for this purpose.

Warning:
Using Swap on Virtual Servers with Solid State drives can significantly affect their lifetime due to heavy reads/writes. It is your responsibility to make sure you use these features responsibly. If in doubt, contact your web host for more information.

Check if you need to enable Swap

If Swap is already enabled, you obviously don’t need to do anything. An easy way to check this is to issue the following in the terminal after you’ve logged in via SSH:

$ sudo swapon --show

If nothing is returned, you don’t have any Swap available so you should continue following this guide. Otherwise, you’re all set!


How much Swap do I need?

A good rule of thumb is to take the amount of physical memory your server has and match it or double it. So for example, if my server had 512MB of physical memory, then I could use a 512MB or 1GB swap file to optimise memory usage.


Create the Swapfile

On Debian systems, we use the fallocate command to create a file with a fixed size, and this will become our swapfile.

Say we wanted to go for the 1GB of swap. We will do so by entering the following command, which will create the file in the root directory (/).

$ sudo fallocate -l 1G /.swap

You can check the file was created and allocated with the right size by issuing:

$ ls -lah /

Which should yield (amongst other things):

-rw-r--r--   1 root root 1.0G Jan 21 21:30 .swap

See the manpage for ls for more information on how the ls command works.


Secure the Swapfile

To ensure the integrity of your system, it’s extremely important to make sure only the root user can modify and read the Swapfile. You can enforce this by issuing:

$ sudo chmod 600 /.swap

Which will set the file’s read and write flags to be by root only. You can verify this change by issuing the command from before again:

$ ls -lah /

You will see:

-rw-------   1 root root 1.0G Jan 21 21:30 .swap

See the first few characters? They’ve changed to show that read (r) and write (w) are only available to root. Good! Let’s continue!


Enabling the Swapfile

To tell the operating system that the file is to be swap, it needs to be “flagged”. We can do this by typing:

$ sudo mkswap /.swap

If all went well, you should see something similar to the following:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=e25c4f76-9566-4895-8f77-95cc064c071a

Then, we need to tell the system to actually use it. Type:

$ sudo swapon /.swap

Verify that the swap was activated properly:

$ sudo swapon --show

You should see something like:

NAME TYPE SIZE USED PRIO
/.swap file 1024M 0B -1

If so – you’ve done it! You’ve set up Swap. But, this configuration will not survive a reboot, so I highly recommend making it permanent!


Making your changes permanent

You’re going to do this by adding a value to your /etc/fstab file. This file is a list of the drives (and virtual drives) within your system. Let’s add the new swap file to the end of the fstab file.

Heads up:
It’s always a good idea to back up key system files like fstab before working on them. You can do this by typing:

$ sudo cp /etc/fstab /etc/fstab_bk

If there’s a problem you can retrieve the file by typing:

$ sudo cp /etc/fstab_bk /etc/fstab

Now, add the swap file to the end of fstab by entering:

$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Summary

You have created and enabled your swap space. Enjoy!

%d bloggers like this: