Linux System Administration

0 of 85 lessons complete (0%)

Managing Unix File Systems

Partition using fdisk

You don’t have access to this lesson

Please register or sign in to access the course content.

Partitioning in Linux is essential for managing disk storage. The fdisk and parted commands are commonly used for this purpose. Below are steps to partition a disk using each command, along with sample inputs and expected outputs.

1. Using fdisk

fdisk is a popular tool for creating and managing partitions on a disk.

Steps:

  1. Open fdisk with the target disk:
sudo fdisk /dev/sdX

Replace /dev/sdX with your target disk (e.g., /dev/sdb).

Sample Output:

Command (m for help):

2. View the current partition table (optional):

  • Enter p to print the partition table
p

Sample Output:

Disk /dev/sdb: 10 GB, 10000000000 bytes
255 heads, 63 sectors/track, 1216 cylinders, total 19531250 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot      Start         End      Blocks   Id  System

Create a New Partition:

  • Enter n to create a new partition.
  • Choose p for primary or e for extended.
  • Enter the partition number (e.g., 1).
  • Specify the first sector (press Enter to accept the default).
  • Specify the last sector (or specify size, e.g., +5G for 5 GB).
n
p
1
[press Enter]
+5G

Sample Output:

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-19531250, default 2048): [press Enter]
Last sector, +sectors or +size{K,M,G,T,P} (2048-19531250, default 19531250): +5G
Created a new partition 1 of type 'Linux' and of size 5 GiB.

Write the Partition Table:

  • Enter w to write the partition changes to disk.
w

Sample Output:

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

5. Format the New Partition: After partitioning, you should format it with a file system (e.g., ext4).

sudo mkfs.ext4 /dev/sdX1

Sample Output:

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: a8c9c2d4-8e9c-432b-8ae4-d6e94cbd1e59
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

6. Mount the New Partition:

sudo mkdir /mnt/new_partition
sudo mount /dev/sdX1 /mnt/new_partition

Sample Output:

(no output if successful)