Setting up swap space on a Linux server

Recently, I set up an EC2 host. It was a smooth and relatively quick process.

However, the problem with the default image is that it doesn’t come with swap space. You can verify this by running:

swapon -s

I found this tutorial online that walks you through creating a swap file, setting it up, and using it as your swap space:

All About Linux Swap Space

That post doesn’t mention it, so I’ll say it here: you need to use sudo in front of each of those commands. Another┬áthing missing from that tutorial is that as I was setting this up, the shell gave me this warning:

$ sudo swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.

So, I had to do this:

$ sudo chmod 0600 /swapfile

At the very end, you can check whether you were successful using the swapon -s command:

$ swapon -s
Filename Type Size Used Priority
/swapfile file 1048572 0 -1

Linux utilities: `watch` and `df`

I’ve been using these two tools a lot lately:

df – Display the amount of free space on your disks

watch – Run a command repeatedly, refreshing once every 2 seconds by default

My work involves a large amount of logging, so disk space needs to be monitored fairly closely. I use these utilities in conjunction with each other to keep track of how quickly I’m running out of disk space.

I find this script useful, too. However, because it recursively runs on every level below the working directory, it can take a while.