venket's b log

venket's b log

How to enable SWAP space on AWS Ubuntu or any  linux Servers.

How to enable SWAP space on AWS Ubuntu or any linux Servers.

·Jul 30, 2021·

2 min read

Play this article

Swap memory is When physical RAM is already in use, Amazon EC2 instances use swap space as a short-term replacement for physical RAM.

In Cloud Most and main thing is cost. so we all are taking minimal rented servers. So the memory amount is not enough for our application.

Here SWAP Space coming to the picture.

With out adding more Memory we will change our disk as memory.

so we will create swap space for memory purpose.

Create a swap file

Use the dd command to create a swap file on the root file system.

  • In the command, bs is the block size and count is the number of blocks.

  • The size of the swap file is the block size option multiplied by the count option in the dd command.

  • Adjust these values to determine the desired swap file size.
  • The block size you specify should be less than the available memory on the instance or you receive a "memory exhausted" error.

In this example dd command, the swap file is 4 GB (128 MB x 32):

 sudo dd if=/dev/zero of=/swapfile bs=128M count=32
  • Update the read and write permissions for the swap file:
 sudo chmod 600 /swapfile
  • Set up a Linux swap area:
sudo mkswap /swapfile
  • Make the swap file available for immediate use by adding the swap file to swap space:
sudo swapon /swapfile
  • Verify that the procedure was successful:
sudo swapon -s
  • Enable the swap file at boot time by editing the /etc/fstab file.

Open the file in the editor:

sudo vi /etc/fstab

Output : Amazon Linux2 image.png

Output: Ubuntu


Add the following new line at the end of the file, save the file, and then exit:

/swapfile swap swap defaults 0 0

Output: Amazon Linux 2


Output: Ubuntu


Now check the below command to verify the swap file size.

ls -la /swapfile

See 4GB Size of swap file created.

Output: Amazon Linux 2


Output: Ubuntu


After that restart the server

sudo init 6

Did you find this article valuable?

Support Venketraman by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this