This article provides guide demonstrating how to create a swap file on VPS servers.
What is a Swap File?
A swap file (also called a swap space or page file) is a special file on a computer’s storage drive (HDD or SSD) that the operating system uses as virtual memory when the system’s physical RAM (random access memory) is full.
How to Create a Swap File on VPS Servers
To create a swap file on VPS servers, follow the steps outlined below:
- Login via SSH as root user.
- Ensure the swap file is not already in existence or configured by checking the list of mounted partitions:
df -h - Create the swap file (our example uses the
/vardirectory):sudo touch /var/.swap.img sudo chmod 600 /var/.swap.img - Assign the preferred swap file size by writing zeros (ex: 4GB SWAP):
sudo dd if=/dev/zero of=/var/.swap.img bs=1024k count=4096 - Format the file as swap:
sudo mkswap /var/.swap.img - Activate the swap file:
sudo swapon /var/.swap.img - Make the swap file persist across reboots (optional):
sudo echo "/var/.swap.img none swap sw 0 0" >> /etc/fstab - Verify the swap is active:
swapon --show
The swap file will now be used if/when needed to supplement the system memory.
Conclusion
You now know how to creata a swap file on VPS servers.

👀 Choose SSD-powered VPS servers for increased speed, power, and security! Now 50% off- starting from only $3.19/mo.
