How to Create a Linux Swap File

June 1, 2023

Introduction

Swap files are essential to Linux memory management. Swap enables performance optimization and managing resource-intensive tasks. Knowing how to create and manage swap files is a crucial skill in Linux memory management.

This article explains how to create, manage, and remove swap files on Linux.

How To Create A Linux Swap File

Prerequisites

  • A system running Linux.
  • Access to the command line/terminal.
  • A user account with sudo privileges.

What is a Swap File?

A swap file is a type of swap memory in the form of a computer file on an HDD or SSD that serves as an extension to physical memory (RAM). A computer uses a swap file to store data from RAM that is temporarily inactive. The swapping process frees up RAM space by moving infrequently used data in RAM to a swap file.

Swap files are a form of virtual memory management and serve as a fallback mechanism. The swap space has significantly slower access times than RAM. Heavy reliance on swap files leads to performance losses.

How to Check Swap Space

There are several ways to check swap space in Linux. The commands that show different information about swap space are:

  • The free command with the -h tag shows the total, available, and used swap space in a human-readable format. For example:
free -h
free -h terminal output
  • The /proc/meminfo file shows the total, free, and used swap space in kilobytes:
cat /proc/meminfo | grep Swap
cat /proc/meminfo | grep Swap terminal output
  • The /proc/swaps file shows active swap devices:
cat /proc/swaps
cat /proc/swaps terminal output
  • The swapon command with the --show tag displays swap information:
swapon --show
swapon --show terminal output

The output shows the active swap devices, including the path, file type, size, and usage.

To monitor swap space, use a monitoring tool such as the vmstat command.

How to View a Swap File on Linux

To view the contents of a swap file, use the strings command and save the contents to another file. For example:

sudo strings <swap file path> > <output file path>.txt

Exchange the paths in the command to the actual swap file location and output file. For example:

sudo strings /swapfile > output.txt
sudo strings swap file terminal output

View the contents of the output file using a text editor.

How to Create a New Swap File on Linux

Creating a swap file on Linux is a simple process. Below are steps that show how to make a new swap file on a Linux system.

1. Create Storage File

Use the dd tool to create a new storage file. For example:

sudo dd if=/dev/zero of=/swapfile bs=1MB count=1024
sudo dd swap file creation terminal output

The command contains the following parameters:

  • if=/dev/zero is the input file. The /dev/zero file is a special file that returns as many null characters as a read operation requests.
  • of=/swapfile is the output swap storage file. The common practice is to place the file in the root directory.
  • The bs parameter is the block size.
  • The count parameter determines how many blocks to copy.

The total data size is bs*count, which in this case is 1GB.

2. Set Swap Permissions

Set the swap file permissions to root read and write. Use the following command:

sudo chmod 600 /swapfile

The command shows no output. Check the permissions with the following:

ls / | grep swapfile
swap file permissions

Changing the permissions to root helps avoid accidental overwriting.

3. Set Up Swap Area

The swap file requires formatting the reserved file size into a swap area. Use the mkswap command to format the swap file:

sudo mkswap /swapfile
sudo mkswap swap file

The command formats the swap file into a swap space and outputs the size, label, and UUID.

4. Enable Swap

To enable the swap area for use, run the following command:

sudo swapon /swapfile

Verify the swap is active with the following command:

swapon --show
swapon --show new swap file terminal output

The output shows a list of swapping devices and files, including the newly created swap file.

5. Persist Swap

The swap file does not persist after a restart by default. To make the changes permanent, do the following:

1. Open the /etc/fstab file with a text editor:

sudo nano /etc/fstab

2. Append the swap information in the following format:

/swapfile swap swap defaults 0 0
/etc/fstab swap file

3. Save the file and close the editor.

How to Adjust Swappiness on Linux

Swappiness determines how often a Linux system uses the swap space. The swappiness value is a number between 0 and 200. Below are the steps to view and adjust swappiness:

1. To check the current system swappiness value, use:

cat /proc/sys/vm/swappiness
/cat/proc/sys/vm/swappiness terminal output

The command prints a number between 0 and 200. A low value avoids swapping, while a high value increases swap usage.

2. To change the swappiness value to 10, run the following command:

sudo sysctl vm.swappiness=10
sudo systemct vm.swappiness 10 terminal output

The optimal value depends on the system, workload, and memory usage. Production servers typically require lower values.

3. To persist the change after a restart, open the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

4. Append the following information to the file:

vm.swappiness = 10
/etc/sysctl vm.swappiness 10

5. Save the file and close nano.

How to Remove a Swap File on Linux

Removing a swap file requires deactivating the swap space before removal. To remove a swap file on Linux, do the following:

1. Deactivate the swap with the following command:

sudo swapoff -v /swapfile
sudo swapoff deactivate swap terminal output

Exchange the path and file name if it differs.

2. Use the rm command to delete the swap file:

sudo rm /swapfile

3. If the swap is persistent, open the /etc/fstab file with a text editor:

sudo nano /etc/fstab

Locate the entry, remove the whole line, and save the changes.

Conclusion

After reading this guide, you know what swap files are and how to manage them.

For more memory management topics, check out these helpful memory management commands for Linux.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Check Memory Usage in Linux, 5 Simple Commands
March 28, 2024

In this tutorial, learn the five most commonly used commands to check memory usage in Linux. We also...
Read more
How to Check Disk Space in Linux
May 8, 2024

This tutorial shows how to display disk usage from a command line in Linux. It is important to know how much disk space you have used up, as well as how free disk space...
Read more
7 Ways to Reduce Server Response Time
December 20, 2023

Reducing server response time to the bare minimum is one of the primary goals of website owners and SEO experts. The first couple of seconds determine...
Read more
Best Tools to Monitor Network Bandwidth on a Linux Server
September 19, 2024

There are many different tools for monitoring network traffic on a Linux server. Each option has its strengths and weaknesses. Most...
Read more