Swap space is an important part of memory management since it protects the server from memory overloads and increases system stability. Here is how to set up swap space on an Almalinux8 server.
Steps for setting up Swap Space on AlmaLinux 8 :
To do this, we need to check the information on currently active swap spaces. The swapon command gives a list of active swap devices or files, with their sizes and utilisation.
#swapon -s
To check the system’s current memory and swap utilisation, run the “free” command:
#free -g
Output :
total | used | free | shared | buff/cache | available | |
Mem | 251 | 196 | 30 | 0 | 25 | 58 |
Swap | 4 | 3 | 1 |
Also check the disk usage and availability using df -h command
#df -h
Now, Create a new swap file of 8GB using the following command:
#dd if=/dev/zero of=/swap bs=1M count=8192
In this command, ‘of’ specifies the output file. ie, the partition or disk name where you want to write the data, here I am using the partition name /swap.
‘Bs’ is the block size (bs) for both reading and writing data. In this command, 1M sets the block size to 1 megabyte. And ‘count’ is the number of blocks to copy. In this command, 8192 is the count of blocks. Since the block size is set to 1 megabyte, this will create a swap file of 8GB.
We can use the ls -lh command to view and verify the space reserved for the swap
#ls -lh /swap
Also, specify the correct permissions for the swap file for improved security.
#chmod 600 /swap
The next step is to format it as a swap space using the mkswap command.
#mkswap /swap
Then activate the swap space located at /swap using the ‘swapon’ command
#swapon /swap
Now, use the ‘swapon -s’ command to check that the swap space is active
#swapon -s
Always remember to configure the partition to mount at boot time. This can be accomplished by configuring the fstab file.
Obtain the UUID of the filesystem /swap using the blkid command:
#blkid /swap
/swap: UUID=”7e00b266-887a-49f8-a2ca-e0b417899c1a” TYPE=”swap”
Now edit the fstab file to define how filesystems should be mounted at boot time.
#vim /etc/fstab
UUID=364e9b36-4d85-46a2-91fe-25443efaea87e9 / xfs defaults 0 1
UUID=6f11dd66-bfa0-40c7-8e5b-a1b39e567d72 /boot xfs defaults 0 0
UUID=512e47f5-3d6a-48f4-8eaf-dfccefec109e swap swap defaults 0 0
UUID=7e00b266-887a-49f8-a2ca-e0b417899c1a swap swap defaults 0 0
At last, run the ‘free’ command to verify that the changes made to the swap space configurations were applied correctly.
#free -g
Output :
total | used | free | shared | buff/cache | available | |
Mem | 251 | 190 | 36 | 0 | 25 | 58 |
Swap | 12 | 4 | 8 |
This is how we can add swap space to an Almalinux8 server. The expanded swap space setup will allow the server to meet the needs of your workload, ensuring an efficient and uninterrupted computing experience.
The members of our Support Team are available to help you with “How To Add Swap Space on Alma Linux 8” in case you experience any problems or glitches.