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
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“.
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.
Here enter the available partition number for that disk and Press Enter.
Now it’s asking for Cylinder, You can just press Enter to take the system default Cylinder.
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.
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“.
As we can see in help file “t” is for change the partition’s system ID, So let’s go for “t“.
Now we have to enter the Partition code for swap partition, for we can enter “L” to check the same.
As we can see on the Partition code List “82” is the code of Linux swap.
Now enter “w” to commit changes and 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
Also Read :
- HOW TO MOUNT NTFS FILE SYSTEM IN REDHAT/CENTOS
- COMPLETE UNIX COMMANDS AND BASIC LINUX COMMANDS WITH EXAMPLES FOR BEGINNERS
- HOW TO CREATE SYMLINK (SYMBOLIC LINK) AND HARDLINK IN LINUX
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
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.
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
We can check the swap status with below command.
# swapon -s
“swapon -s” shows swap usage summary by device, Equivalent to “cat /proc/swaps”
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.