{"id":885,"date":"2026-10-20T18:00:00","date_gmt":"2026-10-20T18:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=885"},"modified":"2026-10-20T18:00:00","modified_gmt":"2026-10-20T18:00:00","slug":"add-swap-space-almalinux-9","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/20\/add-swap-space-almalinux-9\/","title":{"rendered":"How to Add Swap Space on AlmaLinux 9 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on AlmaLinux 9 by creating a protected 2 GiB swap file and enabling it immediately and after reboots. The setup takes about five to ten minutes and gives a small VPS extra disk-backed headroom 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>An AlmaLinux 9 server with at least 2 GiB of free space on the filesystem that contains <code>\/swapfile<\/code>.<\/li>\n\n\n<li>SSH access as <code>root<\/code>, or a regular account with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>The path <code>\/swapfile<\/code> must not be an existing application file or active swap area.<\/li>\n\n\n<li>A <a href=\"https:\/\/vpslake.com\/buy-vps\">Linux VPS from VPSLake<\/a> if you need an AlmaLinux server for this procedure.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Swap is slower than RAM, so it is a safety buffer rather than a replacement for more memory. This guide uses a 2 GiB file; use a smaller size when the disk has limited free space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Check AlmaLinux 9, free disk, and existing swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Inspect the server first so you do not create a second swap file accidentally or fill the root filesystem.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/almalinux-release\nfree -h\nswapon --show\ndf -h \/\nsudo test -e \/swapfile &amp;&amp; ls -lh \/swapfile || echo '\/swapfile is available'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On a clean VPS, the output should identify AlmaLinux 9, show <code>Swap: 0B<\/code>, return no rows from <code>swapon --show<\/code>, and report enough available space for the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AlmaLinux release 9.x (Emerald Puma)\nSwap:              0B          0B          0B\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  7.0G   33G  18% \/\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>swapon --show<\/code> already lists swap, keep it unless you have a specific reason to add more. If <code>\/swapfile<\/code> exists, choose another path such as <code>\/swapfile-2g<\/code> instead of overwriting it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Allocate and secure the swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Allocate the file and restrict it to root before creating the swap signature, which prevents other local users from reading memory pages written to disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fallocate -l 2G \/swapfile\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mkswap<\/code> should report a new swapspace UUID and a size close to 2 GiB:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Setting up swapspace version 1, size = 2 GiB\nno label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">AlmaLinux 9 commonly uses XFS, where <code>fallocate<\/code> is the efficient choice. If <code>swapon<\/code> later rejects the file with <code>Invalid argument<\/code>, first confirm that it is not active, remove only this newly created file, and recreate it as a fully written 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=2048 status=progress\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>dd<\/code> fallback writes 2,048 MiB and can take longer because it touches every block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Enable swap now and at boot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put the file in <code>\/etc\/fstab<\/code> so AlmaLinux activates it during startup, then turn it on for the current session without rebooting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check for an existing entry before appending one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo grep -nE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab || true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If that command returns no line, add the entry exactly once:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf '%s\\n' '\/swapfile none swap defaults 0 0' | sudo tee -a \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reload systemd\u2019s generated configuration and activate the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo swapon \/swapfile\nsudo swapon --show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The active table should include the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    2G   0B   -2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Priority can differ when another swap device is already configured. The important checks are that <code>\/swapfile<\/code> appears and its <code>SIZE<\/code> is close to the value you selected. Red Hat documents the same file-based sequence for RHEL 9 in its <a href=\"https:\/\/docs.redhat.com\/en\/documentation\/red_hat_enterprise_linux\/9\/html\/managing_storage_devices\/getting-started-with-swap_managing-storage-devices\" target=\"_blank\" rel=\"noopener\">official swap documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Tune swappiness only when needed<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> setting influences how readily the kernel moves inactive pages to swap; AlmaLinux\u2019s default is usually a good starting point, so change it only for a known workload.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See the current value:<\/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 the VPS should prefer RAM and use swap mainly during pressure, test a value of 10:<\/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 suits the workload, save it in a dedicated file rather than changing a vendor-managed configuration:<\/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 just to avoid disk activity. That can leave the kernel with less flexibility during a sudden memory shortage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Verify the active kernel table, reported capacity, file permissions, and boot entry together:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\ncat \/proc\/swaps\nfree -h\nstat -c '%A %U %n' \/swapfile\nsudo grep -E '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Successful output contains an active <code>\/swapfile<\/code>, a <code>Swap<\/code> total near 2 GiB, mode <code>-rw-------<\/code>, and the following configuration line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile file    2G   0B   -2\n-rw------- 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\">Linux 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 an AlmaLinux 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, partially allocated, or on a filesystem layout that does not support it as a swap file. Run <code>sudo swapon --show<\/code> first; if the file is not active, remove it and use the <code>dd<\/code> fallback in Step 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>fallocate: fallocate failed: No space left on device<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The filesystem holding <code>\/swapfile<\/code> cannot provide 2 GiB. Check <code>df -h \/<\/code>, preserve free space for updates and logs, and recreate the file at a smaller size such as 1 GiB with <code>sudo fallocate -l 1G \/swapfile<\/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. Confirm with <code>sudo swapon --show<\/code> and <code>ls -l \/swapfile<\/code>; if it is already listed, keep the active file and remove any duplicate <code>\/etc\/fstab<\/code> line instead of recreating 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 environments prevent guests from managing swap. This is a virtualization policy, not a missing AlmaLinux package; ask the provider to enable host-level swap or move the workload to a plan that permits 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 AlmaLinux 9?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There is no universal size. Two GiB is a practical starting point for a small VPS, but choose based on available disk, peak memory use, and the amount of space required by the operating system and applications.<\/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 keep a short memory spike from immediately terminating a process, while frequent swap activity indicates that the workload needs more memory or tighter limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need to reboot after creating the swap file?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>swapon \/swapfile<\/code> enables it immediately, and the <code>\/etc\/fstab<\/code> entry handles future boots. <code>systemctl daemon-reload<\/code> refreshes systemd\u2019s generated configuration after the file is edited.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can I remove the swap file later?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm the 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 it is no longer active, delete the file with <code>sudo rm \/swapfile<\/code>.<\/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 AlmaLinux 9?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"There is no universal size. Two GiB is a practical starting point for a small VPS, but choose based on available disk, peak memory use, and the amount of space required by the operating system and applications.\"}}, {\"@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 keep a short memory spike from immediately terminating a process, while frequent swap activity indicates that the workload needs more memory or tighter limits.\"}}, {\"@type\": \"Question\", \"name\": \"Do I need to reboot after creating the swap file?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. `swapon \/swapfile` enables it immediately, and the `\/etc\/fstab` entry handles future boots. `systemctl daemon-reload` refreshes systemd\u2019s generated configuration after the file is edited.\"}}, {\"@type\": \"Question\", \"name\": \"How can I remove the swap file later?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Confirm the path with `sudo swapon --show`, run `sudo swapoff \/swapfile`, and remove only its line from `\/etc\/fstab`. After checking that it is no longer active, delete the file with `sudo rm \/swapfile`.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Add swap space on AlmaLinux 9 with a secure 2 GB file, persistent boot setup, optional tuning, verification, and troubleshooting for VPS users.","protected":false},"author":1,"featured_media":886,"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":[95,93,92],"class_list":["post-885","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-almalinux-9","tag-linux-vps","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/885","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=885"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/885\/revisions"}],"predecessor-version":[{"id":3661,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/885\/revisions\/3661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/886"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}