This tutorial shows how to set up a swap file on Ubuntu Linux. Normally, you would use a swap partition on Linux, not a swap file. But sometimes, you can not create a swap partition. In such a case, a swap file comes in handy. The steps have been tested in recent Ubuntu versions, incl. Ubuntu 22.04.
What Does Swapping Mean in Linux?
A swap file allows an operating system (OS) to use hard disk space to simulate additional memory. When the system goes into low-memory mode, a certain amount of memory occupied by an inactive program is swapped out to the hard disk to free up space for other important programs. When the user returns to the swapped-out program, it swaps space with another programme in RAM. This intelligent mix of working memory and swap files is commonly referred to as virtual memory. It allows the system to run a far greater number of programs than would be possible with built-in working memory.
In the context of Linux, the RAM on the Linux server consists of memory sections, technically called pages. To free up these RAM pages, a LINUX swap can be performed to copy the memory page from the parent RAM to a pre-configured area on the hard disk. Such Linux swaps allow a system to use more physical memory than was originally made available. Although it is a useful tool, swapping has some disadvantages. First, this activity triggers a considerable amount of disk reads and writes, inevitably slowing your system down considerably. Secondly, swap thrash can occur when the system is flooded with many files that are frequently swapped in and out.
Important: Preliminary Check before setting up a Swapfile
Before setting up a swap file, users need to check whether swap files have already been activated on the VPS. To do this, you can look at the swap usage summary. The following command will take you to the desired summary:
sudo swapon -s
The above command shall lead to the following results:
Filename Type Size Used Priority
If the above happens to be an empty list, you may rest assured that no swap files have been enabled.
Adding a Swap File on Ubuntu – A Step-By-Step Guide
Once the preliminary check has been done, the users are now all geared up for adding a Swap File on Ubuntu. All they need to do is follow the steps given below:
Step 1 – Checking the File System
Once it is determined that there are no active swap files on the virtual server, one must use the df command to check how much space is available on the server. Since the swap file normally occupies 256 MB of space, which is only 8% of the /dev/sda on a typical server with 32 GB capacity, it is safe to proceed with the process.
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda 20907056 1437188 18421292 8% / udev 121588 4 121584 1% /dev tmpfs 49752 208 49544 1% /run none 5120 0 5120 0% /run/lock none 124372 0 124372 0% /run/shm
The above step shows the free space on your hard disk. Ensure that there is enough free space for your swap file. Next, we will create the actual file with dd as explained below.
Step 2 – Creating the Swap File
The second step comprises creating the Swap File with a size of 256 MB by using the dd command follows:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k
of=/swapfile” designates the file’s name. In this case the name is swapfile.
Consequently, the swap file can be prepared by creating a designated Linux swap area using the following command:
sudo mkswap /swapfile
The above command shall display the following results:
Setting up swapspace version 1, size = 262140 KiB
no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb
Completion of Step 2 shall lead to the creation of the Swap File. The next step, as described below, shall lead to the activation of the Swap File thus created.
Step 3 – Enabling the Swap File
Creating the Swap File on Ubuntu needs to be finished by enabling the file so created. This can be done by using the following command:
sudo swapon /swapfile
Completion of Step 3 shall enable users to view the new swap file when under swap summary, as follows:
Filename Type Size Used Priority
/swapfile file 262140 0 -1
The file displayed as per details above shall exist on the virtual private server till the time the machine reboots. However, users enjoy the option of making the Swap File permanent by following the next step. 1.3.4: Step 4-Adding the Swap to the fstab file In order to lend permanence to the swap file you created, you must add the same to the fstab file. The following command that will help you open up the file remains the first in the process that shall help you do that:
sudo nano /etc/fstab
Subsequently, users need to paste in the following line:
/swapfile none swap sw 0 0
Post the above step, the Swappiness in the file must ideally be set to 10, by using the following set of commands:
echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
Important: Doing away with the above important sub-step may invariably lead to poor system performance, while configuring the setting to 10 shall lead to the swap acting as an emergency buffer, thus averting irksome out-of-memory crashes.
Step 4 – Setting Up Appropriate Permission Levels on the Swap File
The last and final step in creating a Swap File on Ubuntu remains setting up apt permission levels for the file. This is essential to prevent the file from becoming world-readable and lending appropriate security. The same may be executed by using the following either of the following commands (depending upon the case):
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
Finally, we test that the swap file is active. Run the top command
the swap space should be shown in the column
KiB Swap: 262140