How To Create Partition In Linux Step By Step Centos-Redhat-RHEL

How To Create Partition In Linux Step By Step Centos/Redhat/Rhel

In the following steps i will show you how to create partitions in centos or rhel with fdisk command. Before creating partitions in Linux/centos/redhat/rhel, First know your hard drives information with lsblk command. lsblk command will show you all your hard drives details like their partitions size, type and their mount points.

[root@localhost ~]# lsblk
NAME          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda             8:0    0 122.9G  0 disk 
├─sda1          8:1    0     1G  0 part /boot
└─sda2          8:2    0 121.9G  0 part 
  ├─rhel-root 253:0    0    50G  0 lvm  /
  ├─rhel-swap 253:1    0   2.5G  0 lvm  [SWAP]
  └─rhel-home 253:2    0  69.4G  0 lvm  /home
sdb             8:16   0    10G  0 disk 
└─sdb1          8:17   0     1G  0 part 
  └─myvg-mylv 253:3    0   512M  0 lvm  /project
sdc             8:32   0     5G  0 disk 
sr0            11:0    1  56.8M  0 rom  /run/media/admin/VBOXADDITIONS_5.1.24_117012

Here you can see sdc hard drive doesn’t having any partitions and the size of sdc is 5G.

or you can use cat /proc/partitions command it will also display your hard drive details.

[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  128888448 sda
   8        1    1048576 sda1
   8        2  127838208 sda2
  11        0      58100 sr0
   8       16   10496240 sdb
   8       17    1048576 sdb1
   8       32    5242880 sdc
 253        0   52428800 dm-0
 253        1    2621440 dm-1
 253        2   72781824 dm-2
 253        3     524288 dm-3

Create Partition With Fdisk Command:

To create partitions in your hard drive we use fdisk command.

syntax is fdisk   <hard-disk> and hit enter

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x2743f12f.

after hitting enter it will ask you  commands to create partitions.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +1G
Partition 1 of type Linux and of size 1 GiB is set

To create a new partitions we use the command ‘n’. So type n and hit enter. In the next step It will ask you partitions type like primary or extended, enter p or dont enter anything, just hit enter by default it will take primary. in the next step it  will ask you partition number, enter 1 or dont enter anything, just hit enter it will take 1 as by default. In the next step it will ask you first sector value, just hit enter or dont enter any values, by default it will take 2048.

in the next step it will ask you last sector size . Here we will mention size of our partitions. in my case i have 5 gb hard drive and making my first partition, so i will create 1 gb partition for that i need to enter +1G.

the syntax is      +numberG

if you want 2gb partition just enter +2G and hit enter.

Command (m for help): p

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x2743f12f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     2099199     1048576   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Now we have created partition. To verify is our partitions created or not by typing p and hit enter. It will show you all partitions in your hard drive. Here my hard drive is /dev/sdc. Just now we have created one new partition,  you can see new partition /dev/sdc1 . We have created partition but it is not saved for that we need to enter w command. So type w and hit enter. Now we have successfully created partition /dev/sdc1 in hard-drive /dev/sdc.

Partprobe:

part probe command informs the OS kernel to re read the partitions in hard drive. Then our newly created partitions will become active.

partprobe <hard-drive>

partprobe /dev/sdc

Format partition: Making a filesystem

Now we have to format our partition. To format partition we use mkfs command

syntax is    mkfs.<file-system-type>   <partition>

mkfs.ext4 /dev/sdc1
[root@localhost ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

We have formatted our newly created partition with ext4 file system.

Create Mount Point:

Now we have to mount our partition on any directory. For that i am creating one directory called decodingdevops.

mkdir /decodingdevops

you can create any directory.

Get UUID of Partition:

To mount our partition we have to edit /etc/fstab file. To edit that file first we need uuid of our newly created partition. To find uuid of any partition we use blkid command. ot will give you uuid of your partition and file system type.

blkid   /dev/sdc1

[root@localhost ~]# blkid /dev/sdc1 
/dev/sdc1: UUID="55706b3f-560d-4d6c-a7e8-e61b6c8e9cd8" TYPE="xfs"

Add uuid Of Partition In /etc/fstab File:

Now we got our uuid of newly created partition.

UUID=55706b3f-560d-4d6c-a7e8-e61b6c8e9cd8  /decodingdevops  xfs  defaults  0 0

syntax is  uuid=<uuid-number>   <mount directory>   <fileystme-type>   defaults   0 0

vi /etc/fstab

copy above line and add at the end of /etc/fstab file

[root@localhost ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Fri Sep 14 00:57:34 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=18d7ea5c-15fe-473a-b335-18fae1330d2e /boot                   xfs     defaults        0 0
/dev/mapper/rhel-home   /home                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
UUID=90ea5c44-f2f9-4087-a5cc-c868050061b1 /project                 xfs      defaults        0 0
UUID=55706b3f-560d-4d6c-a7e8-e61b6c8e9cd8   /decodingdevops   xfs   defaults  0 0

last line you can  see i added uuid of of our newly partition /dev/sdc1

Mount The Partition:

now use mount -a command to mount.

[root@localhost ~]# mount -a

mount -a command mounts all file systems from fstab file. Now verify the newly created partition with lsblk command.

[root@localhost ~]# lsblk
NAME          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda             8:0    0 122.9G  0 disk 
├─sda1          8:1    0     1G  0 part /boot
└─sda2          8:2    0 121.9G  0 part 
  ├─rhel-root 253:0    0    50G  0 lvm  /
  ├─rhel-swap 253:1    0   2.5G  0 lvm  [SWAP]
  └─rhel-home 253:2    0  69.4G  0 lvm  /home
sdb             8:16   0    10G  0 disk 
└─sdb1          8:17   0     1G  0 part 
  └─myvg-mylv 253:3    0   512M  0 lvm  /project
sdc             8:32   0     5G  0 disk 
└─sdc1          8:33   0     1G  0 part /decodingdevops
sr0            11:0    1  56.8M  0 rom  /run/media/admin/VBOXADDITIONS_5.1.24_117012

You can see sdc1 is having 1 Gb space and mounted on /decodingdevops directory.

 

 

  • fdisk create partition,
  • fdisk linux
  • how to create partition in rhel 7
  • centos 7 create partition
  • redhat partition disk command line
  • centos create partition fdisk
  • how to create partition in centos 7

3 Responses

  1. Gregorio says:

    Excelent article. complete and clear.
    Just a comment, following the article, in the /etc/fstab file there should be ext4 instead of xfs?
    Thank you so much too.

    • Gregorio says:

      …. shouldn’t be ext4 instead of xfs?

  2. jagadeesh says:

    I did not see this type of article great explanation Thank you so much

Leave a Reply

Your email address will not be published. Required fields are marked *