How to Create and Use Swap File on Linux (2024)

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.

How to Create and Use Swap File on Linux (1)

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 availableMem: 7.5G 4.1G 267M 971M 3.1G 2.2GSwap: 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 --showNAME 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 --showNAME TYPE SIZE USED PRIO/swapfile file 2G 0B -2

If you don’ have a swap space on your system, it should show something like this:

free -h total used free shared buff/cache availableMem: 7.5G 4.1G 267M 971M 3.1G 2.2GSwap: 0B 0B 0B

The swapon command won’t show any output.

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.

sudo chmod 600 /swapfile

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.

sudo mkswap /swapfile

You should see an output like this:

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.

sudo swapon /swapfile

Now if you check the swap space, you should see that your Linux system recognizes and uses it as the swap area:

swapon --showNAME 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

Now you can add the following line to the end of /etc/fstab file:

/swapfile none swap sw 0 0

You can do it manually using a command line text editor or you just use the following command:

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:

cat /proc/sys/vm/swappiness

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:

vm.swappiness=25

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.

sudo swapoff /swapfile

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

Now mark the file as swap space again:

sudo mkswap /swapfile

And turn the swap on again:

sudo swapon /swapfile

You may also choose to have multiple swap files at the same time.

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:

sudo swapoff /swapfile

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:

sudo rm /swapfile

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.

How to Create and Use Swap File on Linux (2024)

FAQs

How do I create a swap file in Linux? ›

To create and use a swap file:
  1. Use the dd command to create a file of the required size (for example, one million one-kilobyte blocks): # dd if=/dev/zero of=/swapfile bs=1024 count=1000000.
  2. Initialize the file as a swap file: # mkswap /swapfile.
  3. Enable swapping to the swap file: # swapon /swapfile.

How do I use swap command in Linux? ›

To see swap size in Linux, type the command: swapon -s . You can also refer to the /proc/swaps file to see swap areas in use on Linux. Type free -m to see both your ram and your swap space usage in Linux. Finally, one can use the top or htop command to look for swap space Utilization on Linux too.

How do I set up a swap file? ›

Create swap file on Linux
  1. Step 1: Make a new swap file. First thing first, create a file with the size of swap space you want. ...
  2. 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. ...
  3. Step 3: Enable the swap file. ...
  4. Step 4: Make the changes permanent.
Mar 18, 2023

Which command is used to create a swap file? ›

A swap partition is just what its name implies—a standard disk partition that is designated as swap space by the mkswap command. A swap file can be used if there is no free disk space in which to create a new swap partition or space in a volume group where a logical volume can be created for swap space.

How to create swap partition in Linux step by step? ›

To create and use a swap partition:
  1. Use fdisk to create a disk partition of type 82 ( Linux swap ) or parted to create a disk partition of type linux-swap of the size that you require.
  2. Initialize the partition (for example, /dev/sda2 ) as a swap partition: # mkswap /dev/sda2.

How do I create a LVM swap? ›

11.2. 2. Creating an LVM2 Logical Volume for Swap
  1. Create the LVM2 logical volume of size 256 MB: Copied! ...
  2. Format the new swap space: Copied! ...
  3. Add the following entry to the /etc/fstab file: Copied! ...
  4. Enable the extended logical volume: Copied! ...
  5. Test that the logical volume has been extended properly: Copied!

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6153

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.