{"id":891,"date":"2026-10-21T13:00:00","date_gmt":"2026-10-21T13:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=891"},"modified":"2026-10-21T13:00:00","modified_gmt":"2026-10-21T13:00:00","slug":"add-swap-space-debian-13","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/21\/add-swap-space-debian-13\/","title":{"rendered":"How to Add Swap Space on Debian 13 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>A Debian 13 (Trixie) VPS or dedicated server with at least 1.5 GiB of free space on the filesystem mounted at <code>\/<\/code> for the example file.<\/li>\n\n\n<li>SSH access as <code>root<\/code>, or a normal user with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>The path <code>\/swapfile<\/code> must be unused; do not overwrite an existing file or an active swap area.<\/li>\n\n\n<li>A <a href=\"https:\/\/vpslake.com\/buy-vps\">VPSLake Linux VPS<\/a> if you need a Debian server for this procedure.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect Debian 13, storage, and existing swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -E '^(PRETTY_NAME|VERSION_ID)=' \/etc\/os-release\nfindmnt -no FSTYPE \/\ndf -h \/\nfree -h\nswapon --show\ntest -e \/swapfile &amp;&amp; ls -lh \/swapfile || echo \"\/swapfile is available\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On a Debian 13 VPS without active swap, the first line identifies Trixie, <code>swapon --show<\/code> has no data rows, and the final check confirms that the target path is free:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PRETTY_NAME=\"Debian GNU\/Linux 13 (trixie)\"\nVERSION_ID=\"13\"\next4\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  6.4G   32G  17% \/\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       430Mi       1.0Gi        12Mi       590Mi       1.4Gi\nSwap:              0B          0B          0B\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Device names, memory values, and the root filesystem will differ on your server. If <code>swapon --show<\/code> already lists a swap partition or file, you may not need another one. If <code>\/swapfile<\/code> exists, stop before Step 2 and choose another unused path instead of replacing it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create a protected 1 GiB swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the usual ext4 or XFS root filesystem, create the file with <code>dd<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dd if=\/dev\/zero of=\/swapfile bs=1M count=1024 status=progress\nsudo chown root:root \/swapfile\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The write should finish at 1,073,741,824 bytes. <code>mkswap<\/code> then writes the swap signature and prints a generated UUID:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1024+0 records out\n1073741824 bytes (1.1 GB, 1.0 GiB) copied\nSetting up swapspace version 1, size = 1 GiB\nno label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If Step 1 reported <code>btrfs<\/code>, use Debian 13\u2019s Btrfs-aware helper instead. Btrfs swap files need preallocated, non-copy-on-write blocks, and the helper sets those properties for you:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update\nsudo apt-get install -y btrfs-progs\nsudo btrfs filesystem mkswapfile --size 1G \/swapfile\nsudo chmod 0600 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Do not run both creation sequences. The <a href=\"https:\/\/manpages.debian.org\/trixie\/util-linux\/swapon.8.en.html\" target=\"_blank\" rel=\"noopener\">Debian <code>swapon<\/code> manual<\/a> explains why sparse and copy-on-write files can be rejected, while Debian\u2019s <a href=\"https:\/\/manpages.debian.org\/trixie\/btrfs-progs\/btrfs-filesystem.8.en.html\" target=\"_blank\" rel=\"noopener\">Btrfs filesystem manual<\/a> documents <code>mkswapfile<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Enable the file immediately<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Preparing a swap area and turning it on are separate operations; <code>swapon<\/code> registers the finished file with the running kernel without a reboot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon \/swapfile\nsudo swapon --show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output should contain a row similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    1G   0B   -2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The priority may differ if Debian already has another swap area. The important result is that <code>\/swapfile<\/code> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Make swap return after a reboot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one entry to <code>\/etc\/fstab<\/code> so Debian\u2019s boot process activates this same path on every restart, while the guard prevents duplicate entries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ! sudo grep -qE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab; then\n  echo '\/swapfile none swap defaults 0 0' | sudo tee -a \/etc\/fstab\nfi\nsudo systemctl daemon-reload\nsudo grep -nE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The last command should show one active line like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile none swap defaults 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The file is already active for this session from Step 3. The <code>fstab<\/code> record is what makes it available on later boots; <code>systemctl daemon-reload<\/code> refreshes systemd\u2019s generated configuration after the file changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Review swappiness before tuning it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> 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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If this server should prefer RAM and use swap mainly during pressure, test a value of 10 for the current boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sysctl vm.swappiness=10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If monitoring shows that value is appropriate, persist it in a separate configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf '%s\\n' 'vm.swappiness = 10' | sudo tee \/etc\/sysctl.d\/99-swap.conf\nsudo sysctl -p \/etc\/sysctl.d\/99-swap.conf\nsysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Expected output is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vm.swappiness = 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent Debian 13 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\nfree -h\nsudo stat -c '%A %U:%G %n' \/swapfile\nsudo grep -E '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Successful output includes an active 1 GiB file, root-only permissions, and one matching <code>fstab<\/code> line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    1G   0B   -2\nSwap:          1.0Gi          0B     1.0Gi\n-rw------- root:root \/swapfile\n\/swapfile none swap defaults 0 0<\/code><\/pre>\n\n\n\n<aside class=\"vl-element vl-cta-banner\" aria-label=\"Add memory headroom\">\n\t<div class=\"vl-cta-banner__content\">\n\t\t<p class=\"vl-cta-banner__eyebrow\">Debian VPS<\/p>\n\t\t<h3 class=\"vl-cta-banner__title\">Add memory headroom<\/h3>\n\t\t<p class=\"vl-cta-banner__text\">Deploy a Debian VPS with room for applications and a protected swap file.<\/p>\n\t<\/div>\n\t<a class=\"vl-cta-banner__button\" href=\"\/buy-vps\" aria-label=\"View Linux VPS plans\">View Linux VPS plans<\/a>\n<\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: \/swapfile: swapon failed: Invalid argument<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The file is often sparse, partly allocated, or a copy-on-write file on Btrfs. Confirm the filesystem with <code>findmnt -no FSTYPE \/<\/code>; for Btrfs, remove the inactive failed file only and recreate it with <code>btrfs filesystem mkswapfile --size 1G \/swapfile<\/code> from Step 2. For ext4 or XFS, recreate the inactive file with the <code>dd<\/code> sequence, then run <code>mkswap<\/code> and <code>swapon<\/code> again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>dd: error writing '\/swapfile': No space left on device<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The root filesystem cannot provide the requested 1 GiB. Check <code>df -h \/<\/code>, free space you recognize as unnecessary, or use a smaller 512 MiB file after confirming the failed file is inactive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\nsudo rm \/swapfile\nsudo dd if=\/dev\/zero of=\/swapfile bs=1M count=512 status=progress\nsudo chown root:root \/swapfile\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the <code>\/etc\/fstab<\/code> path unchanged; it does not store the file size. Never run the <code>rm<\/code> command while <code>\/swapfile<\/code> appears in <code>swapon --show<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: \/swapfile: swapon failed: Device or resource busy<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The path is already active or another process has it open. Run <code>sudo swapon --show<\/code> and <code>ls -l \/swapfile<\/code>; if it is already listed, keep the existing file and remove any duplicate <code>fstab<\/code> line rather than overwriting it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: \/swapfile: swapon failed: Operation not permitted<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How large should swap be on Debian 13?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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\u2019s short memory spikes and leave storage for updates, logs, and application data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does swap increase the VPS\u2019s physical RAM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Debian 13 use a swap file on Btrfs?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but a normal sparse or copy-on-write file can be rejected. Use <code>btrfs filesystem mkswapfile<\/code> as shown in Step 2, and remember that an active swap file affects some Btrfs operations such as snapshotting its containing subvolume.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I remove the swap file later?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm the exact path with <code>sudo swapon --show<\/code>, run <code>sudo swapoff \/swapfile<\/code>, and remove only its line from <code>\/etc\/fstab<\/code>. After confirming it is inactive, delete it with <code>sudo rm \/swapfile<\/code>; do not remove an active swap file.<\/p>\n\n\n\n<script type=\"application\/ld+json\">{\"@context\": \"https:\/\/schema.org\", \"@type\": \"FAQPage\", \"mainEntity\": [{\"@type\": \"Question\", \"name\": \"How large should swap be on Debian 13?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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\u2019s short memory spikes and leave storage for updates, logs, and application data.\"}}, {\"@type\": \"Question\", \"name\": \"Does swap increase the VPS\u2019s physical RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}, {\"@type\": \"Question\", \"name\": \"Can Debian 13 use a swap file on Btrfs?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}, {\"@type\": \"Question\", \"name\": \"How do I remove the swap file later?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Learn how to add swap space on Debian 13 with a protected 1 GiB file, Btrfs-safe setup, persistent boot configuration, verification, and fixes.","protected":false},"author":1,"featured_media":892,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","footnotes":""},"categories":[5,9],"tags":[98,93,92],"class_list":["post-891","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-debian-13","tag-linux-vps","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/comments?post=891"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/891\/revisions"}],"predecessor-version":[{"id":3671,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/891\/revisions\/3671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/892"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}