Ubuntu 22.04

How To Add Swap Space On Ubuntu 22.04 LTS

How To Add Swap Space On Ubuntu 22.04
Written by itsubuntu

How To Add Swap Space On Ubuntu 22.04

Swap space is the part of the HDD or SDD that is designed to help the operating system temporarily store data. The data or information that the OS cannot hold in RAM is stored in Swap space. It is useful when there is more load on RAM.

In this tutorial, we will see the steps to add swap space on Ubuntu 22.04 LTS.

How To Add Swap Space On Ubuntu 22.04

You can check if the Ubuntu already has some swap space available with the following command.

sudo swapon --show

You should get an output if you have swap space available other you won’t get any output. Now, you can create the swap space from the available HDD space or SDD space. Run the following command to see if there is any space available or not.

df -h

If you have 2 GB RAM then you can create 2 GB swap space accordingly. Run the following command to create swap file at first. You need to convert the swap file to swap space later.

sudo fallocate -l 2G /swapfile

You can verify that the correct amount of space was reserved by typing:

ls -lh /swapfile
-rw-r--r-- 1 root root1.0G Apr 25 11:14 /swapfile

Now, after creating the swap file, you need to turn this into swap space. Meanwhile, you need to change the file permission to the root level to prevent further modification from the users.

sudo chmod 600 /swapfile

 

Verify the permissions change by typing:

ls -lh /swapfile
Output
-rw------- 1 root root 2.0G May 01 05:15 /swapfile

As you can see, only the root user has the read and write flags enabled.

Now run the following command to change the file as swap space by typing:

Sudo mkswap /swapfile

Now, run the following command to inform your system and let your system use it as a swap space.

sudo swapon /swapfile

You can verify that the swap is available by running the following command:

sudo swapon --show

About the author

itsubuntu

Leave a Comment