introduction
Swap is a space on a disk drive that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, it moves the inactive pages from the RAM to the swap space.
Swap space can take the form of either a dedicated swap partition, a swap file, or a combinaton of partitions and files. Generally, when running Ubuntu on a virtual machine, a swap partition is not present, and the only option is to create a swap file.
This article will guide you through the steps of adding a swap file to servers
Before You Begin
Swap is not a replacement for physical memory. Since swap space is a section of the drive, it has a slower access time than physical memory. If your system constantly runs out of memory, you should add more RAM.
In most cases, the size of the swap file depends on the physical RAM the Linux system has:
Systems with less than 2 GB RAM – 2 times the amount of RAM.
Systems with 2 to 8 GB RAM – the same size as the amount of RAM.
Systems with more than 8 GB RAM – at least 4 GB of Swap.
Only root or users with sudo privileges can activate the swap file.
Check If Swap Is Already Enabled
Before you begin, it’s always a good idea to check if you already have a swap on your system. You can do that by using the following command:
sudo swapon --show
If no output is shown, it means swap is not configured. You can also check using:
free -h
Example output:
total used free shared buff/cache available
Mem: 4.0G 800M 2.9Gi 0.0 300M 3.2G
Swap: 0 0 0
Since `Swap` is `0B`, we need to add swap space.
Create a Swap File
In this guide, we will create a 4 GB swap file. If you need to add more or less swap, replace 4G with the required swap space size.
Before creating the file, ensure you have enough free disk space to complete this process successfully. You can get a detailed report on the system’s disk space usage with the df command :
df -h
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 10G 38G 20% /
Since there is enough space(38G), proceed with creating a 4GB swap file:
sudo fallocate -l 4G /swapfile
for Ubuntu based system use:
sudo fallocate -l 4G /swap.img
If the fallocate utility is not present on your system, or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
In this `dd` command:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096bs=1M
Sets the block size to 1 megabyte (MB) (1M = 1024 * 1024 = 1,048,576 bytes).
Each read/write operation will process 1 MB at a time.
count=4096
Specifies the number of blocks to write.
It tells `dd` to copy 4,096 blocks of 1 MB each.Total File Size Calculation: Total size = [bs] x [count]
= [1M] x [4096]
= 4096 MB = 4GB
So, this command creates a 4 GB swap file (`/swapfile`) filled with zeros.
This is a more efficient way than using `bs=1024 count=4194304` because larger block sizes reduce the number of operations, making the process faster.
Set File Permissions
Once the file is created, set the file permissions to 600 to prevent regular users to write and read the file:
sudo chmod 600 /swapfile
Convert the File into Swap Space
Format the file as swap space:
sudo mkswap /swapfile
Output:
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=xxxxxxx
Enable the Swap File
Activate the swap file by running the following command:
sudo swapon /swapfile
Verify that the swap is active by using either the swapon or the free command, as shown below:
sudo swapon --show
free -h
Output:
total used free shared buff/cache available
Mem: 4.0G 800M 2.9G 0.0 300M 3.2G
Swap: 4.0G 0 4.0G
Make Swap Permanent
Make the change permanent by opening the /etc/fstab file:
sudo nano /etc/fstab
and pasting the following line:
/swapfile swap swap defaults 0 0
Adjust Swappiness Value
The swappiness parameter controls how aggressively the system uses swap (0-100). A lower value minimizes swap usage. Check the current value:
cat /proc/sys/vm/swappiness
Output:
30
default swappiness value on CentOS is : 30
default swappiness value on Ubuntu and Debian is : 60
For example, to set the swappiness value to 10, type:
sudo sysctl vm.swappiness=10
To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
vm.swappiness=10
or
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Removing a Swap File (If Needed)
To deactivate and remove the swap file, perform the steps below:
1.First deactivate the swap space by typing:
sudo swapoff -v /swapfile
2.Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
3.Finally, delete the actual swapfile file:
sudo rm /swapfile
Conclusion
You’ve successfully added swap space to your Server. 🚀
This improves system performance, especially for low-RAM systems. If swap is used frequently, consider adding more RAM.
If you have a problem or feedback, leave a comment below.