How to Add Swap Space on AlmaLinux 10

Swap space is used when the system's physical memory (RAM) is full, providing additional virtual memory by utilizing disk space. Follow this guide to add a swap file as swap space on an AlmaLinux 10 system.

Check Current Swap Space

To check the existing swap space on your system, run the following command:

swapon --show

Output:

NAME      TYPE      SIZE   USED   PRIO

NOTE: If the output is empty, it means that swap space is not enabled yet on your system.

To verify memory and swap details:

free -h

Output:

[root@server ~]# free -h
               total        used        free      shared  buff/cache   available
Mem:           5.5Gi       434Mi       5.1Gi       8.5Mi       171Mi       5.1Gi
Swap:          2.0Gi          0B       2.0Gi

Create a Swap File

To create a swap file with a size of 2GB (adjust the size as needed), use the following command:

fallocate -l 2G /swapfile

If fallocate is not available, use:

dd if=/dev/zero of=/swapfile bs=1M count=2048

Set the correct permissions for the swap file:

chmod 600 /swapfile

Set Up the Swap File

Now, format the file as swap space:

mkswap /swapfile

Output:

[root@server ~]# mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=1009a4b9-7853-40f6-bb06-eac347b6f6c8

Enable the swap file:

swapon /swapfile

To verify that the swap space is active, run:

swapon --show

Output:

[root@server ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/vda3 partition   2G   0B   -2
/swapfile file        2G   0B   -3

Make the Swap File Permanent

Edit the /etc/fstab file to ensure the swap file is enabled at boot:

nano /etc/fstab

Add the following line to the file:

/swapfile none swap sw 0 0

Save and exit the editor (Ctrl+O, Enter, Ctrl+X).


Adjust Swap Settings (Optional)

Modify the swappiness value to control the frequency of swap usage:

sysctl vm.swappiness=10

To make this change permanent, add the following line to /etc/sysctl.conf:

vm.swappiness=10

Reload the settings:

sysctl -p

Verify Swap Configuration

Confirm that the swap file is active and permanent by running the following commands:

swapon --show
cat /proc/swaps
free -h

Output:

NAME      TYPE      SIZE USED PRIO
/dev/vda3 partition   2G   0B   -2
/swapfile file        2G   0B   -3

[root@server ~]# free -h
               total        used        free      shared  buff/cache   available
Mem:           5.5Gi       451Mi       3.1Gi       8.5Mi       2.2Gi       5.1Gi
Swap:          4.0Gi          0B       4.0Gi

Conclusion

You have successfully added and configured swap space on AlmaLinux 10. The swap file will now be used as virtual memory whenever physical memory runs low, ensuring smoother system performance.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS