- How To Create a Linux Swap File
- How to Create and Enable Swap in Linux
- How to Create and Use Swap File on Linux
- What is a swap file in Linux?
- Check swap space in Linux
- Create swap file on Linux
- Step 1: Make a new swap file
- Step 2: Mark the new file as swap space
- Step 3: Enable the swap file
- Step 4: Make the changes permanent
- Adjust swappiness
- Resizing swap space on Linux
- Removing swap file in Linux
- Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine
- What is TTY in Linux?
- Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
- How to Access the GRUB Menu in Virtual Machine
- FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More
- Using cp Command in Linux
- Become a Better Linux User
How To Create a Linux Swap File
In this article, we will explain swap space, and learn how to create swap space using a swap file in Linux: this is important in case we don’t have a swap partition created on the hard disk.
Swap space/partition is space on a disk created for use by the operating system when memory has been fully utilized. It can be used as virtual memory for the system; it can either be a partition or a file on a disk.
When the kernel runs out of memory, it can move idle/inactive processes into swap creating room for active processes in the working memory. This is memory management that involves swapping sections of memory to and from virtual memory.
With that said, below are the steps we can follow to create a swap space using a file.
How to Create and Enable Swap in Linux
1. In this example, we will create a swap file of size 2GB using the dd command as follows. Note that bs=1024 means read and write up to 1024 bytes at a time and count = (1024 x 2048)MB size of the file.
# dd if=/dev/zero of=/mnt/swapfile bs=1024 count=2097152
Alternatively, use the fallocate command as follows.
# fallocate --length 2GiB /mnt/swapfile
And then set the appropriate permissions on the file; make it readable only by root user as follows.
2. Now setup the file for swap space with the mkwap command.
3. Next, enable the swap file and add it to the system as a swap file.
4. Afterwards, enable the swap file to be mounted at boot time. Edit the /etc/fstab file and add the following line in it.
/mnt/swapfile swap swap defaults 0 0
In the line above, each field means:
- /mnt/swapfile – device/file name
- swap – defines device mount point
- swap – specifies the file-system type
- defaults – describes the mount options
- 0 – specifies the option to be used by the dump program
- 0 – specifies the fsck command option
6. To set how often the swap file can be used by the kernel, open the /etc/sysctl.conf file and add the line below.
Note that the default value of how frequent swap space can be used is 60 (maximum value is 100). The higher the number, the more frequent swap space utilization by the kernel. When the value is set to 0, the swap file will only be used if the operating system has fully utilized memory.
6. Now verify the swap file was created using the swapon command.
# swapon -s OR # free OR # cat /proc/swaps
We can optionally reboot the system to effect the above changes using the following command.
Remember to also read through these useful Linux memory management guides:
That’s It! If you have any issues, use the feedback form below to send us any questions or important additional ideas to this topic.
How to Create and Use Swap File on Linux
This tutorial discusses the concept of swap files 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.
What is a swap file in Linux?
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.
Check swap space in Linux
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.
free -h total used free shared buff/cache available Mem: 7.5G 4.1G 267M 971M 3.1G 2.2G Swap: 14G 0B 14G
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.
swapon --show NAME TYPE SIZE USED PRIO /dev/nvme0n1p4 partition 14.9G 0B -2
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.
swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2
free -h total used free shared buff/cache available Mem: 7.5G 4.1G 267M 971M 3.1G 2.2G Swap: 0B 0B 0B
Create swap file on Linux
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.
Step 1: Make a new swap file
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.
sudo fallocate -l 1G /swapfile
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.
Step 2: Mark the new file as swap space
Your need to tell the Linux system that this file will be used as swap space. You can do that with mkswap tool.
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=7e1faacb-ea93-4c49-a53d-fb40f3ce016a
Step 3: Enable the swap file
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:
swapon --show NAME TYPE SIZE USED PRIO /swapfile file 1024M 0B -2
Step 4: Make the changes permanent
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.
sudo cp /etc/fstab /etc/fstab.back
/swapfile none swap sw 0 0
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Now you have everything in place. Your swap file will be used even after you reboot your Linux system.
Adjust swappiness
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:
sudo sysctl vm.swappiness=25
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:
Resizing swap space on Linux
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:
sudo fallocate -l 2G /swapfile
Removing swap file in Linux
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.
Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine
What is TTY in Linux?
Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI
How to Access the GRUB Menu in Virtual Machine
FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More
Using cp Command in Linux
Become a Better Linux User
With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world