Add Swap Space in Linux

In case you are falling short of swap space on a linux server, follow below steps to add more swap space.

Create swap file

Use linux dd command to create a swap file under root directory (/swapfile1)

below command will create 20gb swap file

# dd if=/dev/zero of=/swapfile1 bs=1024 count=20971520

Note: formula to calculate count = 1024 * (swap size in MB)

  • For 1 GB of swap. count = 1024 * 1024 = 1048576
  • For 2 GB of swap. count = 2048 * 1024 = 2097152
  • For 4 GB of swap. count = 4096 * 1024 = 4194304

Give permissions

chmod 0600 /swapfile1
mkswap /swapfile1
swapon /swapfile1

Add to /etc/fstab

vi /etc/fstab

/swapfile1 swap swap defaults 0 0

Check swap space

cat /proc/swaps

Was this article helpful?

Related Articles

Leave a Comment