How to Add Swap Space on Debian 11 (Step-by-Step)

Add swap space on Debian 11 with a protected 1 GB swap file, persistent boot setup, verification checks, tuning guidance, and fixes for common errors.
A glowing swap storage block is being added to a dark Linux server in a blue-lit server room.

Add swap space on Debian 11 by creating a protected 1 GB swap file and enabling it now and after every reboot. The process usually takes five to ten minutes and gives a small VPS a disk-backed buffer during short RAM spikes.

Prerequisites

  • A Debian 11 (Bullseye) VPS with at least 1.2 GB of free space on the filesystem mounted at /; keep additional room for updates, logs, and applications.
  • An SSH session as root, or a regular account that can run commands with sudo.
  • A filesystem that supports ordinary swap files, such as ext4. Run the filesystem check in Step 1 before creating /swapfile.
  • A VPSLake Linux VPS if you need a Debian server for this procedure.

If you are already logged in as root, remove sudo from the examples. A regular account should keep it so the file and system configuration are changed with administrator privileges.

Swap is slower than physical memory. Treat it as protection against a brief memory surge, not as a replacement for choosing a VPS with enough RAM.

Step 1: Check Debian, storage, and current swap

Inspect the server first so you do not overwrite an existing file, fill the root filesystem, or create duplicate swap.

cat /etc/debian_version
findmnt -no FSTYPE /
free -h
swapon --show
df -h /
test -e /swapfile && ls -lh /swapfile || echo "/swapfile is available"

On a Debian 11 VPS without active swap, the version begins with 11, swapon --show prints no swap rows, and the last line confirms that the target path is free:

11.11
ext4
               total        used        free      shared  buff/cache   available
Mem:            2.0Gi       410Mi       1.1Gi        12Mi       480Mi       1.4Gi
Swap:              0B          0B          0B
/swapfile is available

If swapon --show already lists a swap partition or file, you usually do not need another one. If /swapfile exists, do not run the creation command in the next step; choose another unused path or manage the existing file instead.

Step 2: Create a root-only 1 GB swap file

Write the file block by block, then restrict access before turning it into swap so other local users cannot read pages that the kernel stores there.

Run these commands only after confirming that /swapfile is available:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
sudo chown root:root /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile

The dd command creates a fully allocated file instead of relying on sparse-file behavior. mkswap should report a new swapspace and a size close to 1 GiB; its generated UUID is informational because this guide refers to the file by path. Debian documents the same mkswap and swapon tools in its Bullseye mkswap manual.

1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied
Setting up swapspace version 1, size = 1 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Step 3: Enable swap for the current session

Initialize-and-enable are separate operations: mkswap writes the swap signature, while swapon tells the running kernel to use the file.

sudo swapon /swapfile
sudo swapon --show

The new file should appear immediately:

NAME      TYPE  SIZE USED PRIO
/swapfile file    1G   0B   -2

The priority may differ if Debian already has another swap area. The important result is that /swapfile is listed with a size close to the one you created. If swapon returns Operation not permitted, see the troubleshooting section rather than repeatedly retrying the command.

Step 4: Make the swap file persistent

Add one /etc/fstab entry so Debian can activate the file during boot, while checking first to avoid duplicate entries.

if ! sudo grep -qE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab; then
  echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
fi
sudo systemctl daemon-reload
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab

The expected configuration line is:

/swapfile none swap sw 0 0

You do not need to reboot. The swapon command in Step 3 already enabled the file for this boot; the fstab line is what makes the setting survive future restarts. Debian’s Swap wiki page explains the file creation and persistent configuration model.

Step 5: Review swappiness before changing it

vm.swappiness influences how readily Debian moves inactive memory pages to swap. The default is a reasonable starting point, so inspect it first and change it only for a workload you are monitoring.

sysctl vm.swappiness

If you want the kernel to prefer RAM and use swap mainly when memory pressure rises, test a value of 10 for the current boot:

sudo sysctl vm.swappiness=10

If monitoring shows that this suits the server, save it in a dedicated file and apply it:

echo 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl -p /etc/sysctl.d/99-swap.conf

Do not set swappiness to zero merely to avoid disk activity. A little flexibility can be safer during a sudden memory shortage, while constant swapping is a sign that the workload needs more RAM or tighter limits.

Verify the persistent Debian 11 swap setup

Check activation, capacity, ownership, permissions, and the boot entry together. This proves both that the kernel is using the file now and that Debian has the information needed after a restart.

sudo swapon --show
free -h
sudo stat -c '%A %U:%G %n' /swapfile
sudo grep -E '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab

Successful output contains a 1 GB /swapfile, a nonzero Swap total, root-only mode, and one matching fstab line:

NAME      TYPE  SIZE USED PRIO
/swapfile file    1G   0B   -2
Swap:          1.0Gi          0B     1.0Gi
-rw------- root:root /swapfile
/swapfile none swap sw 0 0

Troubleshooting

swapon: /swapfile: swapon failed: Invalid argument

The file may be sparse, partly allocated, or on a filesystem with swap-file restrictions. Check findmnt -no FSTYPE /; if it reports btrfs, do not keep retrying the ext4-style file. Remove only an inactive failed file, then create a no-copy-on-write file before allocating its blocks:

sudo rm /swapfile
sudo truncate -s 0 /swapfile
sudo chattr +C /swapfile
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

On ext4, recreate the inactive file with the dd sequence from Step 2. If the Btrfs fallback is unavailable on your image, use a provider-managed swap partition instead of disabling filesystem safety features.

dd: error writing '/swapfile': No space left on device

The root filesystem cannot provide the full 1 GB. Confirm the available space with df -h /, preserve room for Debian updates and logs, and recreate the file at 512 MB only after removing the inactive failed file:

sudo rm /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=512 status=progress
sudo chown root:root /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

swapon: /swapfile: swapon failed: Device or resource busy

The path is already active or is being used by another process. Run sudo swapon --show and inspect ls -lh /swapfile; if it is already listed, keep it and remove any duplicate fstab line instead of overwriting the file.

swapon: /swapfile: swapon failed: Operation not permitted

Some container-style VPS platforms prevent a guest from enabling its own swap. This is a virtualization policy, not a missing Debian package; ask the provider to enable host-level swap or use a virtualization plan that permits guest swap files.

FAQ

How much swap should a Debian 11 VPS have?

A 1 GB file is a reasonable starting point for a small VPS when the disk has room. Size it around the workload’s short-term memory spikes, not as a fixed multiple of RAM, and leave enough storage for the OS, logs, and application data.

Does swap add physical RAM to Debian?

No. Swap is disk-backed virtual memory and is much slower than RAM. It can give a process more time during a temporary spike, but repeated heavy swapping means the VPS needs more memory or the application needs tighter limits.

Can I add swap without rebooting Debian 11?

Yes. swapon /swapfile activates it immediately. The /etc/fstab entry handles later boots, so a restart is not part of the setup.

How do I remove the swap file safely?

Confirm the exact path with sudo swapon --show, then run sudo swapoff /swapfile and remove only its line from /etc/fstab. After confirming it is inactive, delete /swapfile; never remove an active swap file first.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

Private RDP vs Shared RDP: Which Should You Buy?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