How to Create Swap File on Linux [Beginner's Tutorial] – It's FOSS

It's FOSS
A Linux and Open Source Web Portal
Last updated By 57 Comments
This tutorial discusses the concept of swap file in Linux, why it is used and its advantages over the traditional swap partition. You’ll learn how to create swap file or resize it.
A swap file allows Linux to simulate the disk space as RAM. When your system starts running out of RAM, it uses the swap space to and swaps some content of the RAM on to the disk space. This frees up the RAM to serve more important processes. When the RAM is free again, it swaps back the data from the disk. I recommend reading this article to learn more about swap on Linux.
Traditionally, swap space is used as a separate partition on the disk. When you install Linux, you create a separate partition just for swap. But this trend has changed in the recent years.
With swap file, you don’t need a separate partition anymore. You create a file under root and tell your system to use it as the swap space.
With dedicated swap partition, resizing the swap space is a nightmare and an impossible task in many cases. But with swap files, you can resize them as you like.
Recent versions of Ubuntu and some other Linux distributions have started using the swap file by default. Even if you don’t create a swap partition, Ubuntu creates a swap file of around 1 GB on its own.
Let’s see some more on swap files.
Before you go and start adding swap space, it would be a good idea to check whether you have swap space already available in your system.
You can check it with the free command in Linux. In my case, my Dell XPS has 14GB of swap.
The free command gives you the size of the swap space but it doesn’t tell you if it’s a real swap partition or a swap file. The swapon command is better in this regard.
As you can see, I have 14.9 GB of swap space and it’s on a separate partition. If it was a swap file, the type would have been file instead of partition.
If you don’ have a swap space on your system, it should show something like this:
The swapon command won’t show any output.
If your system doesn’t have swap space or if you think the swap space is not adequate enough, you can create swap file on Linux. You can create multiple swap files as well.
Let’s see how to create swap file on Linux. I am using Ubuntu 18.04 in this tutorial but it should work on other Linux distributions as well.
First thing first, create a file with the size of swap space you want. Let’s say that I want to add 1 GB of swap space to my system. Use the fallocate command to create a file of size 1 GB.
It is recommended to allow only root to read and write to the swap file. You’ll even see warning like “insecure permissions 0644, 0600 suggested” when you try to use this file for swap area.
Do note that the name of the swap file could be anything. If you need multiple swap spaces, you can give it any appropriate name like swap_file_1, swap_file_2 etc. It’s just a file with a predefined size.
Your need to tell the Linux system that this file will be used as swap space. You can do that with mkswap tool.
You should see an output like this:
Now your system knows that the file swapfile can be used as swap space. But it is not done yet. You need to enable the swap file so that your system can start using this file as swap.
Now if you check the swap space, you should see that your Linux system recognizes and uses it as the swap area:
Whatever you have done so far is temporary. Reboot your system and all the changes will disappear.
You can make the changes permanent by adding the newly created swap file to /etc/fstab file.
It’s always a good idea to make a backup before you make any changes to the /etc/fstab file.
Now you can add the following line to the end of /etc/fstab file:
You can do it manually using a command line text editor or you just use the following command:
Now you have everything in place. Your swap file will be used even after you reboot your Linux system.
The swappiness parameters determines how often the swap space should be used. The swappiness value ranges from 0 to 100. Higher value means the swap space will be used more frequently.
The default swappiness in Ubuntu desktop is 60 while in server it is 1. You can check the swappiness with the following command:
Why servers should use a low swappiness? Because swap is slower than RAM and for a better performance, the RAM should be utilized as much as possible. On servers, the performance factor is crucial and hence the swappinness is as low as possible.
You can change the swappiness on the fly using the following systemd command:
This change it only temporary though. If you want to make it permanent, you can edit the /etc/sysctl.conf file and add the swappiness value in the end of the file:
There are a couple of ways you can resize the swap space on Linux. But before you see that, you should learn a few things around it.
When you ask your system to stop using a swap file for swap area, it transfers all the data (pages to be precise) back to RAM. So you should have enough free RAM before you swap off.
This is why a good practice is to create and enable another temporary swap file. This way, when you swap off the original swap area, your system will use the temporary swap file. Now you can resize the original swap space. You can manually remove the temporary swap file or leave it as it is and it will be automatically deleted on the next boot.
If you have enough free RAM or if you created a temporary swap space, swapoff your original file.
Now you can use fallocate command to change the size of the file. Let’s say, you change it to 2 GB in size:
Now mark the file as swap space again:
And turn the swap on again:
You may also choose to have multiple swap files at the same time.
You may have your reasons for not using swap file on Linux. If you want to remove it, the process is similar to what you just saw in resizing the swap.
First, make sure that you have enough free RAM. Now swap off the file:
The next step is to remove the respective entry from the /etc/fstab file.
And in the end, you can remove the file to free up the space:
Do you swap?
I think you now have a good understanding of swap file concept in Linux. You can now easily create swap file or resize them as per your need.
If you have anything to add on this topic or if you have any doubts, please leave a comment below.
Like what you read? Please share it with others.
Filed Under: Tutorial
Creator of It’s FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries ranging from Agatha Christie and Sherlock Holmes to Detective Columbo & Ellery Queen. Also a movie buff with a soft corner for film noir.
We use an USB disk for swap, and it will not retain the swapfile after reboot.
Anyone else have this issue. This has happened on both 18 and 20. The modification shows exactly as it should in fstab, but does not register with system after reboot (free -h shows “/[external swapfile]/Swap/swapfile none swap sw 0 0”).
Join 75,000 other Linux users and start your journey to become a better, informed desktop Linux user.
It’s FOSS is Part of chmod777 Media Tech (OPC) Pvt Ltd· Built on Genesis Framework and Powered by UpCloud
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.

source

LINXGO
Logo
Compare items
  • Total (0)
Compare