How to Add 1 GB Swap Space in Ubuntu VPS
Adding swap space to your Ubuntu VPS can help improve performance by providing additional virtual memory. Follow these simple steps to add a 1 GB swap file to your server.
Step 1: Create a Swap File
First, create a swap file with 1 GB of space:
sudo fallocate -l 1G /swapfile
If fallocate
isn’t available, use:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
Step 2: Set Correct Permissions
Ensure only root has access to the swap file:
sudo chmod 600 /swapfile
Step 3: Mark the File as Swap Space
Format the file for use as swap space:
sudo mkswap /swapfile
Step 4: Enable the Swap File
Activate the swap file immediately:
sudo swapon /swapfile
Step 5: Make the Swap Permanent
Add the following line to /etc/fstab
to make the swap file permanent:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 6: Verify Swap Space
Check if the swap is active:
sudo swapon --show
Or view overall memory usage:
free -h
Optional: Adjust Swappiness
To control how often the system uses swap space, adjust the swappiness
value:
sudo sysctl vm.swappiness=10
Make the change permanent by adding this to /etc/sysctl.conf
:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Conclusion
By following these steps, you can easily add and configure a 1 GB swap file on your Ubuntu VPS, helping your server manage memory more efficiently. Happy hosting!