How to add swap space on EC2 Ubuntu 18.04

September 23, 2020 | By Gerald | Filed in: AWS.
Add Swap Space on EC2 Ubuntu

In this tutorial, you will learn how to add swap space on EC2 Ubuntu 18.04 server.

Swap is a space on your server disk drive that is used when the amount of physical RAM memory is full. Your Linux kernel usually uses RAM memory to store the temporary information. When there is no enough RAM space, the Linux kernel takes some of this the information from RAM and write it to the swap space on your disk drive. This is called the swapping process, after that your Linux can release some space of RAM memory and doesn’t crash due to lock of memory.

Upgrading your RAM space on Amazon EC2 instance can cost a lots of money, usually we want to saved some cost from our resources that we used in AWS.

What will you do

  1. Creating a Swap File
  2. Changing a Swap File Permission
  3. Setup Swap Area
  4. Enable the swap file
  5. Mounting the Swap File
  6. Resize the Swap File Size
  7. Removing a Swap File

Requirements

To get started, this guide will show you on how to add swap space on EC2 Ubuntu 18.04 server.

How to check for existing Swap File?

Before you start creating a swap file, you should check your Ubuntu system if it contains an existing swap file, use the command below to verify:

sudo swapon --show

If output is empty, then you can create a new Swap file. Otherwise, If swap file is exist, then you can turn it on or else upgrade the size.

Creating a Swap File

We use a fallocate command to create a swap file of size 1 GB. This utility can be use to preallocate space to, or deallocate space from a file.

sudo fallocate -l 1G /swapfile

If fallocate is not installed, then you can install using:

sudo apt-get install util-linux

If you need help, type command:

fallocate --help

Changing a Swap File Permission

We want to make sure that only root user should be able to write and read the swap file. To set the permission, type command:

sudo chmod 600 /swapfile

Setup Swap Area

Before using the swap partition, you need to set up a Linux swap area, run command:

sudo mkswap /swapfile 

For more information about mkswap utility:

mkswap --help

Enable the swap file

To enable the swap file, type:

sudo swapon /swapfile

Verify the swap is enable using swapon command:

sudo swapon --show

Output:

NAME      TYPE  SIZE   USED PRIO
/swapfile file 1024M 367.5M   -2

You can also run free command to check the memory information.

sudo free -h

Output:

linux free command

If you need help about free command, type:

free --help

Mounting the Swap File

You need to manually mount the swap file at /etc/fstab file to avoid disappearing all changes you made when instances reboot your system.

But before making changes, backup your swap file first incase you missed the configuration, type command:

sudo cp /etc/fstab /etc/fstab.bak

Now begin to modify your fstab file, type command:

sudo vim /etc/fstab

Next, add the following line to the end of /etc/fstab file.

/swapfile swap swap defaults 0 0

Saved and close the file.

Resize the Swap File Size

Optional: If you want to increase the size of the existing swap file by writing 2GB size, here’s the step process you need to follows:

Step 1. Turn off the swap file

sudo swapoff /swapfile

Step 2. Increase the size of the swap file by 2GB, you can use fallocate command to change the size of the file:

sudo fallocate -l 2G /swapfile

Step 3. Make the file usable as swap.

sudo mkswap /swapfile

Step 4. Turn on the swap file.

sudo swapon /swapfile

Step 5. Make sure the swap file is active.

swapon --show

Removing a Swap File

To deactivate and remove the swap file, use the following steps below:

Step 1. Run this command to deactivate the swap file.

sudo swapoff -v /swapfile

Step 2. Remove the swap file entry from the /etc/fstab file.

/swapfile swap swap defaults 0 0

Step 3. Finally, remove the actual swap file using:

sudo rm /swapfile

Reference

If you familiar with swappiness and want to configure it, see: https://askubuntu.com/questions/103915/how-do-i-configure-swappiness

That’s all.

I hope this guide helped you and feel free to comment section below for more suggestions.

SHARE THIS ARTICLE

Tags: , , , , ,

One comment on “How to add swap space on EC2 Ubuntu 18.04

  1. very well explained sir.
    Much appreciated

Leave a Reply

Your email address will not be published. Required fields are marked *