How to Add Swap to a Linux Instance on AWS/EC2 [ Easy Guide ]
How to Add Swap space is useful for systems with limited memory (RAM). If your system is always running out of memory and you don’t want to increase the server’s RAM, enabling swap in your system may be helpful. Although swap is far slower than physical memory, the operating system employs it in the event that the system runs out of memory. Visit this page to learn more about how swaps operate.
Table of Contents
How to Add Swap
This article will guide you through the process of enabling a swap file system in a running instance. In Amazon ec2 Linux instances, there are two ways to add swap. Although this article was tested on CentOS 6.9, it should work on other Linux systems as well.
Enable Swap Using A File
This method is useful if we don’t want to add additional discs to our systems. We just create a file in our present file system with the type swap, which can then be utilized as a swap in our system. To construct and enable swap on our system, use the instructions below.
sudo dd if=/dev/zero of=/var/myswap bs=1M count=4096 sudo mkswap /var/myswap sudo swapon /var/myswap
bs=1M count=4096 implies it will create a swap file of 4 GB. You can alter this as needed. By using the “free -m” command after enabling swap, we can verify that our system has swap enabled.
Simply modify the /etc/fstab file and add the following entry at the end to enable it on system boot. After adding the swap entry to my fstab file, it looks like this.
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
Enable Swap Using Additional Disc
If you don’t have enough space on the system’s current discs, this option is useful. In order to use this option, we must first add additional disc space to our system. The new disc is mounted
as /dev/xvdd in my case (It may change in your case)
sudo mkswap -f /dev/xvdd sudo swapon /dev/xvdd
Simply edit the
/etc/fstab file and add the following entry at the end to enable it on system boot. After you’ve added a 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 /dev/xvdd swap swap defaults 0 0
You learned how to create and enable Swap memory on an EC2 Linux instance in this tutorial.
How to Spend a Weekend in Los Angeles
Los Angeles has it all: sunshine, beaches, spunky speakeasies, and Hollywood stars! Even if you only have a weekend in…