Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»AWS»How to create a Swap partition on your EC2 Instance

    How to create a Swap partition on your EC2 Instance

    By RahulFebruary 14, 20233 Mins Read

    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.

    Advertisement

    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:

      1
      /swapfile swap swap defaults 0 0

      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.

    amazon ec2 linux swap
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Create AWS Cognito User with AWSCLI

    How to Create User Manually in AWS Cognito (CLI)

    Know about the /etc/nsswitch.conf file

    What is the /etc/nsswitch.conf file in Linux

    How To Add Swap Space on Debian 11 (Bullseye)

    How To Add Swap Space on Debian 11

    View 7 Comments

    7 Comments

    1. NANDAN on October 25, 2020 5:57 pm

      Hi Rahul,

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

      Thanks bro.

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

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

      Reply
    3. Venkat on March 3, 2016 10:59 pm

      Very useful and informative article Rahul 🙂

      Cheers,
      Venkat

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

      hi Rahul,

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

      Regards,
      Rahul Janghel.

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

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

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

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

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

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

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.