Skip to main content
How To Add Swap Space on your Linux System

How to Add Swap on AWS EC2 Instance

Swap Space in Linux is used when the amount of Physical Memory is full. It is useful for systems having less memory (RAM). If the system needs more memory resources and the Physical Memory (RAM) is full, inactive pages in memory are moved to the swap space. Swap space is located on hard drives and so it has a slower access time than physical memory (RAM). Due to this reason, it should not be considered as a replacement for more RAM. This article will help you to enable swap on AWS EC2 Instance.

How to Add Swap on AWS EC2 Instance?

First, check if the server has any swap configured on it by using the swapon utility. You can use the -s flag to display a summary of swap usage and availability on your server.

swapon -s

If nothing is returned, then we can ensure that no swap file exists on the server.

You can also check the swap with the free utility. It shows the overall memory usage of the server.

free -m

The typical way of allocating space for swap is to use a dedicated partition. However, altering the partition scheme is not always possible due to hardware or software constraints. In this scenario, we can easily create a swap file that resides on the existing partition with the help of dd or fallocate utilities.

Calculate Swap Space Size

The general rule is that the swap space should be equal to 2X the physical RAM, for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB. Also, Swap space should never be less than 32 MB.

Amount of system RAMRecommended swap space
<= 2GB2x the amount of RAM but never less than 32 MB
> 2GB and < 32GB4 GB + (RAM – 2 GB)
32 GB of RAM or more 1x the amount of RAM

Check the available storage space of the server:

Use the command df to check the available disk space of the server.

df -h

If the server has enough free disk space, please proceed further.

Create a Swap File

As mentioned earlier, you can use either dd utility or fallocate utility to create the swap file for your server.

Here, I am creating a 2GB Swapfile on the root file system.

Creating swapfile using dd utility:

dd if=/dev/zero of=/swapfile bs=1M count=2048

Where, bs = block size and count = Number of blocks

Creating swapfile using fallocate utility:

fallocate --length 2G /swapfile

Next, update the permissions of the created swapfile using chmod command:

chmod 600 /swapfile

Now, convert that file in to a swap area using mkswap command:

mkswap /swapfile

Finally enable swap using swapon utility:

swapon /swapfile

Verify that the procedure was successful:

swapon -s

Make this swap setting persist by adding following line in /etc/fstab file:

/swapfile swap swap defaults 0 0

So, the next time the system boots up, it enables the new swap file.

You can verify if it is enabled or not by viewing the output of the command free or cat /proc/swaps

That’s it. We have added the Swap on AWS EC2 instance.

Also Read:

Advantages of Swap Space

Setup OpenVZ 7 on CentOS 7

Find the Inode usage of your system

Check the Exit status of a command

How to Add Swap on AWS EC2 Instance
5 (100%) 1 vote[s]


TheGeeksAlive is all about Linux, Server Administration, Web Hosting and other Open Source Technologies.