Step 1: Check Current Swappiness Value
Before making any changes, it’s a good idea to check the current swappiness value on your system. Open a terminal and run the following command:
The output will be a number between 0 and 100, representing the current swappiness value. The default value is usually 30.
We can check the swappiness value with another command, and that is:
The output should be the following:
vm.swappiness = 30
Step 2: Temporary Swappiness Change
If you want to change the swappiness value temporarily (until the next reboot), you can use the sysctl command. Open a terminal and run the following command as root or using sudo:
sudo sysctl vm.swappiness=NEW_VALUE
Replace NEW_VALUE with the desired swappiness value you want to set. For example, to set the swappiness value to 10, you would use:
sudo sysctl vm.swappiness=1
This change will take effect immediately, but it will be reset to the default value upon reboot.
Step 3: Permanent Swappiness Change
To make the swappiness change permanent, you need to modify the system’s sysctl configuration file. Here’s how to do it:
Open the sysctl configuration file using a text editor. In the terminal, run the following command:
sudo nano /etc/sysctl.conf
In the file, add or modify the following line to set the desired swappiness value:
vm.swappiness = NEW_VALUE
Replace NEW_VALUE with the desired swappiness value. For example, to set the swappiness value to 10, the line would be:
vm.swappiness = 1
Save the changes and exit the text editor by pressing Ctrl + O to write the file and Ctrl + X to exit Nano.
To apply the changes, run the following command:
sudo sysctl -p
This command reloads the sysctl configuration file and applies the new swappiness value.
From now on, the system will use the modified swappiness value at every boot.
Step 4: Verify the Swappiness Change
To verify that the swappiness value has been changed successfully, you can once again check the current swappiness value using the command:
The output should now reflect the new swappiness value you set.
That’s it! You have successfully changed the swappiness value in CentOS or AlmaLinux. Adjusting the swappiness can help control how aggressively your system swaps memory to disk, potentially improving performance depending on your specific requirements.