Add Swap Space on Ubuntu 16.04

Add Swap Space on Ubuntu 16.04

Swap Space

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

How to check swap space.

$ sudo swapon --show

If you don’t have any swap space you will get blank output.
Check Ram and Swap space using below command.

$ free -m 

According to my this post I am going to make swap space of 1GB.

Step 1-: Create a swap file.

$ sudo fallocate -l 1G /swapfile

The above command make a swap file with 1GB space on your system disk.
To verify it, use the df -hl command like below.

du -hl /swapfile


1.1G      /swapfile

Step-2: Change the permission for root only.

$ sudo chown root:root /swapfile 
$ sudo chmod 600 /swapfile

Step-3: Mark the as swap space.

$ sudo mkswap /swapfile

Step 4-: Enable the swapfile allowing to use the swap file.

$ sudo swapon /swapfile

Now check the swap space would be like.
Use free -m command.

$ free -m


              total        used        free      shared  buff/cache   available
Mem:            992         493          67          29         431         275
Swap:          1023           0        1023

or Use the sudo swapon –show to check the swap space.

$ sudo swapon --show


/swapfile file 1024M 7.2M   -1

Additional Step-: To make the swap file permanent.
First of all we must create backup of existing fstab file.

$ sudo cp /etc/fstab /etc/fstab_org

Add the swap file into /etc/fstab file using the below command.

$ sudo echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

You will get output like below-:

/swapfile none swap sw 0 0
Gotechnies Linux Blog

Rating: 4.9
Votes: 480
Reviews: 560

Post a comment