How to add a swap file in a Linux system

For those that don't like reading, a video

Note: This guide covers adding a swap file, not a swap partition. I have not tested hibernation with this method as I do not use hibernation, but if it does work it may be more secure on an encrypted Elive installation than a swap partition.

Adding a swap file in Linux is quick and painless. Please note that I learned all this from the ArchiveBot page on the Archive Team wiki, I'm just putting it in a more easily reachable place.

First, you want to create an empty file using dd. Make sure you have enough space for this (on the command line: df -h . before you start. This will make a 100MB file.

dd if=/dev/zero of=swapfile bs=1000 count=100000
Click for an explanation on the command.

dd is a command that copies raw data such as hard drives (although regular files work too). In this case, we're copying the special file /dev/zero which is an infinitely long file with only binary 0s in it (therefore nothing in it) into a file called swapfile (you can change that to whatever, you just need the of= before it). The bs field says how much to copy at a time; 4MB is ok. And count=100000 will copy 100 000 KB, or around 100MB of data (feel free to change that value - 1000000 is about 1GB).

Now make the permissions secure so people can't just waltz in and corrupt your swap:

chmod 0600 swapfile

Followed by:

sudo chown root swapfile

Now, we need to make it a swap file.

sudo mkswap swapfile

sudo will likely ask for your password unless you've used it in the past few minutes.

Finally, we need to enable the swap file.

sudo swapon swapfile

If/when you want to turn it off, it's as simple as sudo swapoff swapfile. If you use conky or a similar program, your swap capacity will slowly go down, sometimes as slowly as 20MB at a time. Eventually though, it'll be done.


If you know how to make a swap partition, another howto would be greatly appreciated! :smiley14:

This is already part of the install_elive_on_server.sh script that's being worked on by @Thanatermesis .
The embedded part reads as:

  # create a swap file
    if [[ "$( cat /proc/meminfo | grep -i memtotal | head -1 | awk '{print $2}' )" -lt 1500000 ]] && ! installed_check "swapfile" 1>/dev/null 2>&1 && ! swapon -s | grep -qs "^/" ; then
        if el_confirm "\nYour server doesn't have much RAM, do you want to add a swapfile?" ; then
            is_wanted_swapfile=1
        fi
    fi

    if ((is_wanted_swapfile)) ; then
        if ! [[ -s "/swapfile.swp" ]] ; then
            dd if=/dev/zero of=/swapfile.swp bs=1M count=1000
            chmod 0600 /swapfile.swp
            mkswap /swapfile.swp
            swapon /swapfile.swp
            addconfig "/swapfile.swp swap swap defaults 0 0" /etc/fstab
        fi

        installed_set "swapfile" "Swap file is created and running, special 'swappiness' and 'watermark_scale_factor' configurations added in /etc/sysctl.conf to not bottleneck the server's disk"
    fi
    # tune
    if swapon -s | grep -qs "^/" ; then
        addconfig "vm.swappiness = 10" /etc/sysctl.conf
        addconfig "vm.watermark_scale_factor = 500" /etc/sysctl.conf
    fi
   

It should be quite usable as a standalone and you could put together a few screenshots as a walkthrough/Howto ... maybe you could even repackage it in a simple GUI. :magick:

Yep, it does exactly what my tutorial does. I actually got the inspiration to make this howto from it, but I didn't take any code.

It just changes the variables to make swap more performant that Elive IIRC uses by default which actually I should add to this howto since it's a generic one, not an only-Elive one.