Configuring a swap file in Linux

If you have a Linux computer running somewhere in your home, perhaps headlessly, you must be aware that some applications will use up a lot of memory, especially if your computer has paltry amounts of RAM. I have a Raspberry Pi configured as a headless torrenting computer and I noticed that its performance dropped from “bearable at best” to “absolutely horrible”. After a quick inspection, it became clear that the poor single board computer was running out of memory constantly, as exemplified by this line in top.

Almost all of the computer’s memory is in use, so we need to do something to fix this. Since installing more memory is out of the question (the RAM on Raspberry Pis and other single board computers is not upgradeable), we need to create either a swap partition or a swap file.

Creating a swap file is the easiest option of the two. Here’s how you do it.

Preparing the swap file

The first thing we need to do is to create a file that our computer can use for swapping data in and out of memory. The touch command won’t do us any good, because using that as a swap file will result in an error. To create the swap file, we’ll need to use the command

sudo dd bs=4096 count=250000 if=/dev/zero of=/swapfile

This will create a file in the root directory named swapfile with a size of 1 GB (as is apparent when you multiply the value of bs to the value of count). dd is a very slow program, so you’ll need to be patient. On my Raspberry Pi, it took more than seven minutes to create it.

When the process is complete, dd will output something similar to this

250000+0 records in
250000+0 records out
1024000000 bytes (1.0 GB) copied, 158.66 s, 6.5 MB/s

which tells us that the creation of the file is complete and we can proceed.

Setting the appropriate permissions

Before we can activate our newly-created swap file, we’ll need to set the appropriate permissions. For security reasons, it’s advised to give the root account read and write (but not execute) privileges and no access to the group or the world. We can do this by using the command

sudo chmod 0600 /swapfile

Activating swap

Now that the swap file is ready, we can activate swap with the command

sudo mkswap /swapfile && sudo swapon /swapfile

At the end, we can check that our swap file has been activated with the top command.

Making the swap file mount automatically

If we want to make the computer mount the swap file automatically each time it boots (and we do), we’ll need to add a line inside the /etc/fstab file. We actually talked about the fstab file a few weeks ago, so head over to this article if you need a refresher on that topic.

Open the fstab file with the command

sudo nano /etc/fstab

and add the following line

/swapfile none swap defaults 0 0

Then press Ctrl + X. Nano will ask if you want to save your changes. Press Y to confirm and then Enter to save.

About Andrea Luciano Damico 137 Articles
Andrea Luciano Damico is a freelance translator from Italy. Among his interests are linguistics, technology, video games, and generally being a chill guy. He runs Let's Translate.it and Tech4Freelancers.net.