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

The following is a native paraphrase option for the introduction:

“Allow me to commence by providing an alternative rendition of the introduction.”

To protect against out-of-memory errors in applications, you can preventatively include additional swap space on your server. This tutorial will demonstrate the process of adding a swap file to a Rocky Linux 9 server.

What does Swap mean?

A portion of the hard drive storage is allocated as swap, which serves as temporary storage for data that cannot fit in the RAM. This allows the operating system to store more information in its working memory, but with certain limitations. The swap space on the hard drive is primarily utilized when there is not enough RAM capacity to hold the currently used application data.

Data stored on a hard disk will be noticeably slower compared to data stored in RAM, but the operating system prioritizes keeping application data in memory and relies on swap space for older data. In general, having swap space as a backup when your system’s RAM is full can serve as a useful safety measure, particularly for systems that do not have SSD storage.

First, verify the system for swap information.

Firstly, let’s see if the system already has any existing swap space. It might have multiple swap files or swap partitions, but typically one should suffice.

To determine whether the system has any configured swap, we can type and check.

  1. sudo swapon –show


If you receive no output, it indicates that your system currently lacks swap space.

You can use the free utility to confirm the absence of any active swap.

  1. free -h



total used free shared buff/cache available Mem: 1.7Gi 173Mi 1.2Gi 9.0Mi 336Mi 1.4Gi Swap: 0B 0B 0B

No swaps are currently active on the system, as indicated in the Swap row of the output.

In the second step, you will need to assess the amount of storage space that is currently available on the hard drive partition.

Prior to creating our swap file, we will verify our present disk usage to ensure sufficient space availability. This can be done by entering the following command:

  1. df -h



Filesystem Size Used Avail Use% Mounted on devtmpfs 855M 0 855M 0% /dev tmpfs 888M 0 888M 0% /dev/shm tmpfs 355M 9.4M 346M 3% /run /dev/vda1 59G 1.4G 58G 3% / /dev/vda2 994M 155M 840M 16% /boot /dev/vda15 100M 7.0M 93M 7% /boot/efi tmpfs 178M 0 178M 0% /run/user/0

The disk listed under the “Mounted on” column refers to the device in question. In this particular scenario, we have ample unused space (only 1.4G used). However, your usage may vary.

There is a wide range of opinions on the ideal size for a swap space, but it ultimately comes down to your own preferences and the needs of your applications. In general, a good starting point is to have an amount of swap space that is equal to or double the amount of RAM in your system. Another useful guideline is that if you are using swap primarily as a backup for RAM, anything over 4G is likely unnecessary.

Step 3 involves the creation of a Swap File.

Once we have determined the amount of hard drive space we have, we can proceed to generate a swap file within our file system. We will designate a file with the desired size and name it as “swapfile” in the root directory (/).

The fallocate program is the most effective method for generating a swap file as it promptly creates a file of the desired size.

In this guide, we will create a file of 2G size to match the RAM capacity of our example server. Make sure to adjust this according to your own server requirements.

  1. sudo fallocate -l 1G /swapfile


To ensure that the proper space has been reserved, we can verify it by typing:

  1. ls -lh /swapfile


  1. -rw-r–r–. 1 root root 2.0G Sep 13 17:52 /swapfile


Our document has been generated with the appropriate amount of space allocated.

Step 4 – Activating the Swap File

Once we have a file of the appropriate size at our disposal, our next step is to convert it into swap space.

Initially, it is necessary to secure the file’s permissions in such a way that solely users with root privileges possess the ability to view the contents. This ensures that regular users are unable to access the file, preventing any potential security risks.

To ensure the file can only be accessed by the root, type:

  1. sudo chmod 600 /swapfile


Confirm the change of permissions by typing:

  1. ls -lh /swapfile



-rw——- 1 root root 2.0G Sep 13 17:52 /swapfile

As you can observe, it is only the root user who possesses the read and write flags activated.

To designate the file as swap space, simply type and execute the command:

  1. sudo mkswap /swapfile



Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=585e8b33-30fa-481f-af61-37b13326545b

Once we have labeled the document, we can activate the swap file, thereby enabling our system to utilize it.

  1. sudo swapon /swapfile


Confirm that the swap is accessible by inputting:

  1. sudo swapon –show



NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2

To verify our discoveries, we can re-examine the results of the complimentary tool.

  1. free -h



