Skip to content

How to Increase Swap Space 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 increase swap space in Linux without a reboot, add a second swap file next to the swap you already have: create it with fallocate, run chmod 600, mkswap and swapon, and add it to /etc/fstab. The kernel uses both at once. To make an existing swap file bigger instead, turn it off, grow it, run mkswap again and turn it back on. This guide is for a server that already has swap and needs more of it.

No swap at all yet? Start with How to Create a Swap File in Linux (or How to Add Swap Space on Ubuntu 24.04 and 26.04). On Btrfs, read File Based Swap on Linux: ext4, XFS and Btrfs first, because a plain swap file does not work there.

Before you add more: does the server need it?

Check what you have now:

swapon --show
free -h

More swap helps when a server runs out of swap during short peaks. If it swaps all the time (vmstat 1 5 shows numbers above zero in the si and so columns minute after minute), adding swap will not make it faster: it needs more RAM. How to Check Swap Usage in Linux and Find What Uses It shows how to tell the two apart, and How Much Swap Should You Use in Linux? covers sizing. The old rule of "twice the RAM" comes from small machines and rarely fits a modern server.

Method 1: add a second swap file (nothing to turn off)

This works even while the server is short of memory, because nothing has to be moved. In the lab example below the server had 365 MiB of RAM, almost all of it in use, and 264 MiB already in the first swap file.

  1. Create, protect and format the new file (here 1 GB, named /swapfile2):
    sudo fallocate -l 1G /swapfile2
    sudo chmod 600 /swapfile2
    sudo mkswap /swapfile2
  2. Turn it on:
    sudo swapon /swapfile2
  3. Check that both are active:
    swapon --show

    Output (lab example):

    NAME       TYPE  SIZE USED PRIO
    /swapfile  file 1024M 267M   -2
    /swapfile2 file 1024M   0B   -3
  4. Add it to /etc/fstab so it is used after a reboot:
    /swapfile2 none swap defaults 0 0

Method 2: make the existing swap file bigger

An active swap file cannot be resized, so it has to be turned off first. swapoff moves everything in that file back into RAM or into other active swap. If there is not enough room, the out-of-memory killer can end swapoff itself. On a busy server, do Method 1 first: with a second swap file active, the pages move there. In our lab, swapoff /swapfile moved 264 MiB into /swapfile2 while only 6 MiB of RAM was free.

  1. Turn the file off:
    sudo swapoff /swapfile
  2. Grow it to the new total size (here 2 GB):
    sudo fallocate -l 2G /swapfile
  3. Write a new swap signature. Do not skip this: the old signature still holds the old size, and in our test swapon kept using only 1024M of a file grown to 1.5G until mkswap ran again.
    sudo mkswap /swapfile

    Output (lab example):

    mkswap: /swapfile: warning: wiping old swap signature.
    Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    no label, UUID=e7b80ce0-a74a-45b3-8635-3a5ef4398d7d
  4. Turn it on and check:
    sudo swapon /swapfile
    swapon --show

The fstab line stays the same, because the file name has not changed. If you used Method 1 only to free the old file and no longer need the extra one, remove it with the steps in How to Turn Swap Off in Linux and Disable It Permanently.

Which swap is used first: priorities

In Method 1 the new file showed 0B used: without a set priority, the kernel numbers swap areas -2, -3 and so on in the order they are turned on, and fills the higher one first. If one swap file sits on a faster disk, give it the higher priority (0 to 32767, higher is used first) with pri= in the fstab options:

/swapfile2 none swap defaults,pri=10 0 0

Output of swapon --show after sudo swapon -a (lab example, after both files were resized):

NAME       TYPE SIZE USED PRIO
/swapfile  file   2G   0B   -2
/swapfile2 file 1.5G   0B   10

Method 3: swap on an LVM volume

AlmaLinux, Rocky Linux and RHEL often use an LVM volume for swap. According to Red Hat, extend it like this (the volume name is Red Hat's example; swapon --show may list yours only as /dev/dm-1 or similar, and sudo lvs shows its real name):

sudo swapoff -v /dev/VolGroup00/LogVol01
sudo lvresize /dev/VolGroup00/LogVol01 -L +2G
sudo mkswap /dev/VolGroup00/LogVol01
sudo swapon -v /dev/VolGroup00/LogVol01

The volume group needs free space for this. If it has none, adding a swap file (Method 1) is the simpler way. A plain swap partition cannot grow without repartitioning, so add a swap file next to it.

Common errors

  • mkswap: error: /swapfile is mounted; will not make swapspace: the file is still active. Run swapoff first.
  • swapon failed: Device or resource busy: that file is already on.
  • rm: cannot remove '/swapfile2': Operation not permitted: current kernels refuse to delete an active swap file. Turn it off first.

A KVM VPS runs its own kernel, so you can add swap files on it yourself. If a VPS keeps filling its swap, a plan with more RAM is the real fix; see KVM VPS hosting.

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

Was this answer helpful?

Still stuck? Open a ticket