In this tutorial I’ll explain how to configure and initiate the use of an external removable swap space. Many different conditions and situations could lead you to want more or external swap space on your Linux machine.
Possible Use Cases:
- Your system is running slow and you believe a lack of available RAM (Random Access Memory) to be the problem.
- You are performing clandestine operations on a machine and wish to increase your available swap space without leaving any traces of your activities on the machine.
INTRO AND IMPORTANT NOTES
In this example I’ll be showing how to perform these operations using my Ubuntu 16.04 LTS 64-bit system. (Here is how you can check your system information.)
Please be aware that if you wish to use a USB flash drive as a swap space, you could shorten the lifespan of your USB flash drive considerably (especially if the device is more than a couple years old).
For those of you who are unfamiliar with swap space, here is the definition presented by the official Ubuntu Documentation:
Swap space is the area on a hard disk which is part of the Virtual Memory of your machine, which is a combination of accessible physical memory (RAM) and the swap space. Swap space temporarily holds memory pages that are inactive. Swap space is used when your system decides that it needs physical memory for active processes and there is insufficient unused physical memory available. If the system happens to need more memory resources or space, inactive pages in physical memory are then moved to the swap space therefore freeing up that physical memory for other uses. Note that the access time for swap is slower therefore do not consider it to be a complete replacement for the physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.
Additional information and resources can be found in the following locations:
1) IDENTIFY YOUR SWAP DEVICE
Start by inserting the device that you wish to use as a swap space. Ensure that the device you have chosen is one which has nothing important or valuable on it, as all content on this device will be formatted (deleted) during this process. Once your device is inserted, open the terminal and type the following command:
If you were not previously using the terminal to perform administrative tasks, you will be asked for your password at this point. Enter your password and press the Enter key on your keyboard.
You should now see a full readout of all devices connected to your computer in a list with a few various tab-separated values. Those different values fall into the following categories:
- Device Path Name
- This is the location of the device in your file structure
- Example: /dev/sdi1
- Example: /dev/sdf1
- Device Label
- If your device was given a name or “label”, that device name will appear in this section. If a device doesn’t have a label then the “LABEL” section will not appear on the line describing that particular device.
- Example: LABEL=”Bobs-Big-Hard-Drive”
- Universally Unique Identifier (UUID)
- This is the globally unique digital “fingerprint” of your device
- Example: UUID=”72C0DE8EC0DE57C5″
- Example: UUID=”30fcb748-ad1e-4228-af2f-951e8e7b56df”
- Device Type
- The file structure method of the device will appear here
- Example: TYPE=”swap”
- Example: TYPE=”ntfs”
- Partition UUID
- If the device is a storage partition, then the UUID of that partition will appear here.
Look down the list displayed in your terminal and identify which device you inserted earlier to be used as a swap space. Once you find your device in the list, there are two sections of information that you need to take note of. The two sections are, Device Path and the Universally Unique Identifier (UUID). Once you have copied these two sections of information down onto a notepad/Leafpad document, then you have successfully identified your device. Now on to the next step…
2) UNMOUNT YOUR DEVICE
In order to turn your connected device into a swap space, you first have to unmount the device. To unmount (or to ensure that your device is unmounted), type the following command into your terminal:
sudo umount /dev/xxx
[Where “xxx” is your device’s path name.]
3) FORMAT YOUR DEVICE
The formatting process will completely erase all data on your selected device and will reconfigure the organizational structure of that device to be used as a swap space. Please be careful to enter the correct device path name in this step (as entering an incorrect drive path name here could result in the total formatting of a different drive/device and the loss of all data on that device). To perform the formatting function, type the following command into your terminal:
sudo mkswap /dev/xxx
[Where “xxx” is your device’s path name.] If performed correctly, your output should be similar to the following:
NOTE: The formatting process can also be carried out using the GParted tool if you wish to use a graphical interface instead (this tutorial does not cover the use of GParted).
4) INSTALL GKSU PACKAGE
For this next step you’re going to need a tool called “gksu”. It used to be included in the standard Ubuntu distributions and no longer is included so you’ll have to grab it and install is using the simple commands below:
sudo apt-get update sudo apt-get install gksu
5) IDENTIFY SWAP UUID
Several things have taken place since step #1 above, and the UUID of your intended swap space device has likely changed (during the swap space formatting process), so we need to get a look at the updated UUID. To do so, issue the same command we used last time:
Copy down the UUID of your newly-formatted swap space device.
6) EDIT /etc/fstab
Use the gksu tool to edit your system file located at /etc/fstab from within your terminal. The reason you must edit this system file is so that your system knows WHERE this new swap space is located and so that your system knows that it SHOULD try to connect and use the swap space every time your computer is turned on and running. Begin the editing process by entering the following command into your terminal:
gksudo gedit /etc/fstab
Upon issuing the command above, you will have a window pop up which contains the contents of /etc/fstab (Please be careful while making edits to this document as it controls some important functions in your system). Move your cursor to the end of the document and press Enter to add a new line. Then type the following:
# External SWAP UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX none swap sw,pri=5 0 0
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX” is the UUID of your new swap device as discovered in step #5 above.]
After entering the information above, click the “SAVE” button in the corner of that window to save your edits. At this point, your terminal may display an error related to document metadata failure but this error has no effect upon this operation so no worries – everything is working fine. Now we move on to the final steps.
7) ACTIVATE YOUR SWAP DEVICE
Activate the use of your new external swap device by typing the following command into your terminal window:
sudo swapon -a
Then verify which devices are being used as swap spaces with the following command:
After inputting the command above, you will see your new external swap space listed as an active swap space being used by your system.
THOUGHTS IN CLOSING
Never remove your swap device while your machine is in use. Always properly disable and dismount before removing the device or power off your system before removing the external swap space.