Jump to content

Remove and Create new swapfile in Debian


FunkyBuddha

Recommended Posts

I kept getting alerts from Netdata that my swapfile is running low. I'm sharing a script that does this for you to minimize manual configuration.

 

What does the script do:

  • Creates an 8G swapfile
  • Sets permission for root only
  • Edit fstab to add new swapfile
  • Tweak a couple of swapfile configurations
  • Pause script to review output from commands
  • Edit fstab - User needs to manually comment old swap file

 

IMPORTANT Prep Step:

  • Run "lsblk" command (This will let you know the current location of swapfile)
  • Run "sudo swapoff /dev/sdxy" (This disables the current swapfile. Replace /dev/sdxy with current swapfile location)
  • Run "sudo swapon --show" (If everything went well, this should not produce any output)
  • Run script below.
  • If everything went well, this should show the new swapfile size.
  • Reboot and pray that you haven't foobared your server. LOL

 

Disclaimer:

I've applied this script to several Debian servers in my homelab without any issues. Use at your own risk. 

 

Linux Script:

Quote

sudo swapon --show
free -h
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
free -h
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
echo 'vm.vfs_cache_pressure = 50' | sudo tee -a /etc/sysctl.conf
read -n1 -r -p "Press any key to continue..." key
sudo nano /etc/fstab
sudo swapon --show
free -h
 

 

Link to comment

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...