Home » Linux » How to Create a Linux Swap File

How to Create a Linux Swap File

Update on:
Sep 2, 2021

Swap is a special type of space on disk which is use when physical RAM fully utilized. The swap space used for storing the inactive pages from RAM, when your Linux box is runs out of RAM.

The swap space can be created in two forms, either a dedicated swap partition or a swap file. In the case of cloud infrastructure when you use a virtual machine, a swap partition is not present. So, in this case, you have the only option is to create a swap file.

This tutorial of creating swap space tested on Ubuntu 18.04 and CentOS 7 Linux, but it will work with any flavour and Linux distribution.

How to add swap file

The below steps are used to create 1GB of swap to your server. If you want to add 2 Gb instead of 1 GB, replace 1G with 2G.

Step 1 – Create a swap file with 1 GB space:

$ sudo fallocate -l 1G /swapfile

If faillocate is not available in your system or getting an error message saying fallocate failed: Operation not supported, then you can use the following command to create swap file:

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

Step 2 – Now you have to set correct permission to the swap file, so only root user should be able to read and write the swap file. To set the correct permissions type:

$ sudo chmod 600 /swapfile

Step 3 – You can make a Linux swap area by using mkswap utility, as shown below:

$ sudo mkswap /swapfile

Step 4 – Now time to enable the swap using the following command:

$ sudo swapon /swapfile

To make these changes permanent edit the /etc/fstab file and append the file with the following line:

/swapfile swap swap defaults 0 0

Step 5 – To verify the swap file is active or not, use either the free command or swapon command, as shown below:

$ sudo swapon --show
/swapfile file 1024M 507.4M   -1
$ sudo free -h
              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 unique property of the Linux kernel that is use to define how often the system will use the swap space. It is having a value between 0 and 100. The low value will inform the kernel to try to avoid swapping whenever possible, and the higher value will make the kernel to use the swap space aggressively.

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

$ cat /proc/sys/vm/swappiness

The default value of swappiness is ok for most of the Linux system, but when you are using production servers, you may need to set a lower value.

To set the swappiness value, you can use the sysctl command, for example, to set the swappiness value to 10 by using the following command:

$ sudo sysctl vm.swappiness=10

To make this changes permanent across reboot, append the following line to the /etc/sysctl.conf file:


The optimal value of swappiness is depended on your system workload and how the memory being used. You have to adjust swappiness value in small increments to find an optimal value.

How to remove swap file

If you want to deactivate and remove the swap file cause of any reason, follow the below steps:

Step 1 – In the first step, deactivate the swap file by using the following command:

$ sudo swapoff -v /swapfile

Step 2 – Now, remove the swap file entry from the /etc/fstab file:

/swapfile   swap    defaults     0      0

Step 3 – Finally, need to delete the swapfile using the rm command:

$ sudo rm /swapfile


Now, you have learned to create a swap file and activate, configure swap space on your Linux box.

If you have any issue or feedback, please leave a comment below.

Related Posts

Creating a lame utility HTTP server in Linux Operating System

In this article, we will discuss the cURL tool in Linux. The cURL tool is used for transferring the data from or to a server. It supports many protocols, and http is one of them. cURL is used to transfer the data from URL. It has so many tricks to offer, such as http...

Finding binary dependencies in Linux Operating System

In this article, we are going to check the executable. We will find out which string is present in it by using the string command.PrerequisitesBesides having a terminal open, make sure you have a binary present in your directory.Find dependencies First, we...

Capturing network traffic headlessly in Linux Operating System

In this article, we are going to learn how to capture traffic. We are going to capture network traffic with a packet sniffer tool called tcpdump. This tool is used to filter or capture TCP/IP packets that are transferred or received over a network.PrerequisitesBesides...

Follow Us

Our Communities

More on Linux

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!



Submit a Comment

Your email address will not be published.

4 × 5 =