Linux

Creating a Swap File in Linux: A Comprehensive Guide

This guide explains how to create a swap file in Linux from scratch. You'll learn to allocate disk space for virtual memory, configure its parameters, and verify functionality to stabilize system performance during RAM shortages.

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

Introduction / Why This Is Needed

A swap file in Linux is a disk area used as an extension of RAM. When physical memory runs low, the system automatically moves inactive data from RAM to swap, preventing processes from crashing due to memory shortage.

Creating a swap file is a quick and flexible alternative to allocating a swap partition. You will be able to:

  • Stabilize system performance during peak loads or when working with memory-intensive applications.
  • Increase available memory without upgrading hardware.
  • Configure swapping parameters for specific tasks (server, desktop, embedded system).

After completing this guide, you will have a configured and active swap file that will mount automatically at boot.

Requirements / Preparation

Before starting, ensure that:

  1. You have root account access or sudo privileges.
  2. There is free space on the disk. The swap file size depends on your needs:
    • For systems with 4 GB RAM or less: swap size = 1.5–2 × RAM.
    • For systems with 8+ GB RAM: 1–4 GB of swap is sufficient (or even less if RAM >16 GB).
    • For servers with large databases: swap equal to RAM size may be required.
  3. Basic utilities are installed: fallocate (usually in the util-linux package) or dd (available everywhere). Verify with: which fallocate.

Step-by-Step Instructions

Step 1: Check Current Swap Configuration

Before creating a new file, make sure you don't already have an active swap (e.g., a partition or default file).

free -h

or

swapon --show

What you will see:

              total        used        free      shared  buff/cache   available
Mem:           7.7G        1.2G        5.1G        156M        1.4G        6.1G
Swap:          2.0G          0B        2.0G

If the Swap line shows zero or the partition is absent in the swapon --show output, proceed with file creation.

Step 2: Create a File of the Required Size

The recommended method is fallocate, as it creates the file instantly without filling it with zeros.

sudo fallocate -l 2G /swapfile

Here:

  • -l 2G — file size (2 gigabytes). Replace 2G with the needed value: 1G, 4G, 512M, etc.
  • /swapfile — file path. Typically placed at root /.

Alternative for older systems (if fallocate is unavailable):

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

bs=1M count=2048 will create a 2048 MB (2 GB) file. This method is slower because it writes zeros to the entire file.

Step 3: Set Correct Permissions

The swap file contains sensitive data (process memory fragments). It must be accessible only to the superuser.

sudo chmod 600 /swapfile

Check permissions:

ls -lh /swapfile

Output should be: -rw------- 1 root root ... /swapfile.

Step 4: Format the File as Swap

Prepare the file for use as swap space:

sudo mkswap /swapfile

Example output:

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

Note the UUID (if planning to use it in /etc/fstab), but usually specifying the file path is sufficient.

Step 5: Activate the Swap File

Enable the file in the system without rebooting:

sudo swapon /swapfile

Make the setting permanent (so swap activates after every reboot): Open the /etc/fstab file in an editor (e.g., sudo nano /etc/fstab) and add this line at the end:

/swapfile none swap sw 0 0

Save and close the editor.

Step 6: Configure Swappiness Parameter (Optional)

The vm.swappiness parameter controls how aggressively the system uses swap. Values range from 0 (minimum) to 100 (maximum). The default is often 60.

For desktops (to avoid slowing down applications) — recommended 10–20.
For servers (to keep more data in RAM) — 1–5.

Set temporarily (until reboot):

sudo sysctl vm.swappiness=10

To make the value persistent, add to /etc/sysctl.conf:

vm.swappiness=10

And apply: sudo sysctl -p.

Verification

  1. Ensure swap is active:
    swapon --show
    

    Your /swapfile should appear in the output.
  2. Check total memory:
    free -h
    

    The Swap line will now show size and usage.
  3. Confirm the setting is permanent (reboot the system and run free -h again).
  4. (Optional) Check the /etc/fstab entry:
    cat /etc/fstab | grep swap
    

    There should be a line with /swapfile.

Potential Issues

❌ Error fallocate: fallocate failed: No space left on device

  • Cause: Insufficient free space on the disk.
  • Solution: Free up space (df -h to check) or create a smaller file.

❌ Error chmod: changing permissions of ‘/swapfile’: Operation not permitted

  • Cause: The file is on a filesystem mounted with noexec or nosuid options (e.g., some /home partitions).
  • Solution: Create the file at root / or on a partition where permission changes are allowed.

❌ Swap does not activate after reboot

  • Cause: Error in /etc/fstab (extra spaces, incorrect path) or the file was moved/deleted.
  • Solution: Check /etc/fstab syntax with sudo mount -a (errors will be displayed). Ensure /swapfile exists and has 600 permissions.

❌ System "freezes" with active swap usage

  • Cause: vm.swappiness value is too high (system aggressively writes to swap on a slow HDD).
  • Solution: Reduce vm.swappiness to 1–5. For SSDs this is less critical but still advisable to configure.

mkswap warns about file size

  • Cause: File created with non-integer megabyte size (e.g., 2.5 GB).
  • Solution: mkswap usually works with any size, but for compatibility, create files with sizes divisible by 4 KB. Recreate the file with a size divisible by 1 MB (e.g., 2048M instead of 2G).

F.A.Q.

Why is a swap file needed if there's RAM?
Can a swap file be removed after creation?
What's the difference between a swap file and a swap partition?
How to check if swap is working?

Hints

Check current swap configuration
Create a file of the required size
Set correct permissions
Format the file for swap
Activate the swap file
Configure swappiness parameters (optional)
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