{"id":905,"date":"2026-10-23T09:00:00","date_gmt":"2026-10-23T09:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=905"},"modified":"2026-10-23T09:00:00","modified_gmt":"2026-10-23T09:00:00","slug":"add-swap-space-rocky-linux-8","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/23\/add-swap-space-rocky-linux-8\/","title":{"rendered":"How to Add Swap Space on Rocky Linux 8 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on Rocky Linux 8 by creating a protected 2 GiB file and enabling it for the current session and future boots. You can finish the setup in about five to ten minutes, giving a small VPS a disk-backed buffer for 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>Rocky Linux 8, preferably updated to the latest available 8.x minor release, with at least 2.5 GiB free on the filesystem mounted at <code>\/<\/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>Permission to create <code>\/swapfile<\/code>; that path must not contain an application 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 Rocky Linux server for this procedure.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Swap is much slower than RAM. The 2 GiB example below can absorb a temporary shortage, but frequent swap activity 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 the server before creating swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the Rocky Linux release, filesystem capacity, and existing swap first so you do not overwrite a useful file or fill the root volume.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/rocky-release\nfindmnt -no FSTYPE \/\ndf -h \/\nfree -h\nswapon --show\nif [ -e \/swapfile ]; then ls -lh \/swapfile; else echo \"\/swapfile is unused\"; fi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On a new server, the final commands normally show no active swap rows and an unused target path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Rocky Linux release 8.x (Green Obsidian)\nxfs\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  7.0G   33G  18% \/\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       430Mi       1.0Gi        12Mi       570Mi       1.4Gi\nSwap:              0B          0B          0B\n\/swapfile is unused<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The device name, memory totals, and filesystem type can differ. If <code>swapon --show<\/code> already lists a swap partition or file, keep it unless you have a specific reason to add another. If <code>\/swapfile<\/code> exists, stop and choose another unused path such as <code>\/swapfile-2g<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create and secure a 2 GiB swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write the file fully, set root-only permissions, and add a swap signature; this avoids sparse-file surprises and keeps users without administrative access from reading pages written to disk.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>dd<\/code> method is deliberately compatible with common Rocky Linux 8 XFS and ext4 layouts:<\/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 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> writes 2,048 MiB, so it may take a little longer than an allocation-only command. <code>mkswap<\/code> should report a new 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\">For the compatible Red Hat Enterprise Linux 8 procedure behind Rocky Linux 8, see the official <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\">RHEL 8 swap documentation<\/a>. Do not rerun the creation command against a path that is already active.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Turn on swap for this boot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Formatting prepares the file but does not make the kernel use it; <code>swapon<\/code> activates it immediately without 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 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    2G   0B   -2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The priority can differ if another swap device is present. The important checks are that <code>\/swapfile<\/code> appears and its capacity is close to the size you created.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Persist the file across reboots<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one <code>\/etc\/fstab<\/code> record so Rocky Linux enables the same file during startup, while the guard prevents duplicate entries if you run the command again.<\/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 &gt;\/dev\/null\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 matching line:<\/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\">Swap is already active from Step 3. The <code>\/etc\/fstab<\/code> entry is what restores it after a reboot, so you do not need to restart just to use the new space. If you had to choose a different filename in Step 1, substitute that exact path in both commands and in later checks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Review swappiness and make an optional adjustment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> value influences how readily Linux moves inactive pages from RAM to disk, so inspect the current setting before changing it for a particular workload.<\/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 server should keep active data in 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\nsysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If monitoring shows that this setting fits the workload, save it in a dedicated file and reload that file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf '%s\\n' 'vm.swappiness = 10' | sudo tee \/etc\/sysctl.d\/90-swap.conf\nsudo sysctl -p \/etc\/sysctl.d\/90-swap.conf<\/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 hide disk activity. Constant swapping usually means the VPS needs more RAM or the application needs a lower memory footprint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the Rocky Linux 8 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run these checks together to confirm that the kernel sees the file, the file is protected, and the boot configuration contains one matching entry.<\/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 includes an active 2 GiB file, a nonzero swap total, root-only permissions, and the persistent record:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    2G   0B   -2\n\/swapfile file 2097148      0   -2\nSwap:          2.0Gi          0B     2.0Gi\n-rw------- root:root \/swapfile\n\/swapfile none swap defaults 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The exact block counts and memory figures vary by kernel and server. If you want to test the boot path, schedule a maintenance window and reboot; do not deactivate the only swap area on a memory-stressed server just for a test.<\/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\">Rocky 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 a Rocky Linux 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, incomplete, or stored on a filesystem layout that rejects it as swap. Confirm it is not active with <code>sudo swapon --show<\/code>, remove the inactive file, and recreate it with the <code>dd<\/code> sequence in Step 2. Do not delete a path that appears in the active swap table.<\/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 does not have 2 GiB available. Check <code>df -h \/<\/code>, keep enough room for updates and logs, and use a smaller 1 GiB file only 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=1024 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\">The <code>\/etc\/fstab<\/code> path stays valid when the file size changes. Never run the <code>rm<\/code> command while <code>\/swapfile<\/code> is listed by <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 file 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 it and remove only duplicate <code>\/swapfile<\/code> lines from <code>\/etc\/fstab<\/code>.<\/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 prohibit guest-managed swap. This is a virtualization policy rather than a missing Rocky Linux command, so request host-managed swap or use a virtualization plan that allows swap files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Next steps<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Watch <code>free -h<\/code> and <code>vmstat 5<\/code> during the workload that prompted you to add swap; sustained swap-in and swap-out activity needs attention.<\/li>\n\n\n<li>Leave free disk space for Rocky Linux updates, logs, and application data instead of filling the volume with swap.<\/li>\n\n\n<li>If the server regularly approaches its memory limit, resize the VPS or move the workload rather than treating swap as a RAM replacement.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How much swap does a Rocky Linux 8 VPS need?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There is no universal RAM-to-swap formula. Two GiB is a practical starting point for a small VPS when the disk has room, but size it around short-lived memory peaks and preserve space for the operating system and applications.<\/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 uses slower disk storage as virtual memory and can postpone an out-of-memory failure during a brief spike. It cannot deliver the performance or capacity of additional RAM.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is a swap partition better than a swap file?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A partition can work, but a file is usually easier on a VPS because you can resize it without repartitioning the disk. Both require a swap signature, restrictive permissions where applicable, activation with <code>swapon<\/code>, and a persistent boot configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I remove or resize the swap file later?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First identify the exact path with <code>swapon --show<\/code>, then move the workload away from memory pressure and deactivate that path with <code>swapoff<\/code>. Remove only its <code>\/etc\/fstab<\/code> entry, create the replacement, and verify the new area before deleting the old file.<\/p>\n\n\n\n<script type=\"application\/ld+json\">{\"@context\": \"https:\/\/schema.org\", \"@type\": \"FAQPage\", \"mainEntity\": [{\"@type\": \"Question\", \"name\": \"How much swap does a Rocky Linux 8 VPS need?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"There is no universal RAM-to-swap formula. Two GiB is a practical starting point for a small VPS when the disk has room, but size it around short-lived memory peaks and preserve space for the operating system and applications.\"}}, {\"@type\": \"Question\", \"name\": \"Does swap increase physical RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. Swap uses slower disk storage as virtual memory and can postpone an out-of-memory failure during a brief spike. It cannot deliver the performance or capacity of additional RAM.\"}}, {\"@type\": \"Question\", \"name\": \"Is a swap partition better than a swap file?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"A partition can work, but a file is usually easier on a VPS because you can resize it without repartitioning the disk. Both require a swap signature, restrictive permissions where applicable, activation with `swapon`, and a persistent boot configuration.\"}}, {\"@type\": \"Question\", \"name\": \"Can I remove or resize the swap file later?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"First identify the exact path with `swapon --show`, then move the workload away from memory pressure and deactivate that path with `swapoff`. Remove only its `\/etc\/fstab` entry, create the replacement, and verify the new area before deleting the old file.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Add swap space on Rocky Linux 8 with a secure 2 GiB file, persistent boot setup, careful tuning, verification checks, and fixes for common errors.","protected":false},"author":1,"featured_media":906,"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":[93,105,92],"class_list":["post-905","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-linux-vps","tag-rocky-linux-8","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/905","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=905"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/905\/revisions"}],"predecessor-version":[{"id":3699,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/905\/revisions\/3699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/906"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}