Skip to content

Instantly share code, notes, and snippets.

@SeriousM
Forked from joshenders/swap.md
Created June 19, 2026 12:52
Show Gist options
  • Select an option

  • Save SeriousM/876d3b717730beec898a14f7da6960e1 to your computer and use it in GitHub Desktop.

Select an option

Save SeriousM/876d3b717730beec898a14f7da6960e1 to your computer and use it in GitHub Desktop.
Stop making swap partitions

Stop making swap partitions—use swap files instead!

Swap files have had the same performance characteristics as swap partitions for more than 20 years 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!

  1. Create a swap file.

💡 You can use dd for this too but if your filesystem supports it, fallocate is faster.

fallocate -l 4G /swapfile
  1. Only root should be able to write to the swap file.
chmod 0600 /swapfile
  1. Format the swap file.
mkswap /swapfile
  1. Enable it.
swapon /swapfile
  1. Make it persist across boots.
echo "/swapfile none swap defaults 0 0" >> /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment