Before creating a swap file, we should check the size of the current swap:
#cat /proc/meminfo | grep SwapTotal
SwapTotal: 9233400 kB
Create a swap file
To create swap file, you will need to use dd command:
#dd if=/dev/zero of=/newswap bs=1024 count=1048576
if=/dev/zero - read from /dev/zero
of=/newswap - write to /newswap
bs=1024 - blocksize in bytes for reading and writing
count=1048576 - swap file size
Create swap area
Type the following command to set up a Linux swap area in a file.
#mkswap /newswap
Set permissions for the file, so only the root user can use it:
#chmod 0600 /newswap
#chown root:root /newswap
Acticate swap
Next, we should activate the swap file:
#swapon /newswap
At the end, we want the new swap file to be active after reboot and we should add it in fstab file:
#nano /etc/fstab
add the following line at the end of the file:
/newswap swap swap defaults 0 0
Now check if swap is activated or not:
#cat /proc/meminfo | grep SwapTotal
or
#free -m