How to create a swap partition?

Swap space is a dedicated area on a hard drive that serves as virtual memory for your operating system. It allows your system to temporarily store data that is not actively used in RAM. Creating a swap partition on CentOS/AlmaLinux can help improve overall system performance and prevent out-of-memory errors. In this article, we will guide you through the process of creating a swap partition on CentOS/AlmaLinux.

Step 1: Checking Existing Swap Space: Before creating a new swap partition, it’s essential to check if any existing swap space is already configured on your system. You can use the “swapon” command to verify if a swap partition or file is in use:

 free -m

If the value of ‘Swap:’ in the output shows zero, that means there is no swap space created.

          total      used     free     shared   buff/cache  available
Mem:       7820       112     7643         8       65         7537
Swap:         0         0        0

If this command returns any output, it means that a swap partition or file is already in use. In that case, you can either resize the existing swap partition or create an additional one if needed.

Step 2: Partitioning the Disk:

Now, let’s create the swap file. I’m going to create a 2GB file. However, you may change it depending on your needs. It is recommended to set the swap file size based on your system’s needs. The common practice is to allocate a swap space equal to the amount of RAM or, in some cases, half the amount of RAM.

It is often recommended to allocate a swap space that is approximately half the size of the available physical memory. However, it is generally not recommended to create a swap file larger than 4GB.

sudo dd if=/dev/zero of=/swap count=2048 bs=1MiB

Now Set the correct permissions on the swap file:
Adjust the permissions on the swap file to restrict access to only the root user:

sudo chmod 600 /swap

Step 3: Formatting the Swap Partition:

Use the following command to format the swap file & Make the swap file usable

sudo mkswap /swap

Step 4: Activating the Swap Partition:

Now enable the swap partition

sudo swapon /swap

Now, we can test if the swap partition was created and running correctly. Run the memory checking command again:

 free -m

Step 5: Making the Swap Partition Permanent:

To make the swap partition automatically mounted after each reboot, execute the following command to add it to your system’s fstab config.

 sudo echo "/swap swap swap sw 0 0" >> /etc/fstab

Reboot the system using the following command.

 sudo reboot

Step 6: Verifying the Swap Partition:

You can use the “free” command to check if the swap partition is now active

  free -h

The output should display information about the available swap space.

By following the steps outlined in this article, you should be able to create a swap partition on CentOS/AlmaLinux. Remember to adjust the partition identifiers and device names according to your system configuration. Having an adequately sized swap space can help improve the performance and stability of your system.

Leave a Reply

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