Swap in linux with examples

Hello, everyone In this Article we are going to learn what is the Swap Partition in Linux in a very simple and easy step-by-step approach.

swap in linux

Swap in linux

The swap Partition in Linux is also known as the virtual memory in Linux server

Suppose that we have one server of RAM 4GB. And sometimes in a week server load is increased and in that time our server is not capable to handle the request because of low RAM size to overcome this issue either we have to increase the physical RAM size or we have to make the Swap Partition.

So Swap Partition is used in the accidental case where the server load is increased in the swap Partition the physical RAM is combined with the Swap Partition and if the server load will be increased then also our server will able to handle the requests.

It is recommended that Swap Partition must not be more then 64GB.

How to check RAM and swap space

To check the RAM Size on a server and to check the swap Partition on Linux we can use #free command with ( -h )option.

# free -h
Screenshot 28 1

#lsblk

With the help of #lsblk command, we can see our hard disk attached to the server.

We are now making the swap partition on the /dev/xvdi hard disk this hard disk size is 5GB.

Screenshot 1 11

#fdisk /dev/xvdi

With the the help of #fdisk command we can go inside the hard disk

We have to make the primary partition now we are making the swap of size 3GB soo we are making the 3GB of the primary partition.

Screenshot 2 10

Now as we can see that the normal partition hex code is 82 and for the swap partition the swap code is 83 we need to change the hex code to 82.

Press option ( t ) too assign the hex code and then ( w )to save and quit

Screenshot 4 11

# partprobe /dev/xvdi – To give update to the kernel

Screenshot 5 11

How to assign file system

With the help of ( mkswap ) command, we can assign the file system to the hard disk.

 # mkswap /dev/xvdi
Screenshot 6 10

How to activate swap partition

With the help of ( swapon ) command we can activate the swap partition .

# swapon /dev/xvdi
Screenshot 7 8

With the help of swaopn command with ( -s ) option, we can see the size of the swap partition.

# swapon -s
Screenshot 8 8

But after reboot our swap will be lost in order to make the swap partition as permeant we have to add the entry in /etc/fstab file.

Screenshot 9 8

Now run #mount -a command.

Screenshot 10 7

How to deactivate the Swap partition

With the help of swapoff command we can deactivate the swap partition

Screenshot 11 8

Also delete the entry of swap partition from /etc/fstab file

Also delete the partition that we created.

# partprobe /dev/xvdi – Run this command to update the kernel.

Sometimes in real life we cannot able to make the partitions soo in that scenario we need to assign the file system to the blank file.

Screenshot 12 8

In this command we are making the blank file name /swapfile of size 1Gb

#mkswap /swapfile

With the help of mkswap command we can assign the filesystem on the /swapfile.

Screenshot 13 6

we can also do an entry on /etc/fstab file for the swap partition in order to make that swap partition persistent.

Conclusion

These are the Complete Knowledge on Swap partition on Linux

If you want to learn Linux in the most simple language then please click on this link.

This is the complete knowledge of how we can create and manage the swap partition in the Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment