Use mkswap command to Create Swap Partition in Linux Guide 2021

mkswap-command-to-create-swap-partition-in-Linux-step-by-step-guide

Have you heard about RAM – Random Access Memory? Ram is a type of memory called volatile memory, which is used to store data and programs while they are being actively used by the computer.

RAM plays an important role to give a better performance of your computer. If you want better perforation of your system then increase RAM size.

Every program running in the system utilizes CPU, and RAM as well.

I will cover in this article How to create swap partition in Linux. How to use mkswap swap command in Linux to create swap partition step by step guide. Other commands are also used to help create swap partition including mkswap, swapon, swapoff, lsblk etc.

What is swap Partition in Linux?

When the program/application has requirements of more size than RAM than swap partition is used by the same application.

For Example, you are running an HTTP server and working fine on 64 GB of RAM, but sometimes it gets more load and wants to use 84 GB RAM than 20GB will be used from SWAP.

When RAM capacity has been utilized to maximum by an application, the Linux machine moves inactive pages from RAM memory to swap-memory.

But keep in mind, it should not be considered as a replacement for physical memory/RAM.

What is the right swap partition size?

I was taught in 2008, swap partition size should 2X the amount of RAM installed on your computer. For Example, If your Linux system has 2GB RAM, then you should assign a swap partition size 4GB.

This is the standard rule followed by every administrator, With more than twice RAM for swap, most systems spent more time thrashing than actually performing useful work.

In most cases, it is advised to you keep SWAP partition size equal to the size of RAM (physical memory) at minimum & double (2X) the size of physical memory at maximum.

So if you have 8 GB RAM on your system, Swap should be between 8-16 GB. And there is no problem at all.

If you are dealing with computers having huge amounts of RAM more than 64 GB, the limiting performance factor for swap space is far lower than the 2X multiplier.

You can get the information related recommended swap partition size from the following table. It is depending on the amount of RAM in your system.

Do you want sufficient memory for your system to hibernate? The recommended swap partition size is established automatically during installation.

The following table is based on Recommendations by Red Hat Enterprises.

Amount of RAM in the systemRecommended swap spaceRecommended swap space if allowing for hibernation
⩽ 2 GB2 times the amount of RAM3 times the amount of RAM
> 2 GB – 8 GBEqual to the amount of RAM2 times the amount of RAM
> 8 GB – 64 GBAt least 4 GB1.5 times the amount of RAM
> 64 GBAt least 4 GBHibernation not recommended
Recommended System Swap partition size by Red Hat

Create swap partition CentOS 8

I have CentOs installed on Virtualbox, You can learn How to install CentOS on Virtualbox. Later I have added two hard disks to the CentOS.

I have created 2 partitions on second drive by using fdisk command in Linux.

Now My system is ready to create a swap partition.

lsblk command in Linux to display the available hard disk and partition.

[[email protected] ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   32G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   31G  0 part 
  ├─cl-root 253:0    0 28.9G  0 lvm  /
  └─cl-swap 253:1    0  2.1G  0 lvm  [SWAP]
sdb           8:16   0    8G  0 disk 
├─sdb1        8:17   0    2G  0 part 
└─sdb2        8:18   0    6G  0 part 
sdc           8:32   0   16G  0 disk 
├─sdc1        8:33   0    8G  0 part 
└─sdc2        8:34   0    8G  0 part 
sr0          11:0    1 56.9M  0 rom  /run/media/vijay/VBox_GAs_6.1.4
[[email protected] ~]#  

mkswap command in Linux to create swap partition

First you will have need to create a basic partition like other partitions. There is no difference to create a swap partition and a normal partition. You can convert a normal partition to swap space.

Create a primary partition as per requirement like 2GB, 4GB or 8 GB. Don’t format it.

Next task make swap it using mkswap command syntax as below.

#swapon -s

[[email protected] ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	2170876	202496	-2
[[email protected] ~]# 

mkswap /dev/sdb1

[[email protected] ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=874c144f-8496-41c4-b441-5190cdec03b4
[[email protected] ~]# 

Now partition has been formated by this command.

Activate the swap by swapon command

It’s time to activated swap, use the following syntax to complete this action.

[[email protected] ~]# swapon /dev/sdb1 
[[email protected] ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-1                              	partition	2170876	258388	-2
/dev/sdb1                              	partition	2097148	0	-3
[[email protected] ~]# 

Mount swap partition

You swap partition is ready, but it will not be mounted automatically after reboot your system.

So you need to make entry in file /etc/fstab to mount it permanently on your system.

You need to modify /etc/fstab file. But you must know how and what do you need to make entry in this file.

Open the file /etc/fstab and make the following entry in the end of file.

/dev/sdb1 swap swap default 0 0

Save & close the file. You swap partition now will even work after a reboot.

Conclusion

I’ve tried my best to cover most of the basic uses of mkswap command in Linux to create a swap partition.

For more detailed information, you can check the manual page. To display the manual page use man command from the terminal.

If I’ve missed any important command, please do share it with me via the comment section.

If you like our content, please consider buying us a coffee.
Thank you for your support!