Adding a swap file on Debian gives a server extra disk-backed memory without repartitioning the disk. It is useful on small virtual machines, lab hosts, and minimal installs where no swap area was created, but it is slower than RAM and should not be used as a substitute for enough physical memory.
Current Debian releases provide swap management through util-linux. The mkswap –file command creates a file with the correct permissions and swap metadata, swapon activates swap areas, and systemd turns /etc/fstab swap entries into boot-time swap units.
The example below creates a 2 GiB /swapfile on the root filesystem. Use a local filesystem that is available during boot, adjust the size for the workload and free disk space, and keep a backup of /etc/fstab before adding the persistent entry.
Related: Check swap memory usage in Linux
Related: Remove a swap file in Linux
Steps to create a swap file on Debian:
- Check whether swap is already active.
$ swapon --show
No output means no active swap areas are currently enabled. If rows appear, choose a new swap file path that does not duplicate an existing entry.
- Confirm that the target filesystem has enough free space.
$ df -h / Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G 12G 26G 32% /
The example creates a 2 GiB file under /swapfile, so the root filesystem needs at least that much free space plus normal operating headroom.
- Create the swap file and initialize swap metadata.
$ sudo mkswap --file --size 2G /swapfile Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=74b69c44-f7e9-4b77-a29f-9f01cb4f8328
mkswap –file creates the file, allocates the requested size, applies restricted permissions, and writes the swap header in one step.
- Back up /etc/fstab before adding the boot-time swap entry.
$ sudo cp /etc/fstab /etc/fstab.bak
An incorrect /etc/fstab line can delay boot or require recovery-mode editing. Keep the backup until the next successful boot.
- Add the swap file entry to /etc/fstab.
$ sudoedit /etc/fstab
/swapfile none swap defaults 0 0
The line should appear once. The swap type tells swapon -a and systemd that the file is a swap area rather than a mounted filesystem.
- Reload systemd so it rereads the changed /etc/fstab.
$ sudo systemctl daemon-reload
Debian systems using systemd generate swap units from /etc/fstab, so reloading the manager avoids stale unit state after the edit.
- Activate swap entries from /etc/fstab.
$ sudo swapon -a
The command returns no output when the configured swap entries activate successfully.
- Verify that the new swap file is active.
$ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2
The /swapfile row confirms that Debian is using the new swap file for paging and that the persistent /etc/fstab entry is valid enough for swapon -a.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.