LinuxLow

Creating and Configuring a Swap File in Linux

In this guide, you'll learn how to create, configure, and activate a swap file in Linux to increase virtual memory. Detailed commands and explanations provided.

Updated at February 14, 2026
10-15 minutes
Easy
FixPedia Team
Применимо к:Ubuntu 20.04+CentOS 7+Debian 10+Any Linux with kernel 3.0+

Introduction

A swap file (paging file) is an area on disk used as an extension of RAM when it runs low. Unlike a swap partition, a file is easier to create, resize, or delete without repartitioning the disk. This guide will detail how to set up a swap file in Linux, from creation to permanent activation.

Pre-Checks

Before creating a swap file, ensure:

  1. Sufficient free space on the target partition. The swap file should not occupy more than 50% of free space if you plan to use it for hibernation.
  2. No conflicting swap devices. Check current swap:
    swapon --show
    
    If the output is empty, swap is inactive. If there are active swap partitions or files, you can add another.
  3. You have sudo privileges. All operations require administrator rights.

Step 1: Create the Swap File

The recommended method is fallocate, as it quickly allocates space without writing zeros. An alternative is dd, which is slower but fills the file with zeros.

Example for a 4 GB swap file:

sudo fallocate -l 4G /swapfile

If fallocate is unavailable (rare cases), use dd:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=4194304

Here bs=1024 is a 1 KB block, count=4194304 is 4 GB in KB.

💡 Tip: Swap size depends on RAM volume and usage scenario. For desktop systems with 4-8 GB RAM, 2-4 GB swap is sufficient. For servers with large RAM, swap may be less than 1x.

Step 2: Set Permissions

The swap file must be accessible only by root for security:

sudo chmod 600 /swapfile

This prevents reading or writing by other users.

Step 3: Format the File as Swap

Prepare the file with mkswap:

sudo mkswap /swapfile

The output will show the UUID and size. Example:

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

Record the UUID—you will need it for /etc/fstab (optional but recommended for stability).

Step 4: Activate the Swap File

Enable swap without rebooting:

sudo swapon /swapfile

Verify that swap is active:

swapon --show

Or:

free -h

In the free output, find the Swap: line. It should show total size and used space.

Step 5: Permanent Activation via fstab

To have swap activate on every boot, add an entry to /etc/fstab. Use one of two options:

Option A: by file path (simpler)

/swapfile none swap sw 0 0

Option B: by UUID (more reliable if the path might change)

UUID=your-uuid none swap sw 0 0

You can get the UUID via sudo blkid /swapfile.

⚠️ Important: Do not use defaults or auto options for swap in fstab. Only sw (swap) and 0 0 for dump and fsck.

After editing /etc/fstab, check the syntax:

sudo mount -a

If there are no errors, swap will work after a reboot.

Step 6: Configure Swappiness Parameter (Optional)

vm.swappiness controls how aggressively the kernel uses swap. Values range from 0 (minimal use) to 100 (aggressive). The default is usually 60.

Check the current value:

cat /proc/sys/vm/swappiness

Change temporarily (until reboot):

sudo sysctl vm.swappiness=10

For a permanent change, add to /etc/sysctl.conf:

vm.swappiness=10

And apply: sudo sysctl -p.

Removing the Swap File (if needed)

  1. Disable swap:
    sudo swapoff -v /swapfile
    
  2. Delete the file:
    sudo rm /swapfile
    
  3. Remove the line from /etc/fstab (if you added one).
  4. Check swapon --show—the file should no longer be in the list.

Additional Recommendations

Choosing a Location for the Swap File

  • SSD vs HDD: Swap on SSD is faster but increases wear. For modern SSDs with high endurance, this is not critical. On HDD, swap will be slower but better than a system crash.
  • Location: Place the swap file in the root partition (/) or a separate one with enough free space. Avoid network file systems (NFS)—swap requires low latency.

Monitoring Usage

Regularly check if swap is being used:

watch -n 5 free -h

If used in the Swap line is often above zero, you may need more RAM or to optimize applications.

Swap File and Hibernation

If you use hibernation (saving state to swap before shutdown), the swap file size must be at least equal to the amount of RAM. Otherwise, hibernation will not work.

Increase Swap File Size Without Reboot

  1. Disable current swap: sudo swapoff /swapfile.
  2. Increase the file: sudo fallocate -l 8G /swapfile (or dd).
  3. Repeat steps 2-4 (permissions, mkswap, swapon).
  4. Update fstab if the path or UUID changed.

Conclusion

A swap file is a flexible and straightforward way to add virtual memory in Linux. By following these steps, you can quickly create, configure, and activate swap, improving system responsiveness when RAM is low. Remember to use a reasonable swap size and monitor usage to avoid excessive disk load.

F.A.Q.

Can I use a swap file instead of a swap partition?
What size should the swap file be?
How to remove a swap file?
Does swap affect performance?

Hints

Check current swap
Create swap file
Set permissions
Format as swap
Activate swap file
Add to fstab
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