Bird
Raised Fist0
Linux CLIscripting~5 mins

fdisk and lsblk in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to see and manage the disks and partitions on your Linux computer. fdisk helps you create or change partitions, and lsblk shows you a clear list of all disks and their partitions.
When you want to check what disks and partitions are available on your system before installing software.
When you need to create a new partition on a disk to store files separately.
When you want to see the size and layout of your disks and partitions quickly.
When you want to delete or modify partitions on a disk safely.
When preparing a new hard drive for use by creating partitions.
Commands
This command lists all block devices (disks and partitions) in a tree format so you can see their names, sizes, and mount points.
Terminal
lsblk
Expected OutputExpected
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 232.9G 0 disk sda1 8:1 0 100G 0 part / sda2 8:2 0 132.9G 0 part /home sdb 8:16 0 931.5G 0 disk sdb1 8:17 0 931.5G 0 part /mnt/data
This opens fdisk to manage partitions on the disk /dev/sdb. You need sudo because it changes disk settings.
Terminal
sudo fdisk /dev/sdb
Expected OutputExpected
Welcome to fdisk (util-linux 2.36.1). Changes will remain in memory only, until you decide to write them. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0x12345678. Command (m for help):
Inside fdisk, this command prints the current partition table to see existing partitions.
Terminal
p
Expected OutputExpected
Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors Disklabel type: dos Disk identifier: 0x12345678 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 1953523711 1953521664 931.5G 83 Linux Command (m for help):
This quits fdisk without saving any changes, useful if you only wanted to view partitions.
Terminal
q
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: lsblk shows your disks and partitions clearly, and fdisk lets you safely create or change partitions.

Common Mistakes
Running fdisk without sudo
fdisk needs administrative rights to change disk partitions, so it will fail without sudo.
Always run fdisk with sudo, like 'sudo fdisk /dev/sdb'.
Trying to write changes in fdisk without understanding commands
Writing wrong changes can delete or corrupt partitions and data.
Use 'p' to print partitions and 'q' to quit without saving if unsure. Learn commands before writing changes.
Confusing disk names like /dev/sda and /dev/sdb
Modifying the wrong disk can cause data loss on important drives.
Use lsblk first to identify the correct disk before running fdisk.
Summary
Use lsblk to list all disks and partitions with sizes and mount points.
Use sudo fdisk /dev/sdX to open the disk for partition management.
Inside fdisk, use 'p' to print partitions and 'q' to quit without saving.

Practice

(1/5)
1. What is the primary purpose of the fdisk command in Linux?
easy
A. To display the current directory contents
B. To create and modify disk partitions
C. To list running processes
D. To monitor network connections

Solution

  1. Step 1: Understand the role of fdisk

    The fdisk command is used to create, delete, and modify disk partitions on Linux systems.
  2. Step 2: Compare with other commands

    Other options like listing directories, processes, or network connections are unrelated to disk partitioning.
  3. Final Answer:

    To create and modify disk partitions -> Option B
  4. Quick Check:

    fdisk = disk partition tool [OK]
Hint: fdisk = fix disk partitions [OK]
Common Mistakes:
  • Confusing fdisk with lsblk
  • Thinking fdisk lists files
  • Assuming fdisk monitors network
2. Which of the following is the correct syntax to list all block devices using lsblk?
easy
A. lsblk --all
B. lsblk -d
C. lsblk -l
D. lsblk -f

Solution

  1. Step 1: Identify the option to list all devices

    The --all option with lsblk lists all devices, including empty ones.
  2. Step 2: Check other options

    -d lists only whole disks (no partitions), -l lists in list format, -f shows filesystem info but not all devices.
  3. Final Answer:

    lsblk --all -> Option A
  4. Quick Check:

    lsblk --all = show all devices [OK]
Hint: --all shows everything in lsblk [OK]
Common Mistakes:
  • Using -d instead of --all
  • Confusing -l with --all
  • Using -f to list devices
3. Given the command output below, what is the size of the partition /dev/sda1?
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
sda1     8:1    0   50G  0 part /
sda2     8:2    0   50G  0 part /home
medium
A. 100G
B. 0G
C. 50G
D. 150G

Solution

  1. Step 1: Read the lsblk output

    The line for /dev/sda1 shows a size of 50G under the SIZE column.
  2. Step 2: Understand partition sizes

    The disk sda is 100G total, split into two partitions of 50G each.
  3. Final Answer:

    50G -> Option C
  4. Quick Check:

    /dev/sda1 size = 50G [OK]
Hint: Check SIZE column for partition size [OK]
Common Mistakes:
  • Confusing disk size with partition size
  • Adding partition sizes incorrectly
  • Ignoring the SIZE column
4. You run fdisk /dev/sdb but get the error: cannot open /dev/sdb: Permission denied. What is the most likely cause?
medium
A. The disk /dev/sdb is formatted with ext4
B. The disk /dev/sdb is already mounted
C. The lsblk command is not installed
D. You forgot to run the command as root or with sudo

Solution

  1. Step 1: Understand permission requirements

    Running fdisk on disks requires root privileges; without sudo or root, device access is denied.
  2. Step 2: Check other options

    Disk being mounted or formatted does not cause 'Permission denied' error; missing lsblk is unrelated.
  3. Final Answer:

    You forgot to run the command as root or with sudo -> Option D
  4. Quick Check:

    fdisk needs root access [OK]
Hint: Use sudo with fdisk to access disks [OK]
Common Mistakes:
  • Assuming mounted disk causes 'Permission denied'
  • Thinking formatting affects fdisk detection
  • Confusing fdisk errors with lsblk issues
5. You want to create a new partition on /dev/sdc using fdisk. After creating the partition, which command should you run to immediately see the updated partition layout in a tree view?
hard
A. lsblk /dev/sdc
B. fdisk -l /dev/sdc
C. mount /dev/sdc1 /mnt
D. df -h /dev/sdc

Solution

  1. Step 1: Identify command to show partition tree

    lsblk displays block devices and partitions in a clear tree format, perfect for checking new partitions.
  2. Step 2: Compare with other commands

    fdisk -l lists partitions but not in tree view; mount mounts partitions; df -h shows disk usage, not layout.
  3. Final Answer:

    lsblk /dev/sdc -> Option A
  4. Quick Check:

    lsblk shows partition tree [OK]
Hint: Use lsblk to view partitions visually [OK]
Common Mistakes:
  • Using fdisk -l expecting tree view
  • Trying to mount before checking partitions
  • Using df to see partition layout
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard