How to Add 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.
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. Some of well-known distributions add swap partition automatically.
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.
Check Swap Space
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.
$ free -m
total used free shared buffers cached
Mem: 996 921 74 0 107 268
-/+ buffers/cache: 546 449
Swap: 0 0 0
As we can see on the command output, this example system doesn't have swap enabled. Let's add one.
Create Swap File
First thing first, create a file with the size of swap space you want. Let’s say that I want to add 2 GB of swap space to my system. The swap file is called swapfile
. We use the dd
command to create a file of size 2 GB.
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2,1 GB) copied, 3,84195 s, 559 MB/s
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. Let's apply appropriate permissions to the file.
$ sudo chmod 600 /swapfile
Convert the File to a Swap File
We need to tell the Linux system that this file will be used as swap space. We can do that with mkswap
tool.
$ sudo mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=9cd611d7-dd82-4f9c-96ea-d61b87455d34
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.
$ sudo swapon /swapfile
Confirm Swap Active
Let's run the free
command again. You should see something like this:
$ free -m
total used free shared buffers cached
Mem: 996 913 82 0 107 269
-/+ buffers/cache: 536 460
Swap: 2047 0 2047
Make Swap Permanent
Our new swap space will be gone upon restart unless we update /etc/fstab
file. Modify the /etc/fstab
file to make sure our system will use this swap space everytime it booted.
$ sudo vi /etc/fstab
Add this line:
/swapfile swap swap defaults 0 0
Save the file and quit. Congratulations, now you've added a swap space on your Linux system.
Remove Swap
In case you want to remove added swap space, you can do it with swapoff
command. Make sure you also remove the respective swap entry from the /etc/fstab
file after disabling the swap.
$ sudo swapoff /swapfile
Resize Swap
There are a couple of ways we can resize the swap space on Linux.
When we ask our system to stop using a swap file for swap area, it transfers all the data (pages to be precise) back to RAM. So make sure we have enough free RAM before we swap off.
It's a good practice to create and enable another temporary swap file. This way, when we swap off the original swap area, our system will use the temporary swap file. Then we can safely resize the original swap space. We can manually remove the temporary swap file or leave it as it is and it will be automatically deleted on the next boot.
You can skip creating temporary swap if you have enough free RAM.
First step to resize our swap file is swapoff
your original file.
$ sudo swapoff /swapfile
Next steps are basically repeat the steps from "Create Swap File":
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
Final Words
I hope that you now know how to add swap file on Linux. If you run into any issues or have any feedback feel free to drop a comment below.