How to Create a Swap Partition in Red Hat Enterprise Linux 9

How to Create a Swap Partition in Red Hat Enterprise Linux 9

If you are Linux user, you may have definitely heard about the swap partition but you may not know what it is and how to use it. It is time to change yourself and learn about Linux swap partition.

In this article, I will explain to you what a is swap and how to use it in more detail.

Linux uses your physical RAM to store temporary information. When RAM is full and there is no space left in it the Linux moves some of your information and writes it to the swap space on hard drive. This is called swapping process. In this way, the Linux releases some of the memory from RAM and the application doesn’t crash due to lack of RAM. So, Swap is useful and it extends the RAM and provides additional memory when the RAM space has been exhausted. That is why when you install the Linux operating system, it asks for a swap partition size.

Creating a swap partition in Red Hat Enterprise Linux 9

We have initially 2 GB Swap space and suppose we want to create an additional swap partition of 2 GB.

Open up the terminal and run the top command to verify your existing swap space.

top

Now, create an empty file called swapfile of 2 GB size with fallocate command in the root directory. You can create this file anywhere in your system.

fallocate -l 2G /swapfile

Let’s confirm it.

ls -lh /swapfile

As you can see from the screenshot, the file has been created.

Move ahead and make this newly created file a swap with the help of below command.

mkswap /swapfile

Swap on so that the changes can take effect.

swapon /swapfile

Lets confirm the swap by running the top command. As you can see from the screenshot, now the swap is 4 GB.

If you want this swap space permanently on your system, input the following entry in /etc/fstab

Conclusion

You can create a swap space in Red Hat Enterprise Linux 9 in 3-4 steps. First, you need to create a file in any empty location, make it a swap file with mkswap and then on this swap by using swapon.