In this article, we will talk about Swap files, and how to create swap space in Linux using a swap file.
But before we begin, we need to clarify what swap actually is. Swap is reserved disk space that is used as a place where the OS can temporarily store data when the physical RAM memory is full or close to full. In other words, swap is reserved virtual memory on the disk. Usually, swap is used to improve and maintain system performance when we are dealing with some resource-heavy tasks.
Whenever the system requires more memory to create new processes, the kernel will move some processes that are inactive to the swap partition so that it can make room for active processes.
Please note that swap is generally recommended for systems that are using traditional spinning hard drives. If your system is using swap with an SSD or NVMe drive, it may cause issues with hardware degradation over time.
Table of Contents
1. System Check for Swap Information
Before we create swap space, will check if any swap file or partition has already been set on the server. We are allowed to have more than one swap, but one swap should be more than enough in most cases.
With the following command we can check if there is a swap file or partition already configured on the server:
sudo swapon --show
If there is no output, it means that your server does not currently have swap set up.
To make sure that your server is not using swap you can use the ‘free’ command:
free -h
The output should be similar to this:
total used free shared buff/cache available Mem: 985M 228M 579M 5.5M 177M 618M Swap: 0B 0B 0B
We can see from the output that “Swap” row is zero which means that there is no active swap on the server. However, in our example, we will create a swap file that will be located on a partition that already exists.
2. Create Swap
Usually, the allocated space for swap is used as a separate partition. Before we create a swap file it is best to check the disk usage:
df -h
The output should be similar to this:
Filesystem Size Used Avail Use% Mounted on udev 463M 0 463M 0% /dev tmpfs 99M 3.7M 95M 4% /run /dev/vda1 19G 2.2G 16G 13% / tmpfs 493M 0 493M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 493M 0 493M 0% /sys/fs/cgroup tmpfs 99M 0 99M 0% /run/user/0
We can see from the output that on our server we have 16 GB available space (2.2 GB used) which is enough to create a swap file.
Now that we know that we have storage space available, we will create a file called swap_file that will be 2GB in size and will be located in the /mnt directory. By using the command below we will create the actual swap file:
sudo fallocate -l 2G /mnt/swap_file
We can also use the dd command in case we do not have fallocate installed on the server:
sudo dd if=/dev/zero of=/mnt/swap_file bs=1024 count=2097152
Where bs=1024 means to allocate 1024 bytes at a time for reading and writing count = 2048 means to allocate 1024 bytes 2048 times, creating a 2GB file.
When the swap file is created, we can check if the correct size of the space is being reserved:
ls -lh /mnt/swap_file
The output should be similar to this:
-rw-r--r-- 1 root root 2.0G Nov 24 10:32 /mnt/swap_file
From the output result, we can see that the swap file was created with 2GB of storage.
3. Enable Swap
Now that we have an available swap file, we need to use it and turn into actual swap space.
The first step will be to lock the permissions of swap_file so that it can only be read by users with root privileges. We can do this by typing:
sudo chmod 600 /mnt/swap_file
Now list the file so that we can verify its permissions.
ls -lh /mnt/swap_file
Output:
-rw------- 1 root root 2.0G Nov 24 10:33 /mnt/swap_file
From the output above, we can see that only the root user has the read and write flags enabled.
The next step is to mark the file as swap.
sudo mkswap /mnt/swap_file
Output:
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=c569dd4f-ffd9-4192-93dd-f6a470025d76
After we mark the file, we can activate the swap file and allow our server to start using it:
sudo swapon /mnt/swap_file
We can check if the swap file is available with the following command:
sudo swapon -s
Output:
Filename Type Size Used Priority /mnt/swap_file file 2097148 0 -2
We can again check the output of the ‘free’ command to confirm:
free -h
Output:
total used free shared buff/cache available Mem: 985M 228M 64M 5.5M 691M 608M Swap: 2.0G 0B 2.0G
From the above results, we can see that the swap was successfully set up.
4. Make the Swap File Permanent
With the changes above we created a swap file, but it is temporary and valid only until the next server boot. If we reboot the system, the server will not keep the swap settings that we created. To change this, we need to edit the /etc/fstab file and add the swap file in it.
It is a good idea to create a backup of the /etc/fstab file before making any changes.
sudo cp /etc/fstab /etc/fstab_bak
Now you can edit the /etc/fstab with your preferred text editor (in this case we will use nano)
sudo nano /etc/fstab
and add the following line to it:
/mnt/swap_file swap swap defaults 0 0
Save and close the file.
5. How to remove the Swap File
In the last part of this article, we will show you how to deactivate and remove the swap file.
We can deactivate the swap file by using this command:
sudo swapoff -v /mnt/swap_file
Output:
swapoff /mnt/swap_file
Then edit the /etc/fstab file and remove the swap file by removing or commenting the line:
/mnt/swap_file swap swap defaults 0 0
In the end, you can delete the actual swap file if desired:
sudo rm -rf /mnt/swap_file
Using these steps, we completely removed the swap file from our server.
Conclusion
In this article, we learned how to check if we have swap enabled on our server, how to create a swap file, how to enable swap, how to make the swap file permanent and how to deactivate and remove the swap file from our server.
Swap space can be very useful sometimes, but if your server is running into OOM (out of memory) errors, the best solution is to upgrade your server to a higher hosting plan.
Of course, if you are one of our NVMe hosting customers, we can do all of this for you. Simply contact our system administrators and they will respond to your request as soon as possible.
PS. If you liked this post about how to create a Linux SWAP file in Linux, please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.