{"id":895,"date":"2026-10-22T03:00:00","date_gmt":"2026-10-22T03:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=895"},"modified":"2026-10-22T03:00:00","modified_gmt":"2026-10-22T03:00:00","slug":"add-swap-space-rocky-linux-9","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/22\/add-swap-space-rocky-linux-9\/","title":{"rendered":"How to Add Swap Space on Rocky Linux 9 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on Rocky Linux 9 by creating a root-only 2 GiB swap file and enabling it for the current session and future boots. The setup takes about five to ten minutes and gives a small VPS a disk-backed buffer 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>Rocky Linux 9 on a VPS or dedicated server, with at least 2.5 GiB free on the filesystem that holds <code>\/swapfile<\/code>; the extra room leaves space for system files and logs.<\/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>At least 2 GiB of RAM is useful for a general-purpose server, but swap size should follow the workload rather than replace physical memory.<\/li>\n\n\n<li>The <code>\/swapfile<\/code> path must be unused. Do not overwrite an existing application 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 Rocky Linux server for this procedure.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Swap is slower than RAM. It can absorb a temporary shortage, but frequent swapping means the server needs more memory or the application needs tighter limits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect Rocky Linux 9 and the storage state<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the release, root filesystem, available disk, and current swap before writing a file so you do not duplicate swap or fill the system disk.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/rocky-release\nfindmnt -no FSTYPE \/\ndf -h \/\nfree -h\nswapon --show\ntest -e \/swapfile &amp;&amp; ls -lh \/swapfile || echo \"\/swapfile is available\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A new Rocky Linux 9 VPS commonly reports XFS or ext4, no active swap rows, and an available target path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Rocky Linux release 9.x (Blue Onyx)\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       420Mi       1.0Gi        12Mi       580Mi       1.4Gi\nSwap:              0B          0B          0B\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The exact Rocky minor release, device name, and memory values will differ. If <code>swapon --show<\/code> already lists a swap partition or file, keep it unless you have a specific reason to add more. If <code>\/swapfile<\/code> exists, choose another unused name such as <code>\/swapfile-2g<\/code> instead of replacing it.<\/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\">Allocate the file on the local filesystem, lock it to <code>root<\/code>, and then write the swap signature so other users cannot read memory pages that the kernel stores there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On the usual XFS or ext4 root filesystem, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fallocate -l 2G \/swapfile\nsudo chown root:root \/swapfile\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mkswap<\/code> should print 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\">Rocky Linux 9 follows the RHEL 9 storage procedure, which recommends <code>fallocate<\/code> on modern filesystems such as XFS and ext4. The <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\">RHEL 9 swap documentation<\/a> also documents the <code>mkswap<\/code>, <code>fstab<\/code>, and <code>swapon<\/code> sequence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>fallocate<\/code> is missing or the later activation check rejects the file, first confirm that <code>\/swapfile<\/code> is not active. Then recreate the inactive file with a fully written fallback:<\/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\">The <code>dd<\/code> command writes 2,048 MiB and can take longer because it touches every block. Do not run both creation sequences against the same active file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Enable swap for this boot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creating the swap signature prepares the file but does not make the running kernel use it; <code>swapon<\/code> activates it immediately without 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 contain 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 vary when another swap device is already enabled. The important results are that <code>\/swapfile<\/code> appears and its size is close to 2 GiB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Make the file persistent across reboots<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one guarded <code>\/etc\/fstab<\/code> entry so Rocky Linux activates the same file during startup, then reload systemd\u2019s generated configuration.<\/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 final command should show one active 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\">The guard prevents a duplicate if the entry is already present. <code>swapon<\/code> from Step 3 handles this boot; the <code>fstab<\/code> record is what restores swap after a restart. You do not need to reboot just to activate it now.<\/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 the kernel moves inactive pages to disk, so inspect the current value and tune 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\">To test a value that prefers keeping active data in RAM, apply 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 value is appropriate, save it in a dedicated file rather than editing a vendor configuration:<\/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 merely to eliminate disk activity. A little flexibility can help during a sudden memory shortage, while constant swap use is evidence that the workload needs attention.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent Rocky Linux 9 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check activation, capacity, permissions, and the boot entry together. This confirms that the kernel can use the file now and that Rocky Linux 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 includes an active 2 GiB file, 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    2G   0B   -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<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 space 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 a filesystem that does not support this swap-file layout. Run <code>findmnt -no FSTYPE \/<\/code> and confirm the file is not active with <code>sudo swapon --show<\/code>; on XFS or ext4, recreate the inactive file with the <code>dd<\/code> fallback in Step 2. Do not remove a path that appears in the active swap table.<\/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 cannot provide 2 GiB. Check <code>df -h \/<\/code>, preserve room for Rocky 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>fstab<\/code> path remains 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. Inspect <code>sudo swapon --show<\/code> and <code>ls -l \/swapfile<\/code>; if it is already listed, keep that file and remove any duplicate <code>\/etc\/fstab<\/code> line rather than 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 platforms do not allow a guest to manage its own swap. This is a virtualization policy, not a missing Rocky Linux package, so ask the host for managed swap or use a virtualization plan that permits guest-created swap files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Is a 2 GiB swap file enough for Rocky Linux 9?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is a practical starting point for a small VPS, not a universal formula. Size it around short memory spikes, leave room for the operating system and application data, and watch swap activity before making it larger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Rocky Linux use swap instead of RAM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The kernel uses RAM first and may move less-active pages to disk under memory pressure. Swap is far slower than RAM, so frequent reads and writes indicate that the server needs more memory or the workload needs adjustment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use a swap file on Rocky Linux 9\u2019s default XFS filesystem?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. XFS is a common Rocky Linux layout, and <code>fallocate<\/code> is appropriate for a normal local XFS swap file. The file still needs mode <code>0600<\/code>, a swap signature, an active <code>swapon<\/code> entry, and a persistent <code>\/etc\/fstab<\/code> record.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I resize the swap file later?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create or activate a second swap file first if the server is under memory pressure. Otherwise, run <code>swapoff<\/code> on the old file, remove only its <code>fstab<\/code> line, create the replacement at the new size, and verify the new active path 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\": \"Is a 2 GiB swap file enough for Rocky Linux 9?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"It is a practical starting point for a small VPS, not a universal formula. Size it around short memory spikes, leave room for the operating system and application data, and watch swap activity before making it larger.\"}}, {\"@type\": \"Question\", \"name\": \"Does Rocky Linux use swap instead of RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"The kernel uses RAM first and may move less-active pages to disk under memory pressure. Swap is far slower than RAM, so frequent reads and writes indicate that the server needs more memory or the workload needs adjustment.\"}}, {\"@type\": \"Question\", \"name\": \"Can I use a swap file on Rocky Linux 9\u2019s default XFS filesystem?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes. XFS is a common Rocky Linux layout, and `fallocate` is appropriate for a normal local XFS swap file. The file still needs mode `0600`, a swap signature, an active `swapon` entry, and a persistent `\/etc\/fstab` record.\"}}, {\"@type\": \"Question\", \"name\": \"How do I resize the swap file later?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Create or activate a second swap file first if the server is under memory pressure. Otherwise, run `swapoff` on the old file, remove only its `fstab` line, create the replacement at the new size, and verify the new active path before deleting the old file.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Add swap space on Rocky Linux 9 with a protected 2 GiB file, persistent boot setup, safe swappiness tuning, verification, and common error fixes.","protected":false},"author":1,"featured_media":896,"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,100,92],"class_list":["post-895","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-linux-vps","tag-rocky-linux-9","tag-swap","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/895","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=895"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/895\/revisions"}],"predecessor-version":[{"id":3681,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/895\/revisions\/3681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/896"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}