How To Add Swap in AlmaLinux 8 / 9


INTRODUCTION

One of the easiest ways to make your server more responsive, and guard against out-of-memory errors in your application, is to add some swap space.
Swap is an area on a storage drive where the operating system can temporarily store data that it can no longer hold in memory.

This gives you the ability to increase the amount of information that your server can keep in its working memory, with some caveats.
Reading from and writing to swap is slower than using memory, but it can provide a good safety net for when your server is low on memory.

Without swap, a server that runs out of memory may start killing applications to free up memory, or even crash. This can cause you to lose unsaved data or experience downtime.

To ensure reliable data access, some applications require swap to function correctly.

In this article, we will cover how to create and enable a swap file on an AlmaLinux 8 or 9 Server.


READ ME FIRST

This article is provided as a courtesy.
Installing, configuring, and troubleshooting third-party applications is outside the scope of basic support provided by Starburst Services.

We are not liable for any damages and/or data loss.


REQUIRMENTS

  • SSH credentials or File Manager access Logged In as Root
  • Text editor of your choice. (The example uses nano)

PROCEDURE

STEP 1

Check to see if SWAP is setup

Run:

free -h

SWAP should show 0.
If it doesn’t and has a size, then Swap is already setup on the server.


STEP 2

Create the Swap file

The below example will create a 8GB Swap File with a 1MB Block Size.
Run:

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

STEP 3

Enable the Swap file

Run:

chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

STEP 4

Make the Swap File Permanent

Run:

nano /etc/fstab

Add the following to the bottom:

/ swapfile swap swap sw 0 0


STEP 5

Tweak Your Swap Settings (Optional)

There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.
These configurations are optional in most cases, and the changes that you make will depend on your application needs and your personal preference.

Run:

nano /etc/sysctl.conf

Add the following to the bottom:

vm.swappiness = 10
vm.vfs_cache_pressure = 50


Thanks for learning with Starburst Services.

Learn more about our services


Did you found this Knowledge Base Article useful?

Please considering thanking our SysOp Team.


KB Article Written: 2024-09-22

people found this article helpful. What about you?