Linux

Setting up swap space in Linux: a detailed guide

This guide will help you add or modify swap space in Linux to improve system stability when RAM is low. You'll learn to create swap files, configure their parameters, and manage swapping.

Updated at February 16, 2026
10-15 min
Medium
FixPedia Team
Применимо к:Ubuntu 20.04+Debian 11+CentOS 8+RHEL 8+Fedora 35+Any modern Linux distribution

Introduction / Why You Need This

Swap space is an area on disk used as an extension of RAM. When physical memory runs low, the Linux kernel moves inactive memory pages to swap, freeing up RAM for more important processes. This helps prevent programs from being abruptly terminated due to out-of-memory (OOM) conditions and improves overall system stability, especially on servers with limited RAM or desktops running multiple heavy applications simultaneously.

In this guide, you'll learn how to create and configure a swap file—the most flexible and modern way to add swap space in Linux. You'll be able to control its size, enable/disable it without rebooting, and configure system behavior when using swap.

Requirements / Preparation

Before starting, ensure:

  1. You have access to the system with sudo or root privileges.
  2. The target disk (usually / or /home) has enough free space. Recommended swap size: from 2 GB to the size of your RAM (see FAQ).
  3. You know the path to the partition where the file will reside. You can check with df -h.
  4. Basic utilities are installed: fallocate (usually in the util-linux package) or dd. If fallocate is unavailable, use dd.

Step 1: Check Current Swap Space

First, let's see if there's already active swap and how much it occupies. Run:

sudo swapon --show

Or use the free utility:

free -h

Look for the Swap: line in the output. If it shows 0B or the swap partition is missing from the list, there's no active swap, and you need to create one.

You can also check if a swap partition already exists in the system using:

lsblk -f

Look for the swap type in the FSTYPE column.


Step 2: Create the Swap File

We'll create the swap file in the root directory (/). You can choose another location, such as /swapfile or /var/swap. We'll specify the size in gigabytes. For example, we'll create a 4 GB file.

sudo fallocate -l 4G /swapfile

This command instantly creates a file of the specified size, reserving disk space. If fallocate doesn't work on your filesystem (for example, on some older ext4 versions with -o discard enabled), use Method B.

Method B: Using dd (slower but more universal)

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress

Here, bs=1M is a 1-megabyte block, count=4096 is the number of blocks (4096 * 1MB = 4GB). status=progress shows the progress (available in modern versions of dd).

⚠️ Important: Ensure there is enough space on the target partition. The df -h command will show free space.


Step 3: Set Permissions and Convert to Swap

By default, the created file is readable and writable by everyone. This is insecure for a swap file. Set the correct permissions:

sudo chmod 600 /swapfile

Now only the root user can read and write to this file.

Next, initialize the file as a swap area:

sudo mkswap /swapfile

The command output will look something like:

Setting up swapspace version 1, size = 4 GiB (4294967296 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Remember or save the UUID (unique identifier)—you'll need it for configuring /etc/fstab.



Step 4: Activate the Swap File

Now enable the new swap space in the current session:

sudo swapon /swapfile

Check its presence again:

sudo swapon --show

You should see your /swapfile in the list with its size and type file.

Also, the free -h command will show the total swap volume in the Swap: line.


Step 5: Configure Settings and Make Permanent

5.1 Add to /etc/fstab

To have swap activate automatically at every system boot, add an entry to the /etc/fstab file. Open it for editing (for example, sudo nano /etc/fstab) and add the line:

/swapfile none swap sw 0 0

If you want to use UUID (a more reliable method, especially if the file path might change), specify:

UUID=your-uuid-from-step-3 none swap sw 0 0

Save the file and close the editor.

5.2 Configure vm.swappiness (Optional)

The vm.swappiness parameter controls how aggressively the kernel will use swap. Values range from 0 to 100.

  • 0 — the kernel will avoid swap until RAM is completely exhausted.
  • 100 — the kernel will actively use swap.
  • The default in most distributions is 60.

Check the current value:

cat /proc/sys/vm/swappiness

Change temporarily (until reboot):

sudo sysctl vm.swappiness=10

Change permanently: add a line to /etc/sysctl.conf or create a separate config in /etc/sysctl.d/99-swappiness.conf:

vm.swappiness=10

Apply changes without rebooting:

sudo sysctl -p

For most desktop systems, the recommended value is 10-30. For servers with large amounts of RAM, you can leave it at 60 or even lower it to 10.


Verify the Result

  1. Reboot the system (sudo reboot) to ensure swap activates automatically.
  2. After booting, run:
    sudo swapon --show
    free -h
    
    Your swap file should appear in the output.
  3. Check that the entry in /etc/fstab doesn't cause errors at boot. You can run:
    sudo mount -a
    
    If the command completes without errors, the configuration is correct.
  4. Monitor swap usage in real time:
    watch -n 2 free -h
    
    Or use the htop utility (if installed), where swap is displayed at the top.

Possible Issues

Problem: fallocate doesn't work, file is created with zero size

Solution: Use dd (Method B). This can happen on filesystems that don't support fallocate (for example, some NFS configurations or older ext4).

Problem: Activation produces error swapon: /swapfile: swapon failed: Invalid argument

Solution: Most likely, the file is created incorrectly or has wrong permissions. Ensure that:

  1. The file exists and has a size greater than zero (ls -lh /swapfile).
  2. Permissions are set to 600 (sudo chmod 600 /swapfile).
  3. The file is initialized via sudo mkswap /swapfile.

Problem: Swap doesn't activate after reboot

Solution:

  1. Check the syntax of the entry in /etc/fstab. It's better to use UUID.
  2. Check the output of sudo mount -a for errors.
  3. Ensure that the /swapfile exists at the specified path and hasn't been deleted.

Problem: System doesn't use swap even when memory runs low

Solution: Check the vm.swappiness value. If it's 0, the system will try to avoid using swap. Increase it to 10 or 20. Also check if swap is full (free -h shows Swap: 4.0G 0B 4.0G—that's normal if 0B is used).

Problem: Performance drops when swap is actively used

Solution: Swap on an HDD (traditional hard disk) significantly slows down the system. Consider:

  1. Adding more RAM (the most effective solution).
  2. Using a swap file on an SSD (much faster, but drive wear will increase).
  3. Configuring vm.vfs_cache_pressure and other parameters in /etc/sysctl.conf to optimize caching.
  4. Increasing vm.swappiness to 0 if swap is only used as an emergency buffer.

Problem: Not enough disk space to create a swap file

Solution: Free up space by removing unnecessary files (/var/log, package caches, old kernels). Alternatively, create the swap file on another partition/disk with free space. As a last resort, you can create a swap partition (via fdisk/gdisk and mkswap), but that will require repartitioning the disk and a reboot.

F.A.Q.

Swap file or swap partition: which to choose?
What is the optimal swap space size?
Can swappiness be changed after creating swap?
What to do if the system doesn't use swap even when memory runs out?

Hints

Check current swap space
Create a swap file
Set permissions and convert to swap
Activate the swap file
Configure parameters and enable permanently
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community