notebox

Creating a Swap File in Linux

I follow the following steps to create a swap-file.

  1. # Create the swap file
    sudo fallocate -l 11G /swapfile
    

I have 8GB RAM on my system, so I choose to have 11GB of swap. This is enough to allow me to hibernate if I want to.

  1. # Only root should be able to read this file
    sudo chmod 600 /swapfile
    
  2. # Set the file as the swap area
    sudo mkswap /swapfile
    
  3. # Enable the swap
    sudo swapon /swapfile
    
  4. To make this setting permanent, add a record to /etc/fstab
    # in /etc/fstab
    /swapfile swap swap defaults 0 0
    
  5. Verify that the changes took effect
    sudo swapon --show
    

References