Add Swap Space on AlmaLinux 9 with Best Staps – OrcaCore

Posted on

Add Swap Space on AlmaLinux 9 with Best Staps - OrcaCore

Add Swap Space on AlmaLinux 9 with Best Staps – OrcaCore

In this guide, we want to show you how to Add Swap Space 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 on the Orcacore website, we will show how to create and enable a swap file 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.

1. 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 is 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
Check Server Storage for Swap

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
check available storage space on AlmaLinux 9

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.

2. 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.

3. 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
Enable Swap File on AlmaLinux 9

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. A swap file is used by a computer to help manage memory when there is not enough RAM (Random Access Memory) available. When your computer runs out of RAM while running programs, it moves some data to the swap file on your hard drive or SSD. This helps keep the system running smoothly instead of crashing or slowing down.

Hope you enjoy it. You may also like these guides:

Install and Configure Squid Proxy on AlmaLinux 9

Install Lighttpd on AlmaLinux 9

Set up Samba Share on AlmaLinux 9

Install Prometheus in AlmaLinux 8

Run ActiveMQ on AlmaLinux 8

Install Adminer MySQL Database Management AlmaLinux 9

Upgrading or Updating MariaDB on AlmaLinux 9

Install Portainer AlmaLinux 8

How to Install Wireshark on AlmaLinux 9

Alternative Solutions to Add Swap Space on AlmaLinux 9

While creating a swap file using dd is a common method, here are two alternative approaches to add swap space on AlmaLinux 9:

1. Using fallocate and mkswap

The fallocate command is a utility to preallocate disk space for a file. It’s generally faster than dd because it doesn’t write actual data to the disk, but simply reserves the space. This can be a significant advantage, especially when creating larger swap files.

Steps:

  1. Allocate the space: Use fallocate to create and preallocate the swap file. For example, to create a 4GB swap file:

    sudo fallocate -l 4G /swapfile
  2. Set permissions: Set the appropriate permissions on the swap file:

    sudo chmod 600 /swapfile
  3. Format as swap: Format the file as a swap area:

    sudo mkswap /swapfile
  4. Enable swap: Activate the swap file:

    sudo swapon /swapfile
  5. Make it persistent: Add the entry to /etc/fstab as described in the original guide to make the swap persistent across reboots.

Explanation:

fallocate -l 4G /swapfile creates a file named /swapfile and immediately allocates 4GB of space on the disk for it. The rest of the process (setting permissions, formatting, enabling, and making persistent) is identical to the original method.

Advantages of using fallocate:

  • Speed: Faster file creation due to preallocation without data writing.
  • Less disk fragmentation: Can help reduce disk fragmentation as the space is allocated contiguously.

2. Using LVM (Logical Volume Management) for Swap

LVM provides a more flexible way to manage disk space, including swap. Using LVM allows you to easily resize the swap space later if needed, without the limitations of a fixed-size swap file or partition. This approach requires that LVM is already set up on your system.

Steps:

  1. Create a new volume group (if needed): If you don’t already have a volume group with sufficient free space, you’ll need to create one. This step is highly dependent on your existing disk configuration. Consult the LVM documentation for AlmaLinux 9 for detailed instructions on creating a volume group. Assuming you have a volume group called vg00, you can skip this step.

  2. Create a logical volume for swap: Use the lvcreate command to create a logical volume for the swap space. For example, to create a 4GB swap volume in the vg00 volume group:

    sudo lvcreate -L 4G -n swap lv00
  3. Format as swap: Format the logical volume as a swap area:

    sudo mkswap /dev/vg00/swap
  4. Enable swap: Activate the swap volume:

    sudo swapon /dev/vg00/swap
  5. Make it persistent: Add the entry to /etc/fstab as described in the original guide, but using the logical volume path:

    /dev/vg00/swap   swap    swap    sw  0   0

Explanation:

  • lvcreate -L 4G -n swap vg00 creates a logical volume named swap with a size of 4GB within the volume group vg00.
  • The remaining steps format the logical volume as swap, enable it, and make it persistent, similar to the other methods.

Advantages of using LVM:

  • Flexibility: Easy resizing of swap space later if needed. You can use lvextend and swapoff/swapon to increase the swap size dynamically.
  • Snapshot capabilities: LVM allows you to take snapshots of the system, which can be useful for backups and disaster recovery.

Disadvantages of using LVM:

  • Complexity: Requires understanding and configuration of LVM, which adds complexity.
  • Overhead: LVM introduces a slight performance overhead compared to using a dedicated swap file or partition.

These alternative methods offer different trade-offs in terms of speed, flexibility, and complexity. Choose the method that best suits your needs and existing system configuration when you add swap space on AlmaLinux 9. Before implementing any of these solutions, ensure you understand the implications and back up your data. Remember to thoroughly test your changes after you add swap space on AlmaLinux 9.

Leave a Reply

Your email address will not be published. Required fields are marked *