Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the primary memory(RAM) is full, inactive pages in memory are moved to the swap space. In this step-by-step tutorial, I am going to create Linux swap file easily.
Steps to Create Linux Swap File
Now, I will create 2GB linux swap file, if you need more you can change the value of 2G.
Step 1: Create a file for swap setup
Create a file for swap using following command.
$ sudo fallocate -l 2G /swapfile
Step 2: Set Permission for Swap File
Set permission for swap file. Only root user should read and write the swap file.
$ sudo chmod 600 /swapfile
Step 3: Set up a Linux swap area
Use mkswap utility for linux swap area.
$ sudo mkswap /swapfile
Step 4: Enable the swap
Make the swap file available for immediate use by adding the swap file to swap space. To make swap available use following command.
$ sudo swapon /swapfile
Step 5: Verify Swap Setup
Verify that the swap setup successful, run following command.
$ sudo swapon -s
Step 6: Enable Swap on startup
To swap enable on startup, edit following file.
$ sudo vi /etc/fstab
Add the following new line at the end of the file, save the file, and then exit
/swapfile swap swap defaults 0 0
In this short step-by-step guide, you have learned to create Linux swap file. If you have any doubts feel free to contact me, I will definitely try to help you. Also, you can join our elite Facebook group to get direct help from me. If you like this tutorial, please share this article on your social media handle.