How to Create or Change a Swap File on CentOS Stream 10

To create or modify a swap file on CentOS Stream 10, follow the detailed steps below.

Creating a Swap File

Step 1 : Use the following command to check the current free and used memory:

Step 2 : Verify if there's enough disk space for the swap file:

Step 3 : Allocate a 2GB file for swap space:

Step 4 : Change the permissions to restrict access:

Step 5 : Format the file as swap:

Step 6 : Activate the newly created swap file:

Step 7 : Check if the swap file is active:

Step 8 : Add the following line to the /etc/fstab file to make the swap file persist across reboots:

Modifying an Existing Swap File

Step 9 : Verify the current swap usage:

Step 10 : Deactivate the swap file temporarily:

Step 11 : Adjust the size of the swap file. For example, to resize it to 1GB:

Step 12 : Ensure the permissions are correct:

Step 13 : Format the file again as swap:

Step 14 : Reactivate the swap file:

Step 15 : Check the updated swap file configuration:

Congratulations! You have successfully created or changed a swap file on your CentOS Stream 10 system.