{"id":883,"date":"2026-10-20T13:00:00","date_gmt":"2026-10-20T13:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=883"},"modified":"2026-10-20T13:00:00","modified_gmt":"2026-10-20T13:00:00","slug":"add-swap-space-almalinux-8","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/20\/add-swap-space-almalinux-8\/","title":{"rendered":"How to Add Swap Space on AlmaLinux 8"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on AlmaLinux 8 by creating a protected 2 GB swap file and enabling it immediately and at boot. You can complete the setup in about five to ten minutes, giving a small VPS extra disk-backed memory for short periods of RAM pressure.<\/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 8 server, preferably updated to the latest AlmaLinux 8 minor release, with at least 2 GB of free space on <code>\/<\/code> for this example.<\/li>\n\n\n<li>SSH access as <code>root<\/code>, or a normal account with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>The path <code>\/swapfile<\/code> must not already be an active swap file or a regular file you need to keep.<\/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 an emergency buffer rather than a substitute for adding memory. The commands below use a 2 GB file; reduce that size if your disk is small.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect memory, disk, and current swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the server first so you do not create duplicate swap or consume the last free space on the root filesystem.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/almalinux-release\nfree -h\nswapon --show\ndf -h \/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On a server with no active swap, <code>swapon --show<\/code> returns no rows. The other checks should look similar to this abbreviated example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AlmaLinux release 8.10 (Cerulean Leopard)\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       410Mi       1.0Gi       12Mi       590Mi       1.4Gi\nSwap:              0B          0B          0B\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  6.5G   34G  17% \/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If any swap device is already listed, you may not need another one. If <code>\/swapfile<\/code> already exists, inspect it before continuing; do not overwrite a file that belongs to an application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create and protect the swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a fully allocated file, then restrict it before Linux accepts it as swap; this avoids sparse-file problems and prevents ordinary users from reading data written to disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dd if=\/dev\/zero of=\/swapfile bs=1M count=2048 status=progress\nsudo chmod 0600 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The copy operation should finish with a summary showing 2,147,483,648 bytes copied. Set up the file as swap space:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The command creates a new swap signature and reports its size and UUID:<\/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\">If <code>dd<\/code> stops with a space error, remove the incomplete file only after confirming that it is not active, then choose a smaller size based on the available space shown in Step 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Activate swap for the current session<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mkswap<\/code> prepares the file but does not use it; <code>swapon<\/code> hands it to the kernel without requiring 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 active table should include the new 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\">The priority may differ if the server already has another swap area. That is normal as long as <code>\/swapfile<\/code> appears as active.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Make the configuration survive reboot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one <code>\/etc\/fstab<\/code> entry so AlmaLinux activates the file during every boot, then ask systemd to reload the generated mount configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add this line on its own line, save the file, and exit the editor:<\/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\">Reload systemd and validate the entry without restarting the server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo swapon --show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Do not add the line twice. A duplicate entry can cause confusing boot messages even though the first entry is valid.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Tune swappiness only if needed<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> value influences how readily the kernel moves inactive memory to swap; the AlmaLinux default is a sensible starting point for most workloads.<\/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 this VPS should prefer RAM and use the file mainly during sustained pressure, try 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 is appropriate, persist it in a dedicated file instead of editing the main sysctl configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/sysctl.d\/99-swap.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add:<\/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\">Apply the file and check the effective value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sysctl --system\nsysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Expected output ends with:<\/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 I\/O; doing so can leave less room for the kernel to handle 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\">Confirm the kernel sees the file, the system reports its capacity, and the boot entry exists.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\ncat \/proc\/swaps\nfree -h\nsudo grep -E '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Successful output contains a 2 GB <code>\/swapfile<\/code>, a matching <code>\/proc\/swaps<\/code> row, and this configuration line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile file    2097148      0        -2\nSwap:          2.0Gi          0B     2.0Gi\n\/swapfile none swap defaults 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For the AlmaLinux 8-compatible storage procedure, see <a href=\"https:\/\/docs.redhat.com\/en\/documentation\/red_hat_enterprise_linux\/8\/html\/managing_storage_devices\/getting-started-with-swap_managing-storage-devices\" target=\"_blank\" rel=\"noopener\">Red Hat\u2019s RHEL 8 swap documentation<\/a>.<\/p>\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 that rejects its current layout. Check that it is not active with <code>swapon --show<\/code>, remove the inactive file, and recreate it with the <code>dd<\/code> command in Step 2 rather than <code>fallocate<\/code> or <code>truncate<\/code>.<\/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 hold a 2 GB file. Check <code>df -h \/<\/code>, remove only data you recognize as unnecessary, or use a smaller file such as <code>1G<\/code> with <code>bs=1M count=1024<\/code>; run <code>mkswap<\/code> again after recreating it.<\/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 the file open. Run <code>swapon --show<\/code> and <code>ls -l \/swapfile<\/code>; if it is already listed, keep it and remove any duplicate <code>\/etc\/fstab<\/code> line instead of creating a second file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: ... Operation not permitted<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The VPS virtualization layer may forbid guest-managed swap. This is a host policy, not a missing AlmaLinux package; ask the provider to enable host 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 much swap should a small AlmaLinux 8 VPS have?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There is no universal ratio. A 2 GB file is a practical starting point for a small server, but choose based on available disk, workload peaks, and whether an application has its own memory requirements. Keep enough free space for updates and logs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Will swap increase the server\u2019s available RAM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It adds slower disk-backed virtual memory and can prevent a short memory spike from immediately killing a process. Continuous swap activity usually means the workload needs more RAM or tighter memory limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use a swap partition instead of a file?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A dedicated partition can be formatted with <code>mkswap<\/code> and added to <code>\/etc\/fstab<\/code> by UUID, but a file is easier to resize on a VPS and does not require repartitioning the disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can I remove this swap file later?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First confirm the path with <code>swapon --show<\/code>, then run <code>sudo swapoff \/swapfile<\/code>, remove only its line from <code>\/etc\/fstab<\/code>, reload systemd, and delete the file with <code>sudo rm \/swapfile<\/code>. Do not remove it while it is active.<\/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 small AlmaLinux 8 VPS have?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"There is no universal ratio. A 2 GB file is a practical starting point for a small server, but choose based on available disk, workload peaks, and whether an application has its own memory requirements. Keep enough free space for updates and logs.\"}}, {\"@type\": \"Question\", \"name\": \"Will swap increase the server\u2019s available RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. It adds slower disk-backed virtual memory and can prevent a short memory spike from immediately killing a process. Continuous swap activity usually means the workload needs more RAM or tighter memory limits.\"}}, {\"@type\": \"Question\", \"name\": \"Can I use a swap partition instead of a file?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes. A dedicated partition can be formatted with `mkswap` and added to `\/etc\/fstab` by UUID, but a file is easier to resize on a VPS and does not require repartitioning the disk.\"}}, {\"@type\": \"Question\", \"name\": \"How can I remove this swap file later?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"First confirm the path with `swapon --show`, then run `sudo swapoff \/swapfile`, remove only its line from `\/etc\/fstab`, reload systemd, and delete the file with `sudo rm \/swapfile`. Do not remove it while it is active.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Add swap space on AlmaLinux 8 with a protected 2 GB swap file, persistent boot setup, safe tuning, verification, and practical troubleshooting steps.","protected":false},"author":1,"featured_media":884,"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":[94,93,92],"class_list":["post-883","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-almalinux-8","tag-linux-vps","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/883","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=883"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/883\/revisions"}],"predecessor-version":[{"id":3656,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/883\/revisions\/3656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/884"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}