There are two types of Swap space a user can have –
- Swap partition and,
- Swap file.
While installing Ubuntu, we may get a prompt to create a Swap partition. Apart from that, we can also use a Swap file to extend Swap space. When Physical memory (or, RAM) isn’t able to handle process requests then, inactive pages in RAM move to Swap space. This frees up the RAM and processes function as usual. But, if there was no Swap file/partition then processes would fail.
Follow the article – What is Swap in Ubuntu? to know more about Swap space. Next, we cover how to extend Swap space through Swap file in Ubuntu.
Note: Following operations require you to have superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance.
Swap file to extend Swap space in Ubuntu
Firstly, we need to create a file of defined size to be used as Swap file. For instance, to create a 1GB file we can issue the following command –
sudo dd if=/dev/zero of=/swap1G.img bs=1M count=1024
Next, we set file permissions so that regular users don’t get read access –
sudo chmod 600 /swap1G.img
Now, to setup Swap on the file – we use mkswap command-line utility.
sudo mkswap /swap1G.img
To enable file for swapping, use swapon command-line utility –
sudo swapon /swap1G.img
Use the following code to see the changes –
At this stage, if we don’t make relevant entries in the /etc/fstab file then, all the changes we have made will be lost at next boot. So, use a text-editor to edit /etc/fstab. We have used nano text-editor –
sudo nano /etc/fstab
And, append the file with –
/swap1G.img swap swap defaults 0 0
Save and exit.
In conclusion, we have covered how to extend Swap space through Swap file here. Next article would be about removing the Swap file if it isn’t required.