Home Miscellaneous How to add a swap file in Linux

How to add a swap file in Linux

by SupportPRO Admin

Swap space is defined as a temporary storage  that can be used when
machine’s memory requirements exceeds the size of the RAM available.
Usually size of the swap partition is double of RAM memory.
You can add a swap partition or add a swap file. I would recommend you
to add a swap partition, but sometimes isn’t  easy if you do not have
enough free space. This article explains how to add a swap file to a
Linux system.
Determine the size of the new swap file and multiple by 1024 to find
the block size. For example, the block size of a 128 MB swap file is
131072.
Open the shell prompt as root and type the below command.
dd if=/dev/zero of=/swapfile bs=1024 count=131072
Setup the swap file with the following command:
mkswap /swapfile
To facilitate swap file immediately but not automatically at boot time.
swapon /swapfile
Add the below line in /etc/fstab to enable the swap at boot time.
/swapfile swap swap defaults 0 0
It will enable the new swap file when the next time system boots.
Confirm newly added swap file system by checking the output of the
commands cat /proc/swaps or free

If you require help, contact SupportPRO Server Admin

Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value

Leave a Comment