Swap files have had the same performance characteristics as swap partitions for almost 20 years[1] and yet, linux distributions continue to encourage the use of swap partitions during install.
Swap files are easier to use/add/remove/modify/extend after installation. They're better in every way––use swap files!
- Create a swap file.
💡 You can use
dd
for this too but if your filesystem supports it,fallocate
is faster.
fallocate -l 4G /swapfile
- Only root should be able to write to the swap file.
chmod 0600 /swapfile
- Format the swap file.
mkswap /swapfile
- Enable it.
swapon /swapfile
- Make it persist across boots.
echo "/swapfile none swap defaults 0 0" >> /etc/fstab
If you're using btrfs:
https://btrfs.readthedocs.io/en/latest/Swapfile.html