Skip to content

How to create a Swap partition on your EC2 Instance

How to create a Swap partition on EC2 Instance. how to add Swap partition on Amazon EC2 Linux Instances. Enable swap space in Linux instance

Covers
AWS, Disk Management, Swap

Creating a swap partition on an Amazon Elastic Compute Cloud (EC2) instance can help improve the performance and stability of your system. A swap partition is a space on the hard drive that can be used by the operating system as virtual memory when the physical memory (RAM) is running low.

In this article, we will explain how to create a swap partition on your EC2 instance.

  1. Check if your EC2 instance has a swap partition
  2. Before creating a swap partition, you should check if your EC2 instance already has one. To do this, you can run the following command in the terminal:

    sudo swapon -s 
    

    If you see output that lists a swap file or partition, then your instance already has a swap partition.

  3. Determine the amount of swap space you need
  4. The amount of swap space you need depends on the amount of physical memory (RAM) on your EC2 instance and the amount of memory-intensive applications you plan to run. As a general rule, the swap space should be at least equal to the amount of RAM on the instance.

    To determine the amount of RAM on your instance, you can run the following command in the terminal:

    free -m 
    

    This will display the amount of memory (in megabytes) on your EC2 instance. To calculate the amount of swap space you need, simply multiply the amount of RAM by 2.

  5. Create a swap file
  6. To create a swap file on your EC2 instance, you can use the fallocate command. The following command will create a 2 GB swap file named /swapfile:

    sudo fallocate -l 2G /swapfile 
    

    If the fallocate command is not available on your instance, you can use the dd command instead:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152 
    

    This command will also create a 2 GB swap file named /swapfile.

  7. Set the correct permissions on the swap file
  8. The swap file should only be accessible by the root user. You can set the correct permissions using the following command:

    sudo chmod 600 /swapfile 
    

  9. Set up the swap space
  10. To set up the swap space, you need to use the mkswap command. The following command will set up the /swapfile as swap space:

    sudo mkswap /swapfile 
    

  11. Enable the swap space
  12. To enable the swap space, you can use the swapon command. The following command will enable the /swapfile swap space:

    sudo swapon /swapfile 
    

  13. Make the swap space permanent
  14. By default, the swap file will not be enabled on system reboot. To make the swap space permanent, you need to add an entry to the /etc/fstab file. Open the file with a text editor:

    sudo nano /etc/fstab 
    

    Add the following line at the end of the file:

    Save and exit the file. The swap file will now be enabled on system reboot.

Conclusion

That’s it! You have now created a swap partition on your EC2 instance. This should help improve the performance and stability of your system, especially when running memory-intensive applications.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

7 Comments

  1. on running sudo swapon /var/myswap
    I am getting below error
    swapon: /var/myswap: swapon failed: Device or resource busy

  2. on running sudo swapon /var/myswap
    I am getting
    swapon: /var/myswap: swapon failed: Device or resource busy

Leave a Reply

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


Back to top