{"id":887,"date":"2026-10-21T03:00:00","date_gmt":"2026-10-21T03:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=887"},"modified":"2026-10-21T03:00:00","modified_gmt":"2026-10-21T03:00:00","slug":"add-swap-space-debian-11","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/21\/add-swap-space-debian-11\/","title":{"rendered":"How to Add Swap Space on Debian 11 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/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 11 (Bullseye) VPS with at least 1.2 GB of free space on the filesystem mounted at <code>\/<\/code>; keep additional room for updates, logs, and applications.<\/li>\n\n\n<li>An SSH session as <code>root<\/code>, or a regular account that can run commands with <code>sudo<\/code>.<\/li>\n\n\n<li>A filesystem that supports ordinary swap files, such as ext4. Run the filesystem check in Step 1 before creating <code>\/swapfile<\/code>.<\/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\">If you are already logged in as <code>root<\/code>, remove <code>sudo<\/code> from the examples. A regular account should keep it so the file and system configuration are changed with administrator privileges.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Check Debian, storage, and current swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inspect the server first so you do not overwrite an existing file, fill the root filesystem, or create duplicate swap.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/debian_version\nfindmnt -no FSTYPE \/\nfree -h\nswapon --show\ndf -h \/\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 11 VPS without active swap, the version begins with <code>11<\/code>, <code>swapon --show<\/code> prints no swap rows, and the last line confirms that the target path is free:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>11.11\next4\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       410Mi       1.1Gi        12Mi       480Mi       1.4Gi\nSwap:              0B          0B          0B\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>swapon --show<\/code> already lists a swap partition or file, you usually do not need another one. If <code>\/swapfile<\/code> exists, do not run the creation command in the next step; choose another unused path or manage the existing file instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create a root-only 1 GB swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run these commands only after confirming that <code>\/swapfile<\/code> is available:<\/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 600 \/swapfile\nsudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>dd<\/code> command creates a fully allocated file instead of relying on sparse-file behavior. <code>mkswap<\/code> 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 <code>mkswap<\/code> and <code>swapon<\/code> tools in its <a href=\"https:\/\/manpages.debian.org\/bullseye\/util-linux\/mkswap.8.en.html\" target=\"_blank\" rel=\"noopener\">Bullseye mkswap manual<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1024+0 records in\n1024+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<h2 class=\"wp-block-heading\">Step 3: Enable swap for the current session<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize-and-enable are separate operations: <code>mkswap<\/code> writes the swap signature, while <code>swapon<\/code> tells the running kernel to use the file.<\/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 new file should appear immediately:<\/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> is listed with a size close to the one you created. If <code>swapon<\/code> returns <code>Operation not permitted<\/code>, see the troubleshooting section rather than repeatedly retrying the command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Make the swap file persistent<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one <code>\/etc\/fstab<\/code> entry so Debian can activate the file during boot, while checking first to avoid 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 sw 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 expected configuration line is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile none swap sw 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You do not need to reboot. The <code>swapon<\/code> command in Step 3 already enabled the file for this boot; the <code>fstab<\/code> line is what makes the setting survive future restarts. Debian\u2019s <a href=\"https:\/\/wiki.debian.org\/Swap\" target=\"_blank\" rel=\"noopener\">Swap wiki page<\/a> explains the file creation and persistent configuration model.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Review swappiness before changing it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>vm.swappiness<\/code> 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.<\/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 you want the kernel to prefer RAM and use swap mainly when memory pressure rises, 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 this suits the server, save it in a dedicated file and apply it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo 'vm.swappiness = 10' | sudo tee \/etc\/sysctl.d\/99-swap.conf\nsudo sysctl -p \/etc\/sysctl.d\/99-swap.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent Debian 11 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/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 contains a 1 GB <code>\/swapfile<\/code>, a nonzero <code>Swap<\/code> total, root-only mode, 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 sw 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 disk space for a protected swap file and your applications.<\/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 may be sparse, partly allocated, or on a filesystem with swap-file restrictions. Check <code>findmnt -no FSTYPE \/<\/code>; if it reports <code>btrfs<\/code>, 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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo rm \/swapfile\nsudo truncate -s 0 \/swapfile\nsudo chattr +C \/swapfile\nsudo fallocate -l 1G \/swapfile\nsudo chmod 600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On ext4, recreate the inactive file with the <code>dd<\/code> 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.<\/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 full 1 GB. Confirm the available space with <code>df -h \/<\/code>, preserve room for Debian updates and logs, and recreate the file at 512 MB only after removing the inactive failed file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo rm \/swapfile\nsudo dd if=\/dev\/zero of=\/swapfile bs=1M count=512 status=progress\nsudo chown root:root \/swapfile\nsudo chmod 600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/code><\/pre>\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 is being used by another process. Run <code>sudo swapon --show<\/code> and inspect <code>ls -lh \/swapfile<\/code>; if it is already listed, keep it and remove any duplicate <code>fstab<\/code> line instead of overwriting the file.<\/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-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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How much swap should a Debian 11 VPS have?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A 1 GB file is a reasonable starting point for a small VPS when the disk has room. Size it around the workload\u2019s short-term memory spikes, not as a fixed multiple of RAM, and leave enough storage for the OS, logs, and application data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does swap add physical RAM to Debian?<\/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 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I add swap without rebooting Debian 11?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. <code>swapon \/swapfile<\/code> activates it immediately. The <code>\/etc\/fstab<\/code> entry handles later boots, so a restart is not part of the setup.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I remove the swap file safely?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm the exact path with <code>sudo swapon --show<\/code>, then run <code>sudo swapoff \/swapfile<\/code> and remove only its line from <code>\/etc\/fstab<\/code>. After confirming it is inactive, delete <code>\/swapfile<\/code>; never remove an active swap file first.<\/p>\n\n\n\n<script type=\"application\/ld+json\">{\"@context\": \"https:\/\/schema.org\", \"@type\": \"FAQPage\", \"mainEntity\": [{\"@type\": \"Question\", \"name\": \"How much swap should a Debian 11 VPS have?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"A 1 GB file is a reasonable starting point for a small VPS when the disk has room. Size it around the workload\u2019s short-term memory spikes, not as a fixed multiple of RAM, and leave enough storage for the OS, logs, and application data.\"}}, {\"@type\": \"Question\", \"name\": \"Does swap add physical RAM to Debian?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}, {\"@type\": \"Question\", \"name\": \"Can I add swap without rebooting Debian 11?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes. `swapon \/swapfile` activates it immediately. The `\/etc\/fstab` entry handles later boots, so a restart is not part of the setup.\"}}, {\"@type\": \"Question\", \"name\": \"How do I remove the swap file safely?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"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.","protected":false},"author":1,"featured_media":888,"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":[96,93,92],"class_list":["post-887","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-debian-11","tag-linux-vps","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/887","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=887"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":3666,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/887\/revisions\/3666"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/888"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}