The Linux Rain Linux General/Gaming News, Reviews and Tutorials

Tutorial: Creating a Swap File

By Andrew Powell, published 19/12/2013 in Tutorials


Have you found you forgot to create a Swap partition upon installing your favourite Linux distribution? Or simply have a nice layout of partitions on your hard drive that you do not wish to alter? Then let's instead create a "swap file" in Linux!

Even if you're primarily from a Microsoft Windows background, you may have heard of something called a "page file" or "Paging" in the Windows OS. While swap and page are not entirely the same thing, as in how they actually technically perform the task, from the end user's point of view they do a fairly similar thing (ie. lend your system a helping hand when you run low on physical memory, utilizing a section of your hard disk for extra memory).
I won't claim to be an expert on this either, but I am familiar with swap on GNU/Linux systems and often use a swap file in place of a designated partition. So let's begin!

Warning: The BTRFS filesystem does not currently support swapfiles. The only people who would be actively using BTRFS as of writing though would be aware of it (ie. advanced users). Everyone else should be fine.

Creating a swap file is actually very easy, but you will need to fire up a terminal window (shock, horror!). Do not worry though, it's only a few commands which could just about be copy/pasted. However you will need root privileges. On an Ubuntu-based system, you could just add "sudo" before all these lines I'm about to show you. Everyone else, if you don't have sudo available you could just type "su" and enter the root password. Then these commands could be entered as they are (but please use care! I am not responsible if you break your Linux install or somehow magically set your computer on fire).

I recommend the Arch Wiki's guide on creating a swap file which is short and sweet and would work for almost every distro out there, but if anything is unfamiliar there, I am documenting essentially the same information below with extra comments/explanations.

First off, we'll use the 'fallocate' command to create the actual file correctly:

fallocate -l 512M /swapfile

This would create a 512MB file. You may replace the "512M" with, for example, "2G" (G being gigabytes and M being Megabytes) if you wanted a 2GB swap file. The common rule of thumb is to choose a swap size that is double the amount of RAM you have.

Next, we modify the file's permissions, to make sure they are correct (for security purposes):

chmod 600 /swapfile

Then, we use the mkswap command to format the file to be suitable for swap:

mkswap /swapfile

Finally we can actually activate our new swap file, using the swapon command, which will instantly give our system access to the swap.

swapon /swapfile

Fire up your favourite system monitor program (eg. Gnome System Monitor) to see what I mean. In the RAM usage section, you should see a Swap section, which would show something like 0MB/2048MB or similar (depending on the size of swap you chose of course) instead of 0MB/0MB or No Swap, etc.

Finally, if you wish to make this change permanent and persistent after a reboot, you will need to make an additional entry in the /etc/fstab file. So fire up that file in your favourite text editor. Once again you'll need root privileges.
If you're new to this and/or want to use a GUI editor, this sort of command should work to open the file in a GUI editor with root privileges, if you're on an Ubuntu system or similar:

gksudo gedit /etc/fstab

Otherwise feel free to leave a comment on this article and I'll be happy to help you open the file if you have trouble. Next, this is the line you need to add to the end (bottom) of the fstab file:
Tip: Make sure the the entry is all one line!

/swapfile none swap defaults 0 0

Save that file and close it. You are done!

Resources:



About the author

Andrew Powell is the editor and owner of The Linux Rain who loves all things Linux, gaming and everything in between.

Tags: swap filesystem linux ram
blog comments powered by Disqus