Intermediate · 12 min · practise every command in the built-in terminal

Disks and Partitions — lsblk, fdisk, mkfs

A server rarely keeps just one disk. Your provider attaches a new volume in a click — and then it's yours to set up. This server has just got one: 20 GiB, empty.

What disks are there?

lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda       8:0    0   60G  0 disk
├─sda1    8:1    0 59.9G  0 part /
├─sda14   8:14   0    3M  0 part
└─sda15   8:15   0  124M  0 part /boot/efi
sdb       8:16   0   20G  0 disk
  • sda is the system disk: its partition sda1 is mounted on / — everything you've used so far lives there.
  • sdb is the new one: no partitions, mounted nowhere. Nothing can use it yet.

Disks are files in /dev: /dev/sda, /dev/sdb, and their partitions /dev/sda1, /dev/sda15...

Three steps make a disk usable:

StepToolWhat it does
1. Partitionfdiskdivides the disk (here: one partition for all of it)
2. Formatmkfs.ext4puts a filesystem on the partition
3. Mountmountattaches it to a folder — the next lesson

1. Partition: fdisk

See the disk first:

sudo fdisk -l /dev/sdb

Then open it. fdisk asks with one-letter commands and changes nothing until you write:

KeyDoes
ga new, empty GPT partition table
na new partition
pprint the table
ddelete a partition
wwrite it to the disk and quit
qquit without saving
sudo fdisk /dev/sdb
Command (m for help): g
Created a new GPT disklabel (GUID: 0F4B7A2E-…).

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-41943006, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943006, default 41940991):

Created a new partition 1 of type 'Linux filesystem' and of size 20 GiB.

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

Enter accepts each default — one partition for the whole disk. For a 5 GiB one you'd answer +5G to Last sector.

⚠️ fdisk on the wrong disk wipes it. Check the name with lsblk first — /dev/sda is the system.

2. Format: mkfs.ext4

sudo mkfs.ext4 -L data /dev/sdb1

ext4 is Linux's standard filesystem; -L data gives it a label. Every filesystem gets a UUID — a name that stays the same even if the disk shows up as sdc one day:

sudo blkid /dev/sdb1
/dev/sdb1: LABEL="data" UUID="d4a70b1e-7e2a-4c1d-9f5b-3a8e6c2d1f07" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="…"

lsblk -f shows the filesystems of all disks at once: type, label, UUID, free space.

💡 Formatting a partition that already has data asks Proceed anyway? — that data is gone afterwards.

Practice tasks

  1. List the disks: which one is new?
  2. Look at the new disk's details
  3. Make one partition that fills /dev/sdb, on a GPT partition table
  4. Put an ext4 filesystem on /dev/sdb1, labelled data
  5. Show the new filesystem's UUID
  6. See all disks with their filesystems

Commands in this lesson

Disk & Memory