What is Swap?
On Linux, swap is a space on a hard drive or SSD that functions as an extension of a system’s physical memory (RAM). In case the RAM memory is fully occupied, the Linux operating system has the ability to transfer less frequently used data to the swap space. This smart approach ensures that the most essential and frequently accessed data remains within the RAM, optimizing system performance. This process helps in preventing system crashes due to memory exhaustion, but accessing data from swap is slower than accessing it from RAM. Therefore, while swap can provide a safety net for systems with limited RAM, relying on it excessively can degrade system performance.
In this tutorial, I am using Ubuntu 22.04. What I will do is turn off existing swap and apply new swap with extra space.
Show existing swap size:
sudo swapon --show
Output:
rahil@localhost:$ sudo swapon --show
[sudo] password for rahil:
NAME TYPE SIZE USED PRIO
/dev/sdb partition 512M 0B -2
As you can see in the above output, the size of swap is 512M. This should be fine if your server is lightly used or has higher memory available. In my VPS server, I only have 2Â GB of RAM and I would like to add more swap space so that I can use swap as my server load increases. I plan to run multiple WordPress websites and this will be beneficial to have extra swap space.
Next, we will implement a new swap with 2 GB of space.
Step 1: Remove the existing swap from the server:
sudo rm /swapfile
Step 2: Create a new Swap file with a new size per your desire:
sudo fallocate -l 2G /swapfile
If the `fallocate` command is not available on your Linux system, you can try the dd command:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Step 3: Set permissions:
sudo chmod 600 /swapfile
Step 4: Format the new Swap file you created:
sudo mkswap /swapfile
Output:
rahil@localhost:$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=79ca27fb-28bf-4aa5-a887-dd310ee73703
Step 5: Enable new Swap:
sudo swapon /swapfile
Step 6: Confirm swap creation by executing the below command:
ls -hl /swapfile
Output:
rahil@localhost:$ ls -hl /swapfile
-rw------- 1 root root 2.0G Sep 22 18:52 /swapfile
Step 7: Add entry in fstab
file to keep the swap when the system reboots:
Note: If you see old swap in fstab, please go ahead and delete or comment it out so that old swap settings are disabled.
sudo vi /etc/fstab
In the last line add below:
/swapfile swap swap defaults 0 0
Your file should look like this or similar:
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/sda / ext4 errors=remount-ro 0 1
#/dev/sdb none swap sw 0 0
/swapfile swap swap defaults 0 0
~
Step 8: Now the last step is to reboot the server to apply and active swap on restart:
sudo reboot
This is it! You are done.