This tutorial provides a step-by-step guide to partitioning a disk using the `fdisk` utility in Linux. You’ll learn how to create, modify, and manage disk partitions, with practical examples and engaging exercises to reinforce your understanding.

Beginner30 minutes

Step 1: Step 1: Identify the Disk to Partition

Before partitioning, you need to identify the disk you want to work with. Use the lsblk or fdisk -l command to list all available disks and their partitions.

- lsblk: Lists block devices in a tree format.

- fdisk -l: Lists all disks and partitions with detailed information.

lsblk
# OR
fdisk -l

Practice Exercise

You have a system with multiple disks. Use lsblk to identify the disk labeled /dev/sdb. Write down its size and the number of existing partitions.

Show Solution
lsblk
# Output will show all disks. Look for `/dev/sdb` and note its size and partitions.
# Example output:
# NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
# sdb      8:16   0   100G  0 disk
# └─sdb1   8:17   0    50G  0 part

Step 2: Step 2: Launch fdisk for the Target Disk

Once you've identified the disk, launch fdisk in interactive mode by specifying the disk name (e.g., /dev/sdb).

- Command: sudo fdisk /dev/sdb

- This opens the fdisk prompt, where you can enter commands to manage partitions.

sudo fdisk /dev/sdb

Practice Exercise

Launch fdisk for /dev/sdc and list the available commands by typing m in the fdisk prompt. Write down the command to delete a partition.

Show Solution
sudo fdisk /dev/sdc
# Inside fdisk, type 'm' to list commands.
# Look for the command to delete a partition (usually 'd').

Step 3: Step 3: Create a New Partition

To create a new partition, use the n command in the fdisk prompt. You'll be prompted to specify:

- Partition type: Primary (p) or Extended (e).

- Partition number: Automatically assigned or manually specified.

- First and last sector: Define the partition size.

After creating the partition, write the changes to the disk using the w command.

Command (m for help): n
Partition type: p (primary)
Partition number: 1
First sector: (press Enter for default)
Last sector: +10G (to create a 10GB partition)
Command (m for help): w

Practice Exercise

Create a 20GB primary partition on /dev/sdb. Use the p command to print the partition table and verify the new partition.

Show Solution
sudo fdisk /dev/sdb
Command (m for help): n
Partition type: p
Partition number: 1
First sector: (press Enter)
Last sector: +20G
Command (m for help): p
# Verify the new partition in the output.
Command (m for help): w

Step 4: Step 4: Change Partition Type

If needed, you can change the partition type using the t command. This is useful for setting up swap partitions or other specialized filesystems.

- Command: t

- Hex code: Use L to list all available partition types and their corresponding hex codes.

Command (m for help): t
Partition number: 1
Hex code: 82 (for Linux swap)

Practice Exercise

Change the partition type of /dev/sdb1 to Linux swap (hex code 82). Verify the change using the p command.

Show Solution
sudo fdisk /dev/sdb
Command (m for help): t
Partition number: 1
Hex code: 82
Command (m for help): p
# Verify the partition type in the output.
Command (m for help): w

Step 5: Step 5: Delete a Partition

To delete a partition, use the d command in the fdisk prompt. You'll be prompted to select the partition number to delete.

- Command: d

- Partition number: Specify the partition to delete.

Command (m for help): d
Partition number: 1

Practice Exercise

Delete the second partition (/dev/sdb2) on /dev/sdb. Verify the deletion using the p command.

Show Solution
sudo fdisk /dev/sdb
Command (m for help): d
Partition number: 2
Command (m for help): p
# Verify the partition is no longer listed.
Command (m for help): w

Step 6: Step 6: Format the Partition

After creating a partition, format it with a filesystem using the mkfs command. For example, to format a partition as ext4:

- Command: sudo mkfs.ext4 /dev/sdb1

- Replace ext4 with the desired filesystem type (e.g., xfs, vfat).

sudo mkfs.ext4 /dev/sdb1

Practice Exercise

Format /dev/sdb1 as xfs. Verify the filesystem type using the blkid command.

Show Solution
sudo mkfs.xfs /dev/sdb1
blkid /dev/sdb1
# Output should show the filesystem type as xfs.

Step 7: Step 7: Mount the Partition

To use the partition, mount it to a directory using the mount command. For example:

- Command: sudo mount /dev/sdb1 /mnt/data

- Replace /mnt/data with your desired mount point.

sudo mount /dev/sdb1 /mnt/data

Practice Exercise

Mount /dev/sdb1 to /mnt/backup. Verify the mount using the df -h command.

Show Solution
sudo mount /dev/sdb1 /mnt/backup
df -h
# Output should show `/dev/sdb1` mounted at `/mnt/backup`.

Step 8: Step 8: Automate Mounting with /etc/fstab

To ensure the partition mounts automatically at boot, add an entry to /etc/fstab. Include the partition's UUID, mount point, filesystem type, and options.

- Command: blkid to get the UUID.

- Example entry: UUID=1234-5678 /mnt/data ext4 defaults 0 2

blkid /dev/sdb1
# Add the following line to /etc/fstab:
UUID=1234-5678 /mnt/data ext4 defaults 0 2

Practice Exercise

Add an entry to /etc/fstab to mount /dev/sdb1 to /mnt/backup at boot. Use the xfs filesystem and default options.

Show Solution
blkid /dev/sdb1
# Add the following line to /etc/fstab:
UUID=1234-5678 /mnt/backup xfs defaults 0 2

Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.

Sign in

Click to access the login or register cheese