Swap space is a disk space that is used as a temporary memory extension when the physical memory is full. CentOS 6 is an older version of the CentOS operating system that may still be used in some environments. In this guide, we will show you how to add Linux swap space on CentOS 6 VPS.
Step 1: Check Available Disk Space
Before adding swap space, you need to check the available disk space on your VPS. Run the following command to view the disk usage:
df -h
This command will display a list of mounted file systems and their disk usage information.
Step 2: Create a Swap File
Next, you need to create a swap file using the dd command. Run the following command to create a 1 GB swap file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
This command will create a file called swapfile in the root directory with a size of 1 GB.
Step 3: Set File Permissions
After creating the swap file, you need to set the correct file permissions using the chmod command. Run the following command:
sudo chmod 600 /swapfile
This command will set the file permissions to 600, which means that only the root user can read and write to the file.
Step 4: Set Up Swap Space
Now that you have created the swap file, you need to set it up as swap space using the mkswap command. Run the following command:
sudo mkswap /swapfile
This command will set up the swap file as swap space on your VPS.
Step 5: Enable the Swap File
Finally, you need to enable the swap file using the swapon command. Run the following command:
sudo swapon /swapfile
This command will enable the swap file as swap space on your VPS.
Step 6: Configure Swap Space
To make the swap file permanent, you need to configure it in the /etc/fstab file. Run the following command to open the file in the nano text editor:
sudo nano /etc/fstab
Add the following line at the end of the file:
/swapfile swap swap defaults 0 0
Save the changes and exit the editor by pressing Ctrl + X, then Y, and finally Enter.
Step 7: Verify Swap Space
To verify that the swap space has been set up correctly, you can run the following command:
free -m
This command will display the memory and swap usage information on your VPS.
Commands Mentioned:
- df -h – Check available disk space
- sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576 – Create a swap file
- sudo chmod 600 /swapfile – Set file permissions
- sudo mkswap /swapfile – Set up swap space
- sudo swapon /swapfile – Enable the swap file
- sudo nano /etc/fstab – Configure swap space
- free -m – Verify swap space
Conclusion
In this guide, we have shown you how to add Linux swap space on CentOS 6 VPS. By following these steps, you can create and enable a swap file to extend the physical memory of your VPS. It is important to remember that adding swap space is not a replacement for physical memory and should only be used as a temporary solution. We hope this guide has been helpful to you. If you have any comments or suggestions for improvements, please feel free to share them below.