In this tutorial we will cover how to create a Swap File on CentOS 7 / RHEL 7
Creating the Swap File
– To create a swap file we can use fallocate or dd, for this tutorial we will create a file called swapfile in the root (/) directory with 100 MB of size.
# sudo fallocate -l 100M /swapfile
or you can use dd command
# sudo dd if=/dev/zero of=/swapfile bs=1024 count=102400
Enabling The Swap File
– Adjusting the permissions on the swap file:
# sudo chmod 600 /swapfile
– Setting up the swap file:
# sudo mkswap /swapfile
– Enable the swap file immediately:
# sudo swapon /swapfile
– Check the total system swap space:
# sudo swapon -s
Enable the swap file automatically at boot time
The Swap File is enabled at the moment, but when we reboot, the Swap File will be not automatically enabled for the use, for that reason we will add some entry to the /etc/fstab file to enable it automatically at boot time
– Open the /etc/fstab file and add this line in the end:
/swapfile swap swap defaults 0 0
– you can verify it is enabled by viewing the output of the command cat /proc/swaps or free
# cat /proc/swaps # free -h
We hope this tutorial was enough Helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!
PS. If you like this post please share it with your friends on the social networks using the buttons below.Thanks.