How to Create a Swap Partition in Linux

In this article, we are going to learn how to create a swap partition using the command fdisk, Before that let me explain to you what is the use of swap partition in Linux. SWAP Partition is also known as SWAP memory, It acts as a Virtual Memory in Linux. When the physical memory of the system is full then Swap memory helps to the system with a small amount of memory but we can’t consider it as a replacement of more memory.

How to Create a Swap Partition in Linux

Follow the steps to Create a Swap Partition in Linux:

Step: 1 Create a Partition

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

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

Create a partition by the 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 partition 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 Cylinder 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 the Partition ID for SWAP Space

So we create the partition successfully but our goal is to create an 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 the 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 the 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 creating 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

Also Read – How To Mount NTFS File System In Redhat/Centos

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 a Swap Partition with “mkswap”

L – To Label your Partition for Identification, This can be left as an 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 the 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 the 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

That’s all, In this article, we have explained How to Create a Swap Partition in Linux. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version