> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Add Swap Space on Ubuntu and Other Linux

> Add swap on Ubuntu and other Linux with a swap file: fallocate -l 2G /swapfile, chmod 600, mkswap, swapon, then add it to /etc/fstab so it stays after reboot.

To add swap on Ubuntu (and Debian, AlmaLinux or Rocky Linux), create a swap file and turn it on: `sudo fallocate -l 2G /swapfile`, `sudo chmod 600 /swapfile`, `sudo mkswap /swapfile`, `sudo swapon /swapfile`. Then add `/swapfile none swap defaults 0 0` to `/etc/fstab` so it is used after every reboot. Check it with `swapon --show`.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 5 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux and Rocky 9 / 10, Fedora | sudo, free disk space for the swap file |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md). Commands use `sudo`; root users can leave it out.
* **Swap** is disk space Linux uses as overflow when RAM is full. It stops programs being killed when memory runs out, but it is much slower than RAM. If your server swaps all the time, it needs more memory. See [check memory usage](https://help.freakhosting.com/portals/other/how-to-check-memory-usage-on-linux.md).
* Make sure you have room: `df -h /`. Leave plenty of free space after adding the swap file.

## How to check if you already have swap

```bash theme={null}
swapon --show
free -h
```

If `swapon --show` prints nothing and `free -h` shows `Swap: 0B`, there is no swap.

## How much swap do I need?

Red Hat's guidance for servers without hibernation:

| RAM | Recommended swap |
| - | - |
| Less than 2 GB | 2 × RAM |
| 2 GB to 8 GB | Same as RAM |
| 8 GB to 64 GB | At least 4 GB |
| More than 64 GB | Depends on the workload, at least 4 GB |

These are starting points. For a small VPS, 1–2 GB of swap is a common safety net.

## How to create a swap file

**1. Create the file**

```bash theme={null}
sudo fallocate -l 2G /swapfile
```

If `fallocate` fails, use `dd` instead (2048 blocks of 1 MiB = 2 GiB):

```bash theme={null}
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
```

**2. Lock down permissions**

```bash theme={null}
sudo chmod 600 /swapfile
```

Only root may read swap, because it can contain passwords and other data from memory.

**3. Format it as swap**

```bash theme={null}
sudo mkswap /swapfile
```

```text theme={null}
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=a0bf1a22-defa-4186-863b-ffcac8f0bc7f
```

**4. Turn it on**

```bash theme={null}
sudo swapon /swapfile
```

**5. Keep it after reboot**

Back up `/etc/fstab`, then add the swap line:

```bash theme={null}
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
```

> [!WARNING]
>
> A typo in `/etc/fstab` can stop your server booting normally. Use `tee -a` (append) exactly as shown, never `>` which would overwrite the whole file. Check the file with `sudo findmnt --verify` before you reboot. If the server does not start, use the [VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md) or [rescue mode](https://help.freakhosting.com/portals/vps-panel/how-to-use-rescue-mode.md) on a VPS to restore `/etc/fstab.bak`.

### Swap on Btrfs (Fedora)

On a Btrfs file system the steps above do not work, because a swap file must not be copy-on-write. With btrfs-progs 6.1 or newer, create it in one step:

```bash theme={null}
sudo btrfs filesystem mkswapfile --size 2G /swapfile
sudo swapon /swapfile
```

Check your file system type with `df -T /`.

## How to check it worked

```bash theme={null}
swapon --show
```

```text theme={null}
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2
```

`free -h` now shows the swap total on the `Swap:` line.

## How to change swappiness

**Swappiness** (0–100) controls how readily Linux moves memory to swap. The default is `60`. Many servers use a lower value, such as `10`, so swap is used mainly when RAM is nearly full.

```bash theme={null}
cat /proc/sys/vm/swappiness                                   # current value
sudo sysctl vm.swappiness=10                                  # change it now
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf   # keep it after reboot
```

## How to make swap bigger or remove it

To resize, turn the swap file off, recreate it at the new size and turn it on again:

```bash theme={null}
sudo swapoff /swapfile
sudo fallocate -l 4G /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
```

`mkswap` warns `wiping old swap signature`; that is expected.

To remove swap: run `sudo swapoff /swapfile`, delete the `/swapfile` line from `/etc/fstab`, then `sudo rm /swapfile`.

> [!NOTE]
>
> `swapoff` moves everything in swap back into RAM. If RAM is nearly full, stop some services first or it can fail.

## Frequently asked questions

### Does adding swap give my VPS more RAM?

No. Swap lets the server survive short memory peaks without killing processes, but disk is far slower than RAM. If `free -h` shows swap in use all the time, upgrade your plan or reduce memory use.

### What if fallocate fails?

Some file systems do not support it. Use the `dd` command shown above instead. On Btrfs, use `btrfs filesystem mkswapfile`.

### Should I use a swap file or a swap partition?

On a server you already run, a swap file is easier: you can add, resize or remove it without repartitioning the disk.

### Why are my processes still killed when I have swap?

If both RAM and swap fill up, the kernel's out-of-memory killer still stops processes. See [fix out-of-memory killed processes](https://help.freakhosting.com/portals/other/how-to-fix-out-of-memory-killed-processes.md).

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Check memory usage](https://help.freakhosting.com/portals/other/how-to-check-memory-usage-on-linux.md)
>
> See how much RAM is in use.

> [card] [Fix out-of-memory kills](https://help.freakhosting.com/portals/other/how-to-fix-out-of-memory-killed-processes.md)
>
> When processes are killed for memory.

> [card] [Check disk space](https://help.freakhosting.com/portals/other/how-to-check-disk-space-on-linux.md)
>
> Make sure there is room for swap.

> [card] [Monitor VPS resources](https://help.freakhosting.com/portals/vps-panel/how-to-monitor-vps-resource-usage.md)
>
> Memory graphs in the VPS Panel.

***

> [!NOTE]
>
> **Checked against the official [Red Hat Enterprise Linux 9 swap documentation](https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/managing_storage_devices/getting-started-with-swap_managing-storage-devices), the [Ubuntu SwapFaq](https://help.ubuntu.com/community/SwapFaq) and the [Btrfs swapfile documentation](https://btrfs.readthedocs.io/en/latest/Swapfile.html)** on 4 November 2026; `fallocate`, `mkswap`, `swapon`, `swapoff` and `swapon --show` tested on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.