Swap is an essential part of Linux, and how it manages programs. With it, Linux users can suspend to RAM, and even allocate extra memory for programs to use when physical RAM runs out. Suffice it to say, everyone should use swap. On traditional Linux distributions, a dedicated partition is created specifically to use swap. This is how things have gone on for a very long time. In 2017, however, swap files have come into fashion. For example: recently Ubuntu has ditched swap partitions for a swap file, and others are likely to do the same in the future. The reason? Convenience. It’s one less partition to set up during installation. The system can install itself faster as a result. Additionally, a swap file can be increased without re-partitioning an entire hard drive, and it can quickly be disabled without much of a fuss. Here’s how you can setup a SWAP file on Linux.
Note: the swap file can be set up on any Linux distribution. Please install your Linux distribution without a swap partition first! Keep in mind that this means not installing with a traditional, easy installer.
Often times, to get a “swap-less” installation, a “custom installation” is required. This means partitioning by hand, including: setting up partitions, and mount points and (if on UEFI) the boot partition.
For example: on the Ubuntu installer, to get a custom installation, you’d check the “something else” box. On other Linux distributions, this might be “custom layout” or something similar.
If you’re using the BtrFS file system , don’t follow this guide. BtrFS has absolutely no support for swap files.
Setting up swap
Making a swap file involves creating a file with DD. We’ll need to make a file with the DD tool. Keep in mind that the swap file should be identical to the size of your physical ram. Enter this string of commands exactly as it appears. The user must enter everything in this long sentence at once, and run it as if it is one command.
If done successfully, this will ask you to enter a number (in GBs) and convert it to megabytes. No math required.
View in gallery
Note: If you have more than 4GBs of ram, make your swap file half of the ram instead.
echo 'enter gigabytes:';read gigabytes;calculated=$((1024 * $gigabytes))
The swap file calculation is ready. The terminal saved the calculation inside of $calculated. Now let’s take this and plug it into DD. Keep in mind that the swap file creation process takes a bit of time, and there will be no loading screen. When the terminal allows, enter the next command.
View in gallery
sudo dd if=/dev/zero of=/swapfile bs=1M count=$calculated
Next, change the permissions for the swap file in /. This is needed, because the system has to have full access to the file. Without this, swap won’t work right.
gain a root shell with:
chmod 600 /swapfile
With the permissions corrected, it is time to use the Mkswap command. Effectively, this is a tool that can take a partition or file and turn it into swap space.
The Permissions are now set, and everything is ready to go. The next step in the process is to tell Linux to turn on swap. This is done with the swapon command.
As the user enters the 4 commands above, a swap file should be up and running. However, that isn’t enough, as the system needs to know at boot what swap is, and where it’s located. This is done by editing the /etc/fstab file (file system tab).
View in gallery
Start by adding a comment to the end of the file. This is to label the file system tab, so that future editing is easier (because the user can easily see the comment label and know what is what).
echo "# Swap file" >> /etc/fstab
Next, add a line under the comment that specifies where the swap file is on the system, how the system should treat it and etc.
echo "/swapfile none swap defaults 0 0" >> /etc/fstab
With these two edits done, reboot the system. From now on when this Linux machine loads, it’ll have a swap file to use.
Disabling the swap file
Unlike a swap partition, users can easily disable the file, and free up previously used space very easily. There is no need to repartition anything. Instead, disabling swap permanently is a quick 3 step process.
Start off by opening a terminal, and entering the following command:
View in gallery sudo nano /etc/fstab
Next, look for anything pertaining to swap (like the comment letting the user know it’s a swap entry) and delete everything. Then, save the edits to the file by pressing
Ctrl + O.
With the edits saved, turn off swap.
sudo swapoff -a
Enter the root filesystem directory.
Delete the swap file itself.
sudo rm swapfile
Restart the computer. Upon restart, the swap file is totally gone, and disabled.
Swap is useful, because it gives extra overhead for programs when they run out of memory. Though it’s not mentioned enough, swap files have some real benefits. With it, swap can easily be made and turned off without repartitioning. Additionally, the size can of it can be changed on the fly, as needed. Hopefully, with this tutorial Linux users who don’t know the benefits that come with using a swap file rather than a dedicated swap partition.
–>x3Cscript async src=”http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”>x3C/script>x3Cscript>x3C/script>’);