Linux Notepad

Create swap space as a file in Linux (Debian/Ubuntu)

Swap is a designated portion of your hard drive that temporarily stores data when your system’s physical memory (RAM) is full.
Using a swap can improve system stability and acts as a safeguard against out-of-memory errors.

Creating a swap file instead of a swap partition is often more convenient, especially on systems where you cannot easily modify partitions or when you need to quickly add swap space without rebooting. Also, changing the swap size is easier with a file than with a partition.

This guide explains how to add swap space as a file on modern Debian and Ubuntu systems.
These instructions were verified on Debian 12 and Ubuntu 24.04, though they should work on any Linux distribution that does not already have swap enabled.

Creating the swap

1. Verify existing swap space

Before creating a new swap file, check if any swap is currently enabled:

sudo swapon --show

If no output is returned, your system currently has no active swap space.

2. Check available disk space

Make sure your hard drive has enough free space on the partition where you intend to create the swap file:

df -h

3. Create the swap file

Use the fallocate command to allocate disk space for the swap file.
For example, to create a 4GB swap, execute:

sudo fallocate -l 4G /swapfile

4. Set proper permissions

Restrict access to the swap file so that only the root user can read or write to it:

sudo chmod 600 /swapfile

5. Mark the file as swap

Configure the newly created file as swap space:

sudo mkswap /swapfile

6. Enable the swap

Activate the swap file immediately:

sudo swapon /swapfile

This command will enable the swap file for the current session only.
To start using the swap file automatically at boot, read the next section.

Confirm that the swap file is active by running:

sudo swapon --show

7. Making the swap permanent

Copy the original swap file configuration to a backup file, in case you need to restore it later:

sudo cp /etc/fstab /etc/fstab.original

To ensure the swap file is used automatically at boot, add it to the /etc/fstab file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This command appends the necessary configuration to the end of the file.

Tuning swap settings

You can adjust the swappiness parameter to control how aggressively your system uses swap space.

The swappiness value ranges from 0 to 100, where:

  • 0 means the system will avoid using swap as much as possible.
  • 100 means the system will use swap space aggressively.

To check the current swappiness value, run:

cat /proc/sys/vm/swappiness

1. Change the swappiness value temporarily (until the next reboot)

sudo sysctl vm.swappiness=10

2. Make the change permanent

Add the following line to /etc/sysctl.conf:

sudo nano /etc/sysctl.conf

If the vm.swappiness line does exist, modify it; otherwise, add it at the end of the file:

vm.swappiness=10

After saving the file, apply the changes with:

sudo sysctl -p

Resources

Back to homepage