Add swap on CentOS and Ubuntu Linux

1. Create a swap file.

dd if=/dev/zero of=/mnt/swapfile bs=1024 count=4096k


This will create a 4GB of swap file which can be used for 8GB server.

2. Change the file to swap partition.

mkswap /mnt/swapfile

3. Enable swap

swapon /mnt/swapfile


4. Verify swap partition.

swapon -s


NOTE: All the above operations need to be done with a sudo user or by root.