How to add a swap file in a Linux system

Please Note: This HowTo is pending a refactor. There are some things unclear in here. If you have any questions, Please reply to this post.

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.

I think you should do the same as in the script and put a swapfile somewhere in '/' with 'root' ownership .

Your Howto doesn't use a full path and will create the swapfile in the $USER home directory, which can become problematic for a number of reasons.

  • OTOH, I have no idea what happens if the machine effectively freezes and whether in that case, 'root' privileges could be abused higher up in the file hierarchy :thinking:

On top the swapfile needs to be added to /etc/fstab to have it mounted on every reboot.

Depending on what there's running (to be hibernated) you might find 100Mb (as in the example) lacking in size.
On top it would also depend on how much physical RAM is available. A 2Gb RAM will probably need swap space more often (and heavier) than an 8Gb machine.

I realise that; I was meaning hibernation on different sizes.

I know, but for the sake of example it was easier at the time (because my root partition is almost full every 3 seconds)

Yeah, but if the HowTo is only meant for your personal machine .... It should clearly state that. Otherwise others will think/assume that doing a copy/paste of your commands are sufficient to have a working swapfile.

As to your root partition:
Why not enlarge it?
Or create a separate partition for i.e '/opt' or '/var', and make some room. :thinking:

In my defence, I thought my

would be enough. Also, I didn't know having it in / would be better. I thought changing the owner to root and modifying the perms would be fine. :thinking:

Don't have time to d/l an Elive ISO, boot it up, and enlarge the partitions
I still haven't moved my GPU's placement to potentially get it actually detected by programs and I've known it might be the cause for like 2 weeks now

I'm not attacking you ..... I'm giving pointers as to where you could edit the HowTo to make it better.
I could do the edits myself but prefer you doing it yourself, considering it is your creation in the first place.

You don't need to (... or you could dl a smaller Linux for that).

Resizing your i.e '/home' partition can be done from single user mode (init 1) , from where you can then create an extra mount-point for one of the heavier directories in '/' and edit '/etc/fstab' to reflect the changes on reboot.
It takes some courage to do but isn't that hard.