Logo

Your Personal Sysadmin

make a swap file on the fly

Disclaimer: this should only be used if you can’t partition your drive by yourself, or it would be a hazzle to do so. I’ve used that method to make one compile process work, otherwise I don’t really need it.

  1. Check for present swap space

    if there’s any output you might consider another solutions

    sudo swapon -s

  2. Create the actual file

    bs = times count equals filesize, in this case 1gb

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k

  3. Alternative:

  4. Create a linux swap area

    sudo mkswap /swapfile

    Output looks something like this:

    Setting up swapspace version 1, size = 262140 KiB no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

    sudo swapon -s

  5. Activate the swap file

    sudo swapon /swapfile

    Now `swapon -s` should show something like this

    Filename Type Size Used Priority /swapfile file 262140 0 -1

  6. Make it persistent in the /etc/fstab with the following entry

    /swapfile none swap sw 0 0

  7. Make swappiness 0, otherwise performance will be poor. So it’s just an emergency buffer

    echo 0 > /proc/sys/vm/swappiness

  8. make swappiness persistent

    echo 'vm.swappiness=0' > /etc/sysctl.conf

  9. A bit of good practice, since this is root’s business

    chown root:root /swapfile chmod 0600 /swapfile

Hey! I'll happily receive your comments via email

Andreas Wagner
Freelance System Administrator from Tallinn, Estonia.