Adding & Creating Swap Files in Ubuntu/Debian


swap

Prerequisites

This guide is intended for a Debian or Ubuntu derived Linux distribution. Which can be on a client machine or VPS with SSH/VNC access. You can purchase VPS server hosting via Hoverdata, who look to provide high quality server hosting at affordable competitive prices.

Introduction 

When memory (RAM) becomes completely maxed out on a Linux/Unix system, the swap file or swap partition comes into play. This concept works by allocating a set amount of hard drive space to act the same way as the normal primary memory would. Providing more room for holding on-going data temporarily whilst the RAM is full. This process is often referred to also as paging or instead of swap file as a page file (in other computer systems mostly).

Let’s see how we can go about configuring a swap file in Ubuntu/Debian that is created with through our own input and not any system default ones. Be aware that this tutorial will create the file on the same partition as the OS file system, and not on a separate dedicated partition.

Step 1 —  Check Existing Swap Files

It’s a good idea to check and see what (if any) swap files are already in existence on the machine.

This can be checked through the swapon command on your Linux VPS/machine:

If you receive output but with empty columns. This means there are no pre-configured or pre-existing swap files in existence on your system.

Other outputs show most notably the “Type” of swap and “Size” the swap is in total. Types will either be partition or file and the size is shown in bytes.  Any space that is at present being filled by the OS is also displayed here for you to see under the “Used” column.

Regardless of whether or not you find you already have some swap configs implemented, you can still proceed and continue to setup another one. Or the first if you have none.


Step 2 — Check Available File-System Space

Before actually creating the swap file we need to know how much free space we have to work with on our main system partition.

Using this next command shows us:

The data here is a bit more concise this time round as we passed the -h parameter to the program which gives us “human readable” formatting on the output i.e. the values are in Gigabytes, Megabytes, etc.

Your values here should be contrasting from user to user as everyone will have a different hardware setup and or hosting plan, with varying potential space open to them.  Partition names will vary too but you’re looking for the root file system sometimes named rootfs most frequently listed on the first line of output.

It’s mounted on / as noted in the last column from the above output. Some providers depending upon their own custom kernel configuration use /dev/sdx or in virtualised environments /dev/vdx where ‘x’ is a letter such as a,b,c for the first column also.


Step 3 — Create the Swap File

To create the planned swap file you’ll need to examine the information from the last step and decide how big you would like the swap to be. There is no set rule of thumb when determining what size to use so the figure is likely to be preferential to the user and system in question.

One marker often used is to take the total amount of RAM hardware you have and use this size for the swap file. So if you have 2GB of RAM then you would have a 2GB swap file. Some double this figure instead, resulting in 4GB instead of 2 for our example. The choice is up to you as long as it is a neither a negligible or extreme amount to allocate, and keep in mind a large page file is not a substitute for inadequate physical system memory.

Once an amount is settled on create your file with this upcoming command structure. Where the digit (2) is the size you want your swap file to be and the uppercase letter (G) is either G for gigabytes or M for megabytes. It’s possible to give the file any name you wish too. I have called mine “swap-file” for this example.


Step 4 — Validate the Swap File

The file is now formed in the system’s root directory. Next this file needs to be excluded from use and access from other users plus entities. This can be done relatively easily by changing the permissions applied to the file with the chmod command.

Make sure to replace the file name I have used with your own.

Our next two commands turn the file into actual swap capable data whilst enabling it for use by the OS when required.

Note: Substitute your chosen file name for my “swap-file” once again just like in all of these examples.


Step 5 — Make the Swap File Persistent

Unfortunately this creation we have performed is not permanent, which means when the system restarts, reboots, or shuts-down the swap we have put together does not continue to get used by the operating system.

To enable it on boot append this line to the /etc/fstab file with any text editor

Replacing the first item in this line with your own swap file’s name one final time.

The swap file is now up and running and persistent between system restarts. Good job on completing this post.

Read the conclusion for some hints and tips and what to take away with you when it comes to swap files.


Conclusion

Setting up this swap space gives your system memory some extra leeway in processing and holding data, but if you find yourself running into problems of this nature you should consider upgrading your server hosting plan or machine’s memory. You can go even further by looking into how to optimise swap files should you desire, if you doing anything more though remember these key points:

  • Linux/Unix operating systems prioritise newer running data in main memory but can use swap space when overloaded to store extra data temporarily.
  • This “swap space” should always only be used as a failsafe and spill over as it takes longer to read/write to the data to physical HDDs.
  • It’s more common for swap space to be kept on a dedicated partition whenever possible instead of just in a system file.
  • The program “free” can be run to see the amount of free and used memory on a Linux system, and where it’s coming from.

Thanks for reading.

Ask any questions in the comments and we will try our best to respond.