Skip to content

Instantly share code, notes, and snippets.

@ka7eh
Last active November 18, 2020 21:15
Show Gist options
  • Save ka7eh/69b22a5a70de7c74fea2f270de7da0de to your computer and use it in GitHub Desktop.
Save ka7eh/69b22a5a70de7c74fea2f270de7da0de to your computer and use it in GitHub Desktop.
Modify swap files

Add a swap file:

Create a file (you can replace /swapfile with another location and filename): sudo fallocate -l 1G /swapfile
Change its permissions: sudo chmod 600 /swapfile
Mark the new file as swap space: sudo mkswap /swapfile
Enable the swap file: sudo swapon /swapfile
Check and see if the file is enabled: swapon --show
Update fstab to make your changes permanent (you might want to create a backup of the existing fstab): echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Adjust swappiness, if needed:

temporarily: sudo sysctl vm.swappiness=25
permanent: echo 'vm.swappiness=25' | sudo tee -a /etc/sysctl.conf

Resize a swap file:

Turn off the active swap file you want to resize: sudo swapoff /swapfile
Change the swap file size: sudo fallocate -l 2G /swapfile
Mark it as swap space: sudo mkswap /swapfile
Turn the swap back on: sudo swapon /swapfile

Remove a swap file:

Turn off the active swap file you want to resize: sudo swapoff /swapfile
Remove the file: sudo rm /swapfile
Clean up the entry for the removed swap file from /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment