How to create a swap partition in Redhat/CentOS/Fedora

How to create a swap partition in Redhat/CentOS/Fedora

How to create a swap partition in Redhat/CentOS/Fedora : In this artical we are going to learn how to create a swap partition using command fdisk, Before that let me explain you what is the use of swap partition in Linux.

SWAP Partition is also known as SWAP memory, It is act as a Virtual Memory in Linux. When the physical memory of the system is full then Swap memory helps to system with a small amount of memory but we can’t consider it as a replacement of more memory.

Follow the steps to add a additional SWAP Partition:

Step: 1 Create a Partition

First check the available free disks to create a new partition by below command.

#  fdisk -l    # To list the available free disks and created partitions.

Create a partition by below command.

#  fdisk /dev/sdb    # To Create a new partition

Create a New Partition using fdisk

Here you can enter “n” to create a new Partition or enter “m” to go for help, We are going to create a new partiton so enter “n“.

Select the Partition Type

Now we have two options i.e. Primary Partition by entering “p” or Extended Partition by entering “e“, So now we can select “p” and go for a Primary Partition.

Assign a Number to Partition

Here enter the available partition number for that disk and Press Enter.

Assign Cylinder to Partition

Now it’s asking for Cylinder, You can just press Enter to take the system default Cylinder.

Giving Size to Partition

 As we can see System took its default Cyliner value as 1, now its time to give the Partition Size.

Here we can enter the partition size in two ways in GB’s or in MB’s, for Example If you want to give the partition size as 1 GB then you can Enter +1G or +1024M.

So Let’s go with +1G and Press Enter.


We need to Change Partition ID for SWAP Space

So we create the partition successfully but our goal is to create a additional swap partition, for that we need to change the partition ID for that we have to enter “t” or for help we can go for “m“.

Partition Help Chart

As we can see in help file “t” is for change the partition’s system ID, So let’s go for “t“.

Change the Partition ID

Now we have to enter the Partition code for swap partition, for we can enter “L” to check the same.

Partition Code Chart

As we can see on the Partition code List “82” is the code of Linux swap.

Select Partition Code for Swap Partition

Now enter “w” to commit changes and save the partition table.

Save the Partition Table

In Linux based system after create a partition we suppose to restart the system to take effect but we can skip that by running “partprobe command”

#  partprobe /dev/sdb    # Changes take effect without restat the system

Step: 2 Create Swap Signature

Now Let’s create the swap Signature with “mkswap” command.

#  mkswap /dev/sdb1 -L newswap    # To make a swap partition

Create Swap Partition with “mkswap”

L – To Label your Partition for Identification, This can be leave as a optional but Highly recommended.

we can use “blkid” command to check the Labels given to File Systems.

Checking the Label of the Partition

Now enable the swapping and publish the swap space to the kernel by below command.

#  swapon /dev/sdb2    # Enable swapping on /dev/sdb2

OR we can use swapon -a to activate all swap partitions (Multiple swap Partitions) 

Step: 3 Mount the Swap Partition

Now It’s time to add the newly created swap partition on “/etc/fstab” file to start the partition at startup.

# vi /etc/fstab   # Edit the "fstab" file

Mounting SWAP Partition in fstab

We can check the swap status with below command.

#  swapon -s

swapon -s” shows swap usage summary by device, Equivalent to “cat /proc/swaps

Checking SWAP Partition Usage

As we can see on above snapshot we have two swap partitions, out of which highlighted one is recently created and currently it’s usage status is 0.

We have another command to check the status of the swap memory is “free -m

$  free -m    # Display amount of free and used memory in the system

So This is how we can create a swap partition for better performance in our Linux System.

Also Read :

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

Enjoyed this video?
How to create a swap partition in Redhat/CentOS/Fedora
"No Thanks. Please Close This Box!"