## Step 1 – Checking the System for Swap Information
You can verify that there is no active swap using the `free` utility:
```bash
free -h
```
Output:
```bash
total used free shared buff/cache available
Mem: 1.7Gi 173Mi 1.2Gi 9.0Mi 336Mi 1.4Gi
Swap: 0B 0B 0B
```
As you can see in the **Swap** row of the output, no swap is active on the system.
## Step 2 – Checking Available Space on the Hard Drive Partition
Before we create our swap file, we’ll check our current disk usage to make sure we have enough space. Do this by entering:
```bash
df -h
```
Output:
```bash
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 device with `/` in the `Mounted on` column is our disk in this case. We have plenty of space available in this example (only 1.4G used). Your usage will probably be different.
Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point. Another good rule of thumb is that anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.
## Step 3 – Creating a Swap File
Since the server in our example has 2G of RAM, we will create a 2G file in this guide. Adjust this to meet the needs of your own server:
```bash
sudo fallocate -l 1G /swapfile
```
We can verify that the correct amount of space was reserved by typing:
```bash
ls -lh /swapfile
```
Output:
```bash
-rw-r--r--. 1 root root 2.0G Sep 13 17:52 /swapfile
```
Our file has been created with the correct amount of space set aside.
## Step 4 – Enabling the Swap File
Make the file only accessible to **root** by typing:
```bash
sudo chmod 600 /swapfile
```
Verify the permissions change by typing:
```bash
ls -lh /swapfile
```
Output:
```bash
-rw------- 1 root root 2.0G Sep 13 17:52 /swapfile
```
We can now mark the file as swap space by typing:
```bash
sudo mkswap /swapfile
```
Output:
```bash
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=585e8b33-30fa-481f-af61-37b13326545b
```
After marking the file, we can enable the swap file, allowing our system to start using it:
```bash
sudo swapon /swapfile
```
Verify that the swap is available by typing:
```bash
sudo swapon --show
```
Output:
```bash
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
```
We can check the output of the `free` utility again to corroborate our findings:
```bash
free -h
```
Output:
```bash
total used free shared buff/cache available
Mem: 1.7Gi 172Mi 1.2Gi 9.0Mi 338Mi 1.4Gi
Swap: 2.0Gi 0B 2.0Gi
```
## Step 5 – Making the Swap File Permanent
Back up the `/etc/fstab` file in case anything goes wrong:
```bash
sudo cp /etc/fstab /etc/fstab.bak
```
Add the swap file information to the end of your `/etc/fstab` file by typing:
```bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
```
## Step 6 – Tuning your Swap Settings
### Adjusting the Swappiness Property
We can see the current swappiness value by typing:
```bash
cat /proc/sys/vm/swappiness
```
Output:
```bash
60
```
For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.We can set the swappiness to a different value by using the `sysctl` command.
For instance, to set the swappiness to 10, we could type:
```bash
sudo sysctl vm.swappiness=10
```
Output:
```bash
vm.swappiness = 10
```
This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our `/etc/sysctl.conf` file.
The default text editor that comes with RHEL based distro's is `vi`. `vi` is an extremely powerful text editor, but it can be somewhat obtuse for users who lack experience with it. You might want to install a more user-friendly editor such as `nano` to facilitate editing configuration files on your server:
```bash
sudo dnf install nano
```
Now you can use `nano` to edit the `sysctl.conf` file:
```bash
sudo nano /etc/sysctl.conf
```
At the bottom, you can add:
```bash
vm.swappiness=10
```
Save and close the file when you are finished. If you are using `nano`, you can save and quit by pressing `CTRL + X`, then when prompted, `Y` and then Enter.
### Adjusting the Cache Pressure Setting
Another related value that you might want to modify is the `vfs_cache_pressure`. This setting configures how much the system will choose to cache _inode_ and _dentry_ information over other data.
This is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the `proc` filesystem again:
```bash
cat /proc/sys/vm/vfs_cache_pressure
```
Output:
```bash
100
```
As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:
```bash
sudo sysctl vm.vfs_cache_pressure=50
```
Output:
```bash
vm.vfs_cache_pressure = 50
```
Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:
```bash
sudo nano /etc/sysctl.conf
```
At the bottom, add the line that specifies your new value:
```bash
vm.vfs_cache_pressure=50
```
Save and close the file when you are finished.
## Sources
[How To Add Swap Space on Rocky Linux 9 | DigitalOcean](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-rocky-linux-9)