How to add Swap file in Linux

2024-02-20
  1. Create a file that will be used for swap
    sudo fallocate -l 4G /swapfile
    
  2. Set the correct permissions for swap file
    sudo chmod 600 /swapfile
    
  3. Use the mkswap utility to set up the file as Linux swap area
    sudo mkswap /swapfile
    
  4. Enable the swap with the following command
    sudo swapon /swapfile
    
  5. if Need to make the change permanent, then update the /etc/fstab file and append the following line
    /swapfile swap swap defaults 0 0
    
  6. To verify that the swap is active
    sudo swapon --show
    free -h