• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Add Swap on AWS/EC2 Linux Instance

Written by Rahul, Updated on May 29, 2020

Swap space is useful for systems having less memory (RAM). If your system facing the problem of lack of memory continuously and you don’t want to increase memory on the server, Then it can be helpful to enable swap in your system. Swap is comparatively much slower than physical memory but the operating system uses swap space in case system goes out of memory. To know more about working of swap visit here.

This article will help you to enable swap filesystem in your running instance. There are two methods to add swap in Amazon ec2 Linux instances. This article has been tested with CentOS 6.9, but it will work on most of Linux distributions.

Method 1 – Using Swap File

This option is helpful if we don’t want to add extra disks in our systems, In this, we simply create a file in our current file system and make it type swap, which can be used as swap in our system. Use the following commands to create and enable swap on our system.

sudo dd if=/dev/zero of=/var/myswap bs=1M count=4096
sudo mkswap /var/myswap
sudo swapon /var/myswap

bs=1M count=4096 means it will create 4 GB of swap file, You may change as per you need. After enabling swap we can see that our system has swap enabled by running “free -m” command.

To make it enable on system boot, simply edit /etc/fstab file and add following entry at end of file. My fstab file looks like below after adding the swap entry.

 cat /etc/fstab

LABEL=/        /           ext4    defaults,relatime  1   1
tmpfs          /dev/shm    tmpfs   defaults        0 0
devpts         /dev/pts    devpts  gid=5,mode=620  0 0
sysfs          /sys        sysfs   defaults        0 0
proc           /proc       proc    defaults        0 0
/var/myswap    swap        swap    defaults        0 0

Method 2 – Using Additional Disk for Swap

This option is helpful if you do not have enough space in our current drives mounted in the system. In this option, first, we need to add extra disk in our system first. In my case new disk mounted as /dev/xvdd (It may change in your case)

sudo mkswap -f /dev/xvdd
sudo swapon /dev/xvdd

To make it enable on system boot, simply edit /etc/fstab file and add following entry at end of file. After adding swap entry, the

 cat /etc/fstab

LABEL=/        /           ext4    defaults,relatime  1   1
tmpfs          /dev/shm    tmpfs   defaults        0 0
devpts         /dev/pts    devpts  gid=5,mode=620  0 0
sysfs          /sys        sysfs   defaults        0 0
proc           /proc       proc    defaults        0 0
/dev/xvdd      swap        swap    defaults        0 0

Conclusion

In this tutorial you have learned to create and enable Swap memory on EC2 Linux instance.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

7 Comments

  1. Avatar NANDAN Reply
    October 25, 2020 at 5:57 pm

    Hi Rahul,

    Very useful one, it really helped me in creating swap.

    Thanks bro.

  2. Avatar Ankit Arora Reply
    January 4, 2018 at 9:48 am

    Can please also share how to revert the above process(Deleting swap)?

  3. Avatar Venkat Reply
    March 3, 2016 at 10:59 pm

    Very useful and informative article Rahul 🙂

    Cheers,
    Venkat

  4. Avatar Rahul Janghel Reply
    April 6, 2015 at 7:45 am

    hi Rahul,

    Pls update on “EPLY TO ANKIT January 27, 2015 at 12:40 pm”

    Regards,
    Rahul Janghel.

  5. Avatar ankit agrawal Reply
    January 27, 2015 at 12:40 pm

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

  6. Avatar ankit agrawal Reply
    January 27, 2015 at 12:39 pm

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

  7. Avatar Mausam Devolia Reply
    January 8, 2015 at 9:12 am

    It really helped me, to get rid of my nagios problem.
    Thnx buddy

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How To Install Python 3.9 on Ubuntu 18.04 0
  • How to Use AppImage on Linux (Beginner Guide) 2
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy