Adding Swap Space on Rocky Linux 9: A Step-by-Step Guide

Introduction:

Swap space is a crucial component of a Linux system that provides virtual memory and helps prevent memory exhaustion. If you’re running Rocky Linux 9 and find that your system is experiencing memory-related issues, adding swap space can help alleviate the problem. In this article, we will guide you through the process of adding swap space on Rocky Linux 9.

Step 1: Check Available Disk Space:

Before creating a swap file, it’s important to check the available disk space on your system. Open a terminal on your Rocky Linux 9 server and run the following command:

df -h

This command will display the available disk space on your system, including mounted partitions.

Step 2: Create a Swap File:

Once you have identified a suitable location for your swap file, create the file using the fallocate command. For example, to create a 2GB swap file named swapfile, run the following command:

sudo fallocate -l 2G /swapfile

Step 3: Set Appropriate Permissions:

To secure the swap file, restrict its access permissions. Run the following command to set the permissions:

sudo chmod 600 /swapfile

Step 4: Set Up the Swap Space:

Next, you need to format the swap file using the mkswap command. Run the following command:

sudo mkswap /swapfile

Step 5: Enable the Swap Space:

To enable the newly created swap space, use the swapon command. Run the following command:

sudo swapon /swapfile

To make the swap space persistent across reboots, add an entry for the swap file in the /etc/fstab file. Open the file in a text editor:

sudo nano /etc/fstab

Add the following line at the end of the file:

/swapfile swap swap defaults 0 0

Save and close the file.

Step 6: Adjust Swappiness (Optional):

Swappiness determines the tendency of the system to use swap space. By default, the value is set to 60. If you want to adjust the swappiness, open the sysctl configuration file:

sudo nano /etc/sysctl.conf

Add or modify the following line to set the desired swappiness value (e.g., 10 for lower swappiness):

vm.swappiness=10

Save and close the file.

Step 7: Verify the Swap Space:

To verify that the swap space is properly set up and active, you can use the free command. Run the following command:

free -h

This command will display the system’s memory usage, including the newly added swap space.

Conclusion:

Congratulations! You have successfully added swap space on your Rocky Linux 9 system. The additional swap space will help improve your system’s performance by providing virtual memory when the physical memory is under heavy usage. Remember to monitor your system’s memory usage and adjust the swap space size if necessary. Adding swap space is a valuable technique to optimize your system’s memory management and ensure smooth operation.


Leave a Reply

Your email address will not be published. Required fields are marked *