Frequently Asked Questions
How to create a Swap File (applies to almost all distributions)
Last Updated 2 years ago
What is a Swap File?
A swap file is a file on your disk used for swapping, which is a technique used by Linux to allow for virtual memory by moving the most infrequently accessed memory onto the disk.
By default, many Linux distributions try to create a swap partition, which is a section of your disk reserved exclusively for swap data. However, this results in poor flexibility (resizing partitions is not fun), and rarely offers any benefit over a swap file.
Unlike swap partitions, swap files can easily be deleted if they're not needed, or if they need to be resized. Similarly, it's easy to create more than one swapfile if you need more swap space, without having to disable the first swapfile.
How do I create and use one?
First, you need to create an empty file somewhere on your disk, and decide how big it should be. In our example, we'll use an 8G (8 Gigabyte) file.
The below commands create an empty 8gb file at the location /swapfile, and then format it with swapfs, turning it into a virtual disk image, which can be used by Linux for swapping.
The permissions 600 will ensure that only root can read and write to it, while the file cannot be executed.
Finally, we enable it using swapon:
Now you have a swap file. But, it will not be enabled on reboot. Let's fix that
First, let's backup the current fstab to our home directory. If you make a mistake, then you can just copy it back.
Open /etc/fstab in your favourite editor. For beginners we recommend nano.
You should see something similar to this:
Create a new line at the bottom of the file, make sure you don't accidentally change any of the existing lines or your system may become un-bootable.
At the bottom of the file, add the following:
Your file should now look similar to this:
Save the file (press CTRL-X if using nano, and press "y" to save), and it should now be automatically loaded on reboot.
To verify you didn't damage the fstab file, you can use the following command:
The none entry is the swap file. It shows ignored as it does not have a mount point (because it's a swap file). This is completely normal. The root partition (/) may also show as ignored, this is usually normal.
If you see any errors, then you've made a mistake in your fstab. You should copy your backup fstab, and try again.
Otherwise, you're good to go :) You now have a working swap file, which automatically re-mounts on reboot.
A swap file is a file on your disk used for swapping, which is a technique used by Linux to allow for virtual memory by moving the most infrequently accessed memory onto the disk.
By default, many Linux distributions try to create a swap partition, which is a section of your disk reserved exclusively for swap data. However, this results in poor flexibility (resizing partitions is not fun), and rarely offers any benefit over a swap file.
Unlike swap partitions, swap files can easily be deleted if they're not needed, or if they need to be resized. Similarly, it's easy to create more than one swapfile if you need more swap space, without having to disable the first swapfile.
How do I create and use one?
First, you need to create an empty file somewhere on your disk, and decide how big it should be. In our example, we'll use an 8G (8 Gigabyte) file.
The below commands create an empty 8gb file at the location /swapfile, and then format it with swapfs, turning it into a virtual disk image, which can be used by Linux for swapping.
sudo fallocate -l 8G /swapfileNext we need to set the permissions to 600 as Linux will refuse to mount it unless it's secure (as it contains the contents of your system's memory).
sudo mkswap /swapfile
The permissions 600 will ensure that only root can read and write to it, while the file cannot be executed.
sudo chmod 600 /swapfile
Finally, we enable it using swapon:
sudo swapon /swapfileUsing free -m we can verify that it was enabled:
user@server1 $ free -m
total used free shared buff/cache available
Mem: 64207 891 442 43917 62874 18686
Swap: 8191 599 7592
Now you have a swap file. But, it will not be enabled on reboot. Let's fix that
First, let's backup the current fstab to our home directory. If you make a mistake, then you can just copy it back.
cp -v /etc/fstab ~/fstab
Open /etc/fstab in your favourite editor. For beginners we recommend nano.
sudo nano /etc/fstab
You should see something similar to this:
proc /proc proc defaults 0 0 /dev/md/0 /boot ext3 defaults 0 0 /dev/md/1 / ext4 defaults 0 0
Create a new line at the bottom of the file, make sure you don't accidentally change any of the existing lines or your system may become un-bootable.
At the bottom of the file, add the following:
/swapfile none swap sw 0 0
Your file should now look similar to this:
proc /proc proc defaults 0 0 /dev/md/0 /boot ext3 defaults 0 0 /dev/md/1 / ext4 defaults 0 0 /swapfile none swap sw 0 0
Save the file (press CTRL-X if using nano, and press "y" to save), and it should now be automatically loaded on reboot.
To verify you didn't damage the fstab file, you can use the following command:
mount -avYou should see something like this:
/proc : already mounted
/boot : already mounted
/ : ignored
none : ignored
The none entry is the swap file. It shows ignored as it does not have a mount point (because it's a swap file). This is completely normal. The root partition (/) may also show as ignored, this is usually normal.
If you see any errors, then you've made a mistake in your fstab. You should copy your backup fstab, and try again.
sudo cp -v ~/fstab /etc/fstab
Otherwise, you're good to go :) You now have a working swap file, which automatically re-mounts on reboot.