The Shift from Partitions to Files
Swap space is an essential safety net for Linux servers. When the system runs out of physical RAM, the Linux kernel begins moving inactive memory pages to the hard drive (the swap space) to prevent critical applications from crashing due to out-of-memory (OOM) errors.
Historically, Linux administrators created a dedicated swap partition during the initial operating system installation. However, resizing a physical partition later is difficult and risky. Modern best practices dictate using a Swap File instead. A swap file lives directly on your existing filesystem (usually the root / directory). It performs identically to a partition, but it can be created, resized, or deleted in seconds without any complex disk partitioning.
Step 1: Check Existing Swap
Before creating a new swap file, verify that you don’t already have one active. Run the following command:
sudo swapon --show
If the output is completely blank, your system has no active swap space. You can also verify your memory status by typing free -h and looking at the “Swap:” row.
Step 2: Create the Swap File
We will use the fallocate command to instantly allocate a contiguous block of space on the hard drive. In this example, we will create a 4 Gigabyte swap file.
sudo fallocate -l 4G /swapfile
Note: If fallocate fails (which can happen on some legacy filesystems like ZFS or older versions of XFS), you can use the slower dd command instead: sudo dd if=/dev/zero of=/swapfile bs=1M count=4096.
Step 3: Secure the File
Swap space contains raw data straight from your system RAM, which means it could hold unencrypted passwords, SSL keys, or sensitive application data. It is a massive security risk if standard users can read this file.
Lock down the file permissions so only the root user can read and write to it:
sudo chmod 600 /swapfile
Step 4: Format and Enable the Swap File
Now that the secure file exists, we must format it specifically for the kernel to use as swap space.
sudo mkswap /swapfile
Finally, activate the swap space:
sudo swapon /swapfile
Verify that it is working by running sudo swapon --show again. You should now see /swapfile listed with a size of 4G.
Step 5: Make it Permanent
If you reboot the server right now, the swap file will not turn back on. You must add it to the system’s file system table (/etc/fstab) to ensure it mounts automatically on boot.
Open the file in your preferred editor:
sudo nano /etc/fstab
Add the following line to the very bottom of the file:
/swapfile none swap sw 0 0
Save and close the file. Your swap space is now permanently configured.
Advanced: Tuning the Swappiness Value
The “swappiness” value controls how aggressively the Linux kernel will move data out of RAM and into the swap file. The value ranges from 0 to 100.
- A value of 100 tells the kernel to aggressively swap data to the disk.
- A value of 10 tells the kernel to avoid swapping as much as possible, relying purely on RAM unless absolutely necessary.
Desktop systems often use a default of 60. For database servers, a very low swappiness (like 10) is recommended to ensure the database remains in fast RAM.
To check your current value:
cat /proc/sys/vm/swappiness
To change it temporarily (until reboot):
sudo sysctl vm.swappiness=10
To make it permanent, add the line vm.swappiness=10 to the bottom of the /etc/sysctl.conf file.
Conclusion
Relying on physical swap partitions is an outdated practice that reduces storage flexibility. By provisioning secure swap files directly on your root filesystem, you can easily scale your server’s virtual memory up or down in seconds to meet the dynamic demands of your applications.