{"id":889,"date":"2026-10-21T09:00:00","date_gmt":"2026-10-21T09:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=889"},"modified":"2026-10-21T09:00:00","modified_gmt":"2026-10-21T09:00:00","slug":"add-swap-space-debian-12","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/21\/add-swap-space-debian-12\/","title":{"rendered":"How to Add Swap Space on Debian 12 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Debian 12 (Bookworm) on a KVM VPS or dedicated server, with at least 1.2 GiB free on the filesystem mounted at <code>\/<\/code> for this example.<\/li>\n\n\n<li>SSH access as <code>root<\/code>, or a regular user with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>The <code>\/swapfile<\/code> path must be unused; do not overwrite an existing file or 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 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect Debian 12 and the current storage state<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the release, root filesystem, free space, and active 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>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 12 VPS without active swap, the version begins with <code>12<\/code>, <code>swapon --show<\/code> has no data rows, and the last command confirms that the target path is available:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>12.12\next4\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       410Mi       1.1Gi        12Mi       480Mi       1.4Gi\nSwap:              0B          0B          0B\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  6.5G   34G  17% \/\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Debian 12 minor release and device names can differ. If <code>swapon --show<\/code> already lists a partition or file, keep it unless you have a specific reason to add more; if <code>\/swapfile<\/code> exists, choose another unused filename instead of replacing it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create and protect a 1 GiB swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the usual ext4 VPS filesystem, run:<\/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\"><code>dd<\/code> should finish after writing 1,073,741,824 bytes. <code>mkswap<\/code> creates the metadata that identifies the file as swap; its UUID is informational because the persistent entry below refers to <code>\/swapfile<\/code> by path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Setting 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 12\u2019s Btrfs-aware helper instead of the <code>dd<\/code> block above. It creates a suitable non-copy-on-write swap file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo btrfs filesystem mkswapfile --size 1G \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The command requires the <code>btrfs-progs<\/code> package. Install it only on a Btrfs root filesystem if the command is missing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install -y btrfs-progs\nsudo btrfs filesystem mkswapfile --size 1G \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For the underlying commands and file format, see Debian\u2019s <a href=\"https:\/\/manpages.debian.org\/bookworm\/util-linux\/mkswap.8.en.html\" target=\"_blank\" rel=\"noopener\"><code>mkswap<\/code> documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Turn swap on for this boot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creating the signature does not activate the file; <code>swapon<\/code> registers it with the running kernel without requiring a restart.<\/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\">You should now see 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 be different if another swap device is already enabled. The important checks are that <code>\/swapfile<\/code> is listed and that its size is close to 1 GiB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Persist the setting across reboots<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one <code>fstab<\/code> record so Debian enables 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 final command should show exactly one active configuration line:<\/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\">The file is already active for the current session from Step 3. The <code>fstab<\/code> line is what tells Debian to enable it again after a reboot; <code>systemctl daemon-reload<\/code> refreshes systemd\u2019s generated view of the changed configuration.<\/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> 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.<\/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 VPS should prefer RAM and use swap mainly during pressure, test 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 that setting suits the workload, save it in a separate sysctl file and apply it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf '%s\\n' 'vm.swappiness = 10' | sudo tee \/etc\/sysctl.d\/60-swap.conf\nsudo sysctl -p \/etc\/sysctl.d\/60-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 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent Debian 12 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\ncat \/proc\/swaps\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 GiB <code>\/swapfile<\/code>, a nonzero <code>Swap<\/code> total, 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 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 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 may be sparse, partly allocated, or on Btrfs with copy-on-write still enabled. Run <code>findmnt -no FSTYPE \/<\/code>; for Btrfs, remove only an inactive failed file and use the <code>btrfs filesystem mkswapfile<\/code> command in Step 2. For ext4 or another normal filesystem, recreate the inactive file with the <code>dd<\/code> sequence, then run <code>mkswap<\/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. Confirm the available space with <code>df -h \/<\/code>, preserve room for Debian updates and logs, and use a smaller inactive file such as 512 MiB:<\/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 0600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only run <code>rm<\/code> after <code>sudo swapon --show<\/code> confirms that the failed file is not active. Keep the same <code>\/etc\/fstab<\/code> path; it does not encode the file size.<\/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 the file is listed as active, keep it and remove any duplicate <code>fstab<\/code> line instead of 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 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.<\/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 12 VPS have?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does swap increase physical RAM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need to reboot after adding swap on Debian 12?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>swapon \/swapfile<\/code> activates it immediately, and the <code>\/etc\/fstab<\/code> entry handles later boots. A restart is useful only if you want to test the boot configuration during a maintenance window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I remove a swap file safely?<\/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 checking that the file is inactive, delete it with <code>sudo rm \/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 12 VPS have?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}, {\"@type\": \"Question\", \"name\": \"Does swap increase physical RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}, {\"@type\": \"Question\", \"name\": \"Do I need to reboot after adding swap on Debian 12?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"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.\"}}, {\"@type\": \"Question\", \"name\": \"How do I remove a swap file safely?\", \"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 checking that the file is inactive, delete it with `sudo rm \/swapfile`; never remove an active swap file first.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"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.","protected":false},"author":1,"featured_media":890,"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":[97,93,92],"class_list":["post-889","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-debian-12","tag-linux-vps","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/889","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=889"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/889\/revisions"}],"predecessor-version":[{"id":3670,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/889\/revisions\/3670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/890"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}