Swap memory is a virtual memory. When we have low server configuration and our system is running out of memory then to reduce cost and increase performance we need swap memory. By default swap is not set-up on any AWS or other server.

So to setup swap below commands must be consider.

1. Use command free -mh to check we have swap already enabled or not

You can see there is no memory available.

Now the further process start

Use df -h command to check we have enough space to create swap.

Create a swap file using the following command

sudo /bin/dd if=/dev/zero of=/var/rpswap bs=1M count=2048

Now we can mark the file to swap by the following command:
sudo /sbin/mkswap /var/rpswap

File must be accessible to root user only use :
sudo chmod 600 /var/rpswap

To enable the swap to work
sudo /sbin/swapon /var/rpswap

To make this swap partition permanently in the system add below code in /etc/fstab
/var/rpswap swap swap defaults 0 0

Now the final step to make swap ready to use
sudo swapon -a

Now restart the system by init 6 and check again free -mh


You can see now swap is available for use.