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

Add swap space on Debian 12 with a protected 1 GiB file, persistent boot setup, safe tuning, verification, and fixes for common VPS errors today.
A glowing swap file block beside a Debian server with subtle red accents on a midnight-blue background.

Add swap space on Debian 12 by creating a root-only 1 GiB swap file and enabling it now and after reboots. The setup normally takes five to ten minutes and gives a small VPS extra disk-backed headroom during short memory spikes.

Prerequisites

  • Debian 12 (Bookworm) on a KVM VPS or dedicated server, with at least 1.2 GiB free on the filesystem mounted at / for this example.
  • SSH access as root, or a regular user with working sudo privileges.
  • The /swapfile path must be unused; do not overwrite an existing file or active swap area.
  • A VPSLake Linux VPS if you need a Debian server for this procedure.

Swap is much slower than RAM, so it is a safety buffer rather than a substitute for choosing a server with enough memory. The commands below use 1 GiB; a memory-heavy workload may need a different size and enough free disk to support it.

Step 1: Inspect Debian 12 and the current storage state

Check the release, root filesystem, free space, and active swap before creating anything so you do not duplicate swap or fill the system disk.

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 12 VPS without active swap, the version begins with 12, swapon --show has no data rows, and the last command confirms that the target path is available:

12.12
ext4
               total        used        free      shared  buff/cache   available
Mem:            2.0Gi       410Mi       1.1Gi        12Mi       480Mi       1.4Gi
Swap:              0B          0B          0B
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  6.5G   34G  17% /
/swapfile is available

The Debian 12 minor release and device names can differ. If swapon --show already lists a partition or file, keep it unless you have a specific reason to add more; if /swapfile exists, choose another unused filename instead of replacing it.

Step 2: Create and protect a 1 GiB swap file

Write a fully allocated file and lock it to root before adding the swap signature, which avoids sparse-file surprises and prevents other local users from reading pages written to disk.

For the usual ext4 VPS filesystem, run:

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

dd should finish after writing 1,073,741,824 bytes. mkswap creates the metadata that identifies the file as swap; its UUID is informational because the persistent entry below refers to /swapfile by path.

Setting up swapspace version 1, size = 1 GiB
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

If Step 1 reported btrfs, use Debian 12’s Btrfs-aware helper instead of the dd block above. It creates a suitable non-copy-on-write swap file:

sudo btrfs filesystem mkswapfile --size 1G /swapfile

The command requires the btrfs-progs package. Install it only on a Btrfs root filesystem if the command is missing:

sudo apt update
sudo apt install -y btrfs-progs
sudo btrfs filesystem mkswapfile --size 1G /swapfile

For the underlying commands and file format, see Debian’s mkswap documentation.

Step 3: Turn swap on for this boot

Creating the signature does not activate the file; swapon registers it with the running kernel without requiring a restart.

sudo swapon /swapfile
sudo swapon --show

You should now see a row similar to this:

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

The priority may be different if another swap device is already enabled. The important checks are that /swapfile is listed and that its size is close to 1 GiB.

Step 4: Persist the setting across reboots

Add one fstab record so Debian enables 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 final command should show exactly one active configuration line:

/swapfile none swap sw 0 0

The file is already active for the current session from Step 3. The fstab line is what tells Debian to enable it again after a reboot; systemctl daemon-reload refreshes systemd’s generated view of the changed configuration.

Step 5: Review swappiness before changing it

vm.swappiness controls how readily the kernel moves inactive pages to swap, so inspect the current value and only change it for a workload you are monitoring.

sysctl vm.swappiness

If this VPS should prefer RAM and use swap mainly during pressure, test 10 for the current boot:

sudo sysctl vm.swappiness=10

If that setting suits the workload, save it in a separate sysctl file and apply it:

printf '%s\n' 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/60-swap.conf
sudo sysctl -p /etc/sysctl.d/60-swap.conf
sysctl vm.swappiness

Expected output is:

vm.swappiness = 10

Do not set the value to zero merely to avoid disk activity. Frequent swapping is a sign that the service needs more RAM or tighter memory limits, not that the kernel needs to be forced to wait longer.

Verify the persistent Debian 12 swap setup

Check the active kernel table, reported capacity, file permissions, and boot entry together; this proves the file works now and is configured for the next restart.

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

Successful output contains a 1 GiB /swapfile, a nonzero Swap total, root-only permissions, 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 Btrfs with copy-on-write still enabled. Run findmnt -no FSTYPE /; for Btrfs, remove only an inactive failed file and use the btrfs filesystem mkswapfile command in Step 2. For ext4 or another normal filesystem, recreate the inactive file with the dd sequence, then run mkswap again.

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

The root filesystem cannot provide the requested 1 GiB. Confirm the available space with df -h /, preserve room for Debian updates and logs, and use a smaller inactive file such as 512 MiB:

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

Only run rm after sudo swapon --show confirms that the failed file is not active. Keep the same /etc/fstab path; it does not encode the file size.

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

The path is already active or another process has it open. Run sudo swapon --show and ls -l /swapfile; if the file is listed as active, keep it and remove any duplicate fstab line instead of overwriting it.

swapon: /swapfile: swapon failed: Operation not permitted

Some container-based VPS platforms do not let the guest manage swap. This is a virtualization policy rather than a missing Debian package, so ask the host to provide swap or move the workload to a virtualization plan that permits guest-managed swap files.

FAQ

How much swap should a Debian 12 VPS have?

There is no universal ratio. One GiB is a reasonable starting point for a small server when the disk has room, but size it around short memory spikes and leave storage for the OS, logs, and application data.

Does swap increase physical RAM?

No. Swap is disk-backed virtual memory and is substantially slower than RAM. It can delay an out-of-memory failure during a brief spike, while continuous swap activity usually means the VPS needs more memory or the application needs tighter limits.

Do I need to reboot after adding swap on Debian 12?

No. swapon /swapfile activates it immediately, and the /etc/fstab entry handles later boots. A restart is useful only if you want to test the boot configuration during a maintenance window.

How do I remove a swap file safely?

Confirm the exact path with sudo swapon --show, run sudo swapoff /swapfile, and remove only its line from /etc/fstab. After checking that the file is inactive, delete it with sudo rm /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

RDP Free Trial vs Paid RDP: What You Really Get

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 ✨