Add swap space on Debian 13 by creating a root-only 1 GiB swap file and enabling it for the current boot and future restarts. You can finish the setup in about five to ten minutes, including checks for Btrfs, disk space, and boot persistence.
Prerequisites
- A Debian 13 (Trixie) VPS or dedicated server with at least 1.5 GiB of free space on the filesystem mounted at
/for the example file. - SSH access as
root, or a normal user with workingsudoprivileges. - The path
/swapfilemust be unused; do not overwrite an existing file or an active swap area. - A VPSLake Linux VPS if you need a Debian server for this procedure.
Swap is slower than RAM. It is useful as a buffer during a short memory spike, but persistent swapping is a sign that the workload needs more memory or tighter limits.
Step 1: Inspect Debian 13, storage, and existing swap
Check the release, root filesystem, available disk space, and current swap before creating anything so you do not duplicate swap or fill the system disk.
grep -E '^(PRETTY_NAME|VERSION_ID)=' /etc/os-release
findmnt -no FSTYPE /
df -h /
free -h
swapon --show
test -e /swapfile && ls -lh /swapfile || echo "/swapfile is available"
On a Debian 13 VPS without active swap, the first line identifies Trixie, swapon --show has no data rows, and the final check confirms that the target path is free:
PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
VERSION_ID="13"
ext4
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 6.4G 32G 17% /
total used free shared buff/cache available
Mem: 2.0Gi 430Mi 1.0Gi 12Mi 590Mi 1.4Gi
Swap: 0B 0B 0B
/swapfile is available
Device names, memory values, and the root filesystem will differ on your server. If swapon --show already lists a swap partition or file, you may not need another one. If /swapfile exists, stop before Step 2 and choose another unused path instead of replacing it.
Step 2: Create a protected 1 GiB swap file
Create a fully allocated file and restrict it to the root user before Linux accepts it as swap; this avoids sparse-file surprises and protects memory pages written to disk.
For the usual ext4 or XFS root filesystem, create the file with dd:
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
The write should finish at 1,073,741,824 bytes. mkswap then writes the swap signature and prints a generated UUID:
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
If Step 1 reported btrfs, use Debian 13’s Btrfs-aware helper instead. Btrfs swap files need preallocated, non-copy-on-write blocks, and the helper sets those properties for you:
sudo apt-get update
sudo apt-get install -y btrfs-progs
sudo btrfs filesystem mkswapfile --size 1G /swapfile
sudo chmod 0600 /swapfile
Do not run both creation sequences. The Debian swapon manual explains why sparse and copy-on-write files can be rejected, while Debian’s Btrfs filesystem manual documents mkswapfile.
Step 3: Enable the file immediately
Preparing a swap area and turning it on are separate operations; swapon registers the finished file with the running kernel without a reboot.
sudo swapon /swapfile
sudo swapon --show
The output should contain a row similar to this:
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 appears as active and its capacity is close to 1 GiB. If the command reports an error, leave the file in place and use the matching fix in Troubleshooting rather than creating a second file.
Step 4: Make swap return after a reboot
Add one entry to /etc/fstab so Debian’s boot process activates this same path on every restart, while the guard prevents duplicate entries.
if ! sudo grep -qE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab; then
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab
fi
sudo systemctl daemon-reload
sudo grep -nE '^[[:space:]]*/swapfile[[:space:]]' /etc/fstab
The last command should show one active line like this:
/swapfile none swap defaults 0 0
The file is already active for this session from Step 3. The fstab record is what makes it available on later boots; systemctl daemon-reload refreshes systemd’s generated configuration after the file changes.
Step 5: Review swappiness before tuning it
The vm.swappiness setting influences how readily the kernel moves inactive memory pages to disk, so inspect it first and change it only when you have a workload reason.
sysctl vm.swappiness
If this server should prefer RAM and use swap mainly during pressure, test a value of 10 for the current boot:
sudo sysctl vm.swappiness=10
If monitoring shows that value is appropriate, persist it in a separate configuration file:
printf '%s\n' 'vm.swappiness = 10' | sudo tee /etc/sysctl.d/99-swap.conf
sudo sysctl -p /etc/sysctl.d/99-swap.conf
sysctl vm.swappiness
Expected output is:
vm.swappiness = 10
Do not set swappiness to zero simply to eliminate disk activity. A little swap flexibility can help during a sudden shortage, while continuous swapping points to a memory or workload problem.
Verify the persistent Debian 13 swap setup
Run one final check for activation, capacity, permissions, and the boot entry. Together these checks prove that the kernel is using the file now and that Debian knows how to restore it 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 includes an active 1 GiB file, 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 defaults 0 0
Troubleshooting
swapon: /swapfile: swapon failed: Invalid argument
The file is often sparse, partly allocated, or a copy-on-write file on Btrfs. Confirm the filesystem with findmnt -no FSTYPE /; for Btrfs, remove the inactive failed file only and recreate it with btrfs filesystem mkswapfile --size 1G /swapfile from Step 2. For ext4 or XFS, recreate the inactive file with the dd sequence, then run mkswap and swapon again.
dd: error writing '/swapfile': No space left on device
The root filesystem cannot provide the requested 1 GiB. Check df -h /, free space you recognize as unnecessary, or use a smaller 512 MiB file after confirming the failed file is inactive:
sudo swapon --show
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
Keep the /etc/fstab path unchanged; it does not store the file size. Never run the rm command while /swapfile appears in swapon --show.
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 it is already listed, keep the existing file and remove any duplicate fstab line rather than overwriting it.
swapon: /swapfile: swapon failed: Operation not permitted
Some container-based VPS plans do not allow the guest to manage swap. This is a virtualization policy, not a missing Debian package, so ask the provider for host-managed swap or use a virtualization plan that permits guest swap files.
FAQ
How large should swap be on Debian 13?
There is no universal size or RAM-to-swap formula. A 1 GiB file is a reasonable starting point for a small VPS when the disk has room; choose based on the workload’s short memory spikes and leave storage for updates, logs, and application data.
Does swap increase the VPS’s physical RAM?
No. Swap is disk-backed virtual memory and is much slower than RAM. It can delay an out-of-memory failure during a brief spike, but sustained swap activity means the server needs more memory or the application needs tighter limits.
Can Debian 13 use a swap file on Btrfs?
Yes, but a normal sparse or copy-on-write file can be rejected. Use btrfs filesystem mkswapfile as shown in Step 2, and remember that an active swap file affects some Btrfs operations such as snapshotting its containing subvolume.
How do I remove the swap file later?
Confirm the exact path with sudo swapon --show, run sudo swapoff /swapfile, and remove only its line from /etc/fstab. After confirming it is inactive, delete it with sudo rm /swapfile; do not remove an active swap file.