Skip to content

How to Create a Swap File in Linux

Updated 5 min read

Updated: 22 October 2026 · Applies to: Debian 13, Ubuntu 24.04 and 26.04, AlmaLinux and Rocky Linux 9 and 10, Fedora 44; tested with util-linux 2.41 on a Linux 6.12 kernel

To create a swap file in Linux, run sudo fallocate -l 2G /swapfile, protect it with sudo chmod 600 /swapfile, format it with sudo mkswap /swapfile, turn it on with sudo swapon /swapfile, and add /swapfile none swap defaults 0 0 to /etc/fstab so it comes back after a reboot. This guide walks through those steps once, for a server that has no swap file yet, on any current distribution.

Related guides for other jobs: if you already have swap and want more, see How to Increase Swap Space in Linux; for the Ubuntu version of these steps, see How to Add Swap Space on Ubuntu 24.04 and 26.04; if your disk uses Btrfs or you are deciding between a swap file and a partition, see File Based Swap on Linux: ext4, XFS and Btrfs.

What a swap file does

Swap is disk space the kernel uses when RAM runs short: memory pages that have not been used for a while are moved to disk, which frees RAM for active programs. A swap file is an ordinary file that holds this swap area, so you need no free partition. Disk is far slower than RAM, so swap is a safety net against out-of-memory crashes, not a replacement for memory. For how big it should be, see How Much Swap Should You Use in Linux?; 1 to 2 GB is a common choice on a small server.

Step 1: check for existing swap and free disk space

swapon --show

No output means the server has no swap. Then check that the root file system has room for the file:

df -h /

Step 2: create the file

On ext4 and XFS, fallocate reserves the space at once:

sudo fallocate -l 2G /swapfile

If fallocate is not available, or swapon later says the file has holes, use dd, which the swapon manual calls the most portable method. This writes 2048 blocks of 1 MiB, so 2 GiB:

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

Never create the file with truncate, cp or a dd command with seek=: those make sparse files, and the kernel refuses them. On Btrfs a plain fallocate file fails too; follow the Btrfs steps in File Based Swap on Linux: ext4, XFS and Btrfs.

With util-linux 2.40 or later (Debian 13, Ubuntu 26.04, AlmaLinux and Rocky Linux 10, Fedora 44), mkswap can create, size and format the file in one step, with mode 0600 already set. Ubuntu 24.04 (2.39) and AlmaLinux 9 (2.37) do not have this option. If you use it, skip to step 5:

sudo mkswap --file --size 2G /swapfile

Step 3: allow only root to read it

Swap holds copies of memory, which can include passwords and keys, so only root may read the file:

sudo chmod 600 /swapfile

If you skip this, swapon still works but warns insecure permissions 0644, 0600 suggested.

Step 4: format it as swap

sudo mkswap /swapfile

Output (lab example with a 1 GB file):

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=cf8568ce-01d2-4628-8eb1-de8b64301670

Step 5: turn it on and check

sudo swapon /swapfile
swapon --show

Output:

NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2
free -h

Output (lab machine with 365 MiB of RAM):

               total        used        free      shared  buff/cache   available
Mem:           365Mi        23Mi       342Mi          0B       7.3Mi       342Mi
Swap:          1.0Gi          0B       1.0Gi

Step 6: keep it after a reboot

Open /etc/fstab with an editor and add this line at the end:

/swapfile none swap defaults 0 0

The fields are: the file, none (swap has no mount point), the type swap, the options (defaults; Ubuntu writes sw, which works the same), and two zeros for the old dump backup flag and the boot-time fsck order, which do not apply to swap. On systemd distributions, reload the generated units so the new line is picked up:

sudo systemctl daemon-reload

Test the fstab line without rebooting: turn the file off, let swapon -a read /etc/fstab, and check that the file is back.

sudo swapoff /swapfile
sudo swapon -a
swapon --show

A wrong file name shows up here as an error (swapon: cannot open /swapfil: No such file or directory) instead of at the next boot. A typo in the third field, such as swp, gives no error at all: the line is simply skipped, so make sure swapon --show lists the file again.

Common problems

After it is running

To see how much of the swap is used and by which programs, and to tune how eagerly Linux swaps (the vm.swappiness setting), see How to Check Swap Usage in Linux and Find What Uses It. To remove the swap file again, see How to Turn Swap Off in Linux and Disable It Permanently. If the server swaps all the time, it needs more RAM rather than more swap.

Official documentation: mkswap(8) and swapon(8).

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket