General Linux

Create a Linux Swap File

create-swap-file-linux

How to Create and Use Swap File on Linux

Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.

Swap space can take the form of either a dedicated swap partition or a swap file. In most cases, when running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file.

This tutorial was tested on Linux systems with Ubuntu 18.04 and CentOS 7, but it should work with any other Linux distribution.

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.

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.

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.

# free -h

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

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.

1. Create a file that will be used for swap:

$ sudo fallocate -l 1G /swapfile

If faillocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:

$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

2. Only the root user should be able to write and read the swap file. To set the correct permissions type:

$ sudo chmod 600 /swapfile

3. Use the mkswap utility to set up the file as Linux swap area:

$ sudo mkswap /swapfile

4. Enable the swap with the following command:

$ sudo swapon /swapfile

 

To make the change permanent open the /etc/fstab file and append the following line:

 /swapfile swap swap defaults 0 0

To verify that the swap is active, use either the swapon or the free command as shown below:

$ sudo swapon --show
output
NAME      TYPE  SIZE   USED PRIO
/swapfile file 1024M 507.4M   -1
$ sudo free -h
 
output
              total        used        free      shared  buff/cache   available
Mem:           488M        158M         83M        2.3M        246M        217M
Swap:          1.0G        506M        517M

How to adjust the swappiness value

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.

The default swappiness value is 60. You can check the current swappiness value by typing the following command:

$ cat /proc/sys/vm/swappiness
 
output
60

While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.

For example, to set the swappiness value to 10, you would run:

$ sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:

vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

How to remove Swap File

If for any reason you want to deactivate and remove the swap file, follow these steps:

1. First, deactivate the swap by typing:

$ sudo swapoff -v /swapfile

2. Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Finally, delete the actual swapfile file using the rm command:

$ sudo rm /swapfile

Conclusion

You have learned how to create a swap file and activate and configure swap space on your Linux system.

If you hit a problem or have feedback, leave a comment below.

 

Tags

About the author

Jamie Vadym

Jamie Vadym

Founder of Error Hat. Expertise in Virtualization, Cloud Computing, Linux/UNIX systems, Programming,Storage systems,HA, Server Clustering e.t.c.

Add Comment

Click here to post a comment