How to create simple disk partition on Linux

In this guide, we’ll walk you through the step-by-step process of partitioning a disk. These steps are compatible with a variety of popular Linux distributions, including Centos, Rocky Linux, AlmaLinux, and Red Hat Linux. So whether you’re a seasoned Linux user or just getting started, you can follow along with ease.

  1. Identify the disk you want to partition using the following command:
    # fdisk -l 

  2. Here's an example output of the fdisk -l command:

    
    Disk /dev/sdb: 20 GiB, 10737418240 bytes, 20971520 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
    Disklabel type: dos
    Disk identifier: 0x00000000
    
    Device   Boot    Start      End          Sectors     Size   Id   Type
    /dev/sdb1         2048     20971519   20969472  10G   83   Linux
    

  3. Once you have identified the disk, use the following command to start the partitioning process. In this tutorial, we shall use /dev/sdb as shown in the example above.
    # fdisk /dev/sdb


    Replace /dev/sdb with the disk name you want to partition.

  4. In the fdisk menu, use the following commands to create a new partition:
  5. 
        Type n to create a new partition.
        Choose the partition type by typing p for primary partition, e for extended partition, or l for logical partition.
        Enter the starting sector for the partition. If you want the partition to start at the beginning of the disk, press Enter.
        Enter the ending sector for the partition. If you want the partition to take up the rest of the disk, press Enter.
        Once you have created the partition, type w to write the changes to the disk and exit fdisk.
    	


    Here's an example of what you will see on the screen:

    
        Command (m for help): n
        Partition type
        p   primary (0 primary, 0 extended, 4 free)
        e   extended (container for logical partitions)
        Select (default p): p
        Partition number (1-4, default 2):
        First sector (2048-20971519, default 2048):
        Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +2G
        Created a new partition 2 of type 'Linux' and of size 2 GiB.
       
        Command (m for help): w
    

  6. After exiting fdisk, run the partprobe command to inform the system of the new partition:
    # partprobe /dev/sdb 

  7. Create a file system on the new partition using the following command:
    # mkfs.ext4 /dev/sdb2

  8. This will create an ext4 file system on the newly created partition. If you want other file systems like xfs, simply replace ext4 with xfs.

  9. Create a mount point for the new partition. Let's say you want to mount the new partition at /mnt/data, so you can create the directory using the following command:
    # mkdir /mnt/data

  10. Mount the new partition at the mount point you just created:
    # mount /dev/sdb2 /mnt/data

  11. This will mount the new partition at /mnt/data.

  12. Verify that the partition is mounted correctly using the df command:
    # df -h

  13. Here's an example output:

    
    Filesystem      Size    Used    Avail    Use%    Mounted on
    /dev/sda1        20G    4.1G    15G      22%       /
    devtmpfs         3.9G     0       3.9G     0%         /dev
    tmpfs              3.9G     0       3.9G     0%         /dev/shm
    tmpfs              3.9G  9.0M     3.9G     1%         /run
    tmpfs              3.9G     0       3.9G     0%          /sys/fs/cgroup
    /dev/sdb2        2.0G   45M     1.8G     3%         /mnt/data
    tmpfs             783M     0       783M     0%         /run/user/0
    

  14. If you want to automatically mount the new partition every time the system starts up, you can add an entry to the /etc/fstab file. First, obtain the UUID of the new partition using the blkid command:
    # blkid /dev/sdb2

  15. Here's an example output:

    
    /dev/sdb2: UUID="1d54360d-82e8-4f69-b4db-9e49208b4c8f" TYPE="ext4" PARTUUID="00000000-0000-0000-0000-000000000000"
    


    Copy the UUID value (in this case, 1d54360d-82e8-4f69-b4db-9e49208b4c8f).

  16. Open the /etc/fstab file in a text editor:
    # vi /etc/fstab

  17. Add the following line at the end of the file to automatically mount the new partition at /mnt/data on system startup:
  18. UUID=1d54360d-82e8-4f69-b4db-9e49208b4c8f /mnt/data ext4 defaults 0 0


    Replace the UUID value with the one you obtained earlier.

  19. Save and close the /etc/fstab file.
  20. To test that the entry you added to the /etc/fstab file is correct, unmount then remount the partition using the following commands:
  21. 
    # umount /mnt/data
    # mount /mnt/data
    

  22. Verify that the partition is mounted correctly by running the df command again:
  23. df -h


    The output should show the new partition mounted at /mnt/data.

0 Comments

Leave a reply

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

*

©2023 ZMATECH: Innovative solutions for your IT challenges.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

Log in with your credentials

Forgot your details?