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.
non-root sudo useror 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
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
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
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:
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.