Creating a Swap File on Your Linux VPS
· Updated: Aug 17, 2026 · Linux, VPS, Memory Management
Table of contents
Introduction
Adding a swap file to your Linux Virtual Private Server (VPS) can significantly enhance system performance and optimize memory management. In this guide, we will provide a step-by-step walkthrough to help you create a swap file on your Linux VPS.
Why Enable Swap?
Enabling swap on your Linux VPS system can significantly enhance its performance and ensure smoother operations. Linux is known for its ability to run efficiently on low-spec hardware, making it possible to operate servers even with limited resources such as 512MB RAM or 1GB RAM. However, there are instances when these resources may prove inadequate for certain workloads.
This is where a swap file comes into play. It acts as a supplementary form of memory that the system can utilize when physical RAM is exhausted. Here’s why enabling swap is beneficial:
Swap gives your VPS extra memory headroom. When processes need more RAM than you have, the kernel moves inactive pages to swap instead of killing them, so memory-hungry workloads keep running and the system does not freeze or crash. On a small-RAM VPS that alone can be the difference between working and OOM.
Step 1: Checking Existing Swap Space (Optional)
Before proceeding with creating a new swap file, it’s advisable to check if your VPS already has active swap space. Open a terminal and run the following command:
sudo swapon --show
If there is no output, your VPS likely doesn’t have active swap space, making it an ideal candidate for creating a new swap file.
Step 2: Creating the Swap File
To create a 2GB swap file, follow these steps:
-
Connect to your VPS via SSH.
-
Execute the following command to create a 2GB swap file (you can adjust the size as needed):
sudo fallocate -l 2G /swapfile
- Set appropriate permissions for the swap file:
sudo chmod 600 /swapfile
- Prepare the swap file:
sudo mkswap /swapfile
Step 3: Enabling the Swap File
Once the swap file is created, enable it with the following command:
sudo swapon /swapfile
Step 4: Making the Swap File Permanent
To keep the swap file after a reboot, add an entry to /etc/fstab. What the fields mean:
defaults: standard mount options, includingrw(read-write) andauto(mount at boot).0: the fsck check order. Swap is not checked, so 0.0: thedumpbackup flag. Swap is never dumped, so 0.
- Open
/etc/fstabwith a text editor such as nano:
sudo nano /etc/fstab
- Add this line:
/swapfile swap swap defaults 0 0
- Or add the entry with one command:
sudo sh -c "echo '/swapfile swap swap defaults 0 0' >> /etc/fstab"
This optional command adds the swap entry to /etc/fstab.
sudo: runs the command as root, needed because/etc/fstabis root-owned.sh -c: runs the quoted string as a shell command, so the>>redirect is done by that shell rather than your current one.echo ... >>: appends the swap line to/etc/fstab.
Step 5: Adjusting Swappiness (Optional)
Swappiness is a kernel parameter that controls the tendency of the Linux system to move processes out of physical memory (RAM) and onto the swap file. By adjusting the swappiness value, you can fine-tune how aggressively your system uses swap space.
Here’s how you can do it:
- Open the
/etc/sysctl.conffile using a text editor like nano:
sudo nano /etc/sysctl.conf
- Add the following line at the end of the file, adjusting the value to your preference (replace
10with your desired value):
vm.swappiness=10
The swappiness value ranges from 0 to 100. A lower value, like 10, means the system will use swap space less aggressively, favoring physical memory. A higher value, like 60, makes the system more willing to use swap space.
- Save the file and exit the text editor.
Modifying the swappiness value is useful if you have sufficient RAM, and you want to minimize the use of swap space unless it’s absolutely necessary. Conversely, if you find your system frequently using swap even when there’s available RAM, you might consider raising the swappiness value to make more use of the swap space.
Step 6: Rebooting Your VPS
To bring the changes into effect, initiate a reboot of your VPS:
sudo reboot
Step 7: Verifying Swap Activation
Check that the swap file is active with htop, btop, or swapon --show.
-
htop: Launch the
htoputility to visualize system processes and memory usage. Look for the swap entry in the memory information section. -
btop: Similarly, you can use
btopto monitor system resources. Check for swap information in the displayed data. -
swapon --show: Alternatively, execute the commandswapon --showin the terminal. This will provide a concise overview of active swap devices and their respective sizes.
If swap shows up with a size, it is working.
Bonus: Deleting a Swap File (If Needed)
If you find yourself needing to remove the swap file from your Linux VPS, you can follow these steps:
-
Disable Swap: To begin, deactivate the swap file using the following command. This command includes the
--verboseoption to provide detailed feedback on the operation:sudo swapoff --verbose /swapfileThe
--verboseflag ensures that you receive clear feedback about the deactivation process. This step is crucial before proceeding with the removal of the swap file.
Remember, each step should be performed carefully to ensure the proper functioning of your system.
-
Remove the Entry from /etc/fstab: Open the
/etc/fstabfile in a text editor:sudo nano /etc/fstab[Optional] One-line Solution: To remove the line associated with the swap file from
/etc/fstab, which typically resembles/swapfile swap swap defaults 0 0, you have two options. First, manually locate and delete the line. Alternatively, you can use a single command to achieve this by employingsed:sudo sed -i '/\/swapfile/d' /etc/fstab
After executing the command, save the file and exit the text editor to complete the process. Please exercise caution when using the sed command, as it directly modifies files. Always ensure you have proper backups or are confident in the changes you are making.
-
Delete the Swap File: Finally, delete the swap file using:
sudo rm /swapfileConfirm the deletion if prompted.
Please exercise caution when using the sed command, as it directly modifies files. Ensure that you have a backup or are confident in the changes you are making.
Remember, removing a swap file can impact your system’s performance, so make sure to consider your system’s requirements before proceeding with the deletion.
Conclusion
By understanding the process of creating, adjusting, and if necessary, deleting a swap file, you have gained valuable insights into managing memory on your Linux VPS. These techniques empower you to fine-tune your system’s performance and ensure optimal resource utilization, even during resource-intensive tasks.