How To Add Swap Space on AlmaLinux 9

In this guide, we want to show you to Add Swap Space (Swap File) on AlmaLinux 9.

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

In this guide, we will show how to create and enable a swap file on AlmaLinux 9.

Steps To Add Swap Space on AlmaLinux 9

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with AlmaLinux 9.

Check Server Storage for Swap

First, you need to check your server’s storage to see if you already have some swap space available.

You can see if the system has any configured swap by using swapon command, a general-purpose swap utility.

swapon -s

Note: If nothing is returned by the command, then the summary was empty and no swap file exists.

Another way is with the free utility, which shows us the system’s overall memory usage. You can see our current memory and swap usage (in megabytes) by typing:

free -m
Output
 total       used       free     shared    buffers     cached
Mem:          3953        315       3637          8         11        107
-/+ buffers/cache:        196       3756
Swap:            0          0       4095

As you can see, our total swap space in the system is 0. This matches what we saw with swapon.

At this point, you should check your available storage space on your AlmaLinux 9 server. To do this, you can use the following command:

df -h
Output
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G  1.5G   55G   3% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G  8.3M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

As you can see on the first line, our storage partition has 59 gigabytes available, so we have quite a bit of space to work with.

Note: Keep in mind that this is on a fresh, medium-sized VPS instance, so your actual usage might be very different.

Since my system has 4 gigabytes of memory and doubling that would take a larger chunk from my storage space than I am willing to part with, I will create a swap space of 4 gigabytes to match my system’s memory.

Create a Swap File on AlmaLinux 9

At this point, we will create a file named swapfile in our root “/” directory. To do this, you can use the dd utility:

sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB

This command will create a 4GiG file.

Then, you can verify that the correct amount of space was reserved for swap by using ls:

ls -lh /swapfile
Output
-rw-r--r-- 1 root root 4.0G Feb 11 11:00 /swapfile

Now you have a swap file and you must enable it.

Enable Swap File on AlmaLinux 9

At this point, you need to tell your system to format this file as a swap and then enable it.

First, you should set the correct permissions for the swap file to increase your security:

sudo chmod 600 /swapfile

This will restrict both read and write permissions to the root account only.

Verify that the swap file has the correct permissions by using ls -lh again:

ls -lh /swapfile
-rw------- 1 root root 4.0G Feb 11 11:00 /swapfile

Then, you can tell your system to set up the swap space for use by running the following command:

sudo mkswap /swapfile
Output
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b99230bb-21af-47bc-8c37-de41129c39bf

At this point, your swap file is ready to use as a swap space. To begin using it, run the command below:

sudo swapon /swapfile

You can see if the system has any configured swap again:

swapon -s
Output
Filename				Type		Size	Used	Priority
/swapfile               file        4194300 0     -1

Also, you can use the free utility again to corroborate your findings:

free -m
Output
             total       used       free     shared    buffers     cached
Mem:          3953        315       3637          8         11        107
-/+ buffers/cache:        196       3756
Swap:         4095          0       4095

At this point, your swap file is enabled at the moment, but when you reboot, the server will not automatically enable the file for use. You can change that by modifying the fstab file, which is a table that manages filesystems and partitions.

Edit the file with sudo privileges in your text editor:

sudo vi /etc/fstab

At the bottom of the file, you need to add a line that will tell the operating system to automatically use the swap file that you created:

/swapfile   swap    swap    sw  0   0

When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.

Conclusion

At this point, you have learned to Add Swap Space (Swap File) on AlmaLinux 9.

Hope you enjoy it. You may be like these guides on the Orcacore website:

Install and Configure Squid Proxy on AlmaLinux 9

Install Lighttpd on AlmaLinux 9

Set up Samba Share on AlmaLinux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!