RHEL BasedRocky Linux

How To Add Swap Space on Rocky Linux 9

Add Swap Space on Rocky Linux 9

In this tutorial, we will show you how to add swap space on Rocky Linux 9. Before we dive into the process of adding swap space, it’s crucial to understand what it is and why it’s important. Swap space in Linux is a designated area on your hard drive that comes into play when your system’s RAM is full. If the system requires more memory resources and the RAM is unable to accommodate, inactive pages in memory are moved to the swap space. This process is known as ‘swapping’ and it allows your system to handle more processes than it could with RAM alone.

However, it’s important to note that while swap space can be a lifesaver for systems with limited RAM, it’s not a substitute for more RAM. Swap space resides on hard drives, which have significantly slower access times than RAM. Over-reliance on swap space can lead to decreased system performance, a state known as ‘thrashing’. Therefore, while managing swap space is important, it’s equally crucial to ensure your system has adequate RAM for its tasks.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step add swap space on Rocky Linux 9 or RHEL-based.


  • A server running one of the following operating systems: Rocky Linux 9.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Add Swap Space on Rocky Linux 9

Step 1. Check Available Disk Space.

Before allocating a partition to swap, ensure that any existing data on the partition is backed up, as the process will erase all data on the partition. You can check the available disk space on your system, including mounted partitions, using the df command.

Step 2. Create a Swap File.

To add a swap file, you first need to determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536. You can create an empty file using the dd command as follows:

dd if=/dev/zero of=/swapfile bs=1024 count=65536

Replace count with the value equal to the desired block size.

Step 3. Set Up the Swap File.

Next, you need to set up the swap file with the mkswap command:

mkswap /swapfile

Step 4. Secure the Swap File.

For security reasons, you need to lock down the permissions of the file so that only users with root privileges can read it. This prevents normal users from being able to access the file, which would have significant security implications. You can make the file only accessible to root by typing:

sudo chmod 600 /swapfile

Verify the permissions change to ensure the file is secure.

Step 5. Enable the Swap File.

After creating the swap area, you need the swapon command to start using it. Usually, swap areas are listed in /etc/fstab so that they can be taken into use at boot time:

swapon /swapfile

This command will enable the swap file.

Step 6. Make the Swap File Permanent.

To make the swap file permanent, i.e., available after a reboot, add an entry to /etc/fstab for the swap file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This command will add the swap file to the system file table, making it available after a reboot.

Congratulations! You have successfully added swap space. Thanks for using this tutorial to add swap space on your Rocky Linux 9 system. For additional help or useful information, we recommend you check the official Rocky Linux website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!


r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button