total used free shared buff/cache available Mem: 1.7Gi 172Mi 1.2Gi 9.0Mi 338Mi 1.4Gi Swap: 2.0Gi 0B 2.0Gi

The swap has been successfully established and our operating system will utilize it when needed.

Step 5 – Ensuring the Swap File Remains Permanent

Our recent modifications have made it possible for the swap file to be used in the current session. Nevertheless, if the server is restarted, the swap settings will not be automatically saved. To rectify this, we can update our /etc/fstab file by including the swap file.

Make a backup of the /etc/fstab file as a precautionary measure if any issues arise.

  1. sudo cp /etc/fstab /etc/fstab.bak


Type the swap file information and append it at the end of the /etc/fstab file.

  1. echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab


Afterwards, we will examine certain configurations that we can modify in order to optimize our swap space.

Step 6 – Adjusting your Swap Settings

When working with swap, there are several configurable options that can affect the performance of your system.

Changing the Swappiness Parameter.

The swappiness parameter determines the frequency at which your system moves data from RAM to the swap space. It is a percentage value ranging from 0 to 100.

By keeping values near zero, the kernel will only resort to swapping data to the disk if it is absolutely essential. It should be kept in mind that accessing the swap file is time-consuming compared to accessing RAM, which can adversely affect system performance. Therefore, instructing the system to minimize reliance on the swap file usually results in faster overall performance.

In certain scenarios, it may be advantageous for values closer to 100 to allocate more data to swap in order to maintain a larger amount of free RAM space. This can vary depending on your server’s purpose or the memory requirements of your applications.

To find out the current swappiness value, we can simply type:

  1. cat /proc/sys/vm/swappiness




A swappiness setting of 60 is reasonable for a Desktop, but if it’s a server, it’s recommended to decrease it towards 0.

Using the sysctl command allows us to adjust the swappiness to a different value.

For example, if we t to change the swappiness to 10, we can enter the following command:

  1. sudo sysctl vm.swappiness=10



vm.swappiness = 10

The changes made will remain in effect until the next time the system is restarted. To automatically apply this value upon restart, we can add the line to the /etc/sysctl.conf file.

The pre-installed text editor in Rocky Linux 9 is vi. While vi is a very powerful text editor, it can be challenging for inexperienced users. To make editing configuration files on your Rocky Linux 9 server easier, it may be advisable to install a more user-friendly editor like nano.

  1. sudo dnf install nano


You have the ability to edit the sysctl.conf file using nano.

  1. sudo nano /etc/sysctl.conf


You have the option to add at the bottom.

The configuration file for sysctl is located at /etc/sysctl.conf.

Once you are done, save and exit the file. In case you are using nano, press CTRL + X to save and quit, then respond with Y and press Enter when prompted.

Modifying the Cache Pressure Setting

One additional value that you may consider adjusting is the vfs_cache_pressure. This option controls the priority given to caching inode and dentry details compared to other data within the system.

Obtaining information about the filesystem can be expensive and is often requested, making it highly beneficial for your system to store this data in its cache. To view the current value, you can query the proc filesystem once more.

  1. cat /proc/sys/vm/vfs_cache_pressure




At its present configuration, our system swiftly eliminates inode information from the cache. To rectify this, we can adjust the setting to a more cautious value such as 50 by entering the command.

  1. sudo sysctl vm.vfs_cache_pressure=50



vm.vfs_cache_pressure = 50

This is only applicable for our current session. We can modify it by including it in our configuration file, similar to what we did with our swappiness setting.

  1. sudo nano /etc/sysctl.conf


Include the line at the end, indicating the new value.

Please provide a native language for paraphrasing.

Please save and close the file once you’re done.

In summary, to conclude

By following the instructions provided in this guide, you will create a buffer zone for situations that would typically result in out-of-memory errors. Utilizing swap space can greatly assist in preventing these prevalent issues.

If you encounter out of memory errors or find that your system cannot run the necessary applications, the most effective remedy is to enhance your application settings or upgrade your server.



More tutorials

The top seven Linux distributions for laptops(Opens in a new browser tab)

A native guide to the atop command in Linux(Opens in a new browser tab)

sudo for new user with privileges on Rocky Linux 8(Opens in a new browser tab)

Strategy Design Pattern in Java tutorial(Opens in a new browser tab)

Python 3 installing on Rocky Linux 9(Opens in a new browser tab)

Strategy Design Pattern in Java tutorial(Opens in a new browser tab)

Leave a Reply 0

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