Create Swap file for Linux

To create a swap file on your linux box (specially on a Raspberry Pi box) login with root on the box, or run

sudo su

to run the following commands as administrator.

  1. Create 2G empty file (to calculate your desire swap size, use the following formula: Nr_of_Giga*1024*1024):

    dd if=/dev/zero of=/2GB.swap bs=1024 count=2097152

  2. Format the file for swap usage

    mkswap /2GB.swap

  3. Test the swap

    swapon /2GB.swap free

    now, you should see the Swap entry.

  4. Adding Swap at boot. Open /etc/fstab

    echo “/2GB.swap none swap sw 0 0” >> “/etc/fstab”

  5. Config the swappiness

    echo ‘vm.swappiness=30’ >> /etc/sysctl.conf

Now, your swap must be up on running.

Happy Swappiness!!!

Add Comment

Required fields are marked *. Your email address will not be published.