Partitioning divides a disk into sections. Changing a partition table can destroy access to everything on the disk, so practise on a spare disk or a throwaway virtual machine, and keep backups.
Identify the disk first
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 40G 0 disk
โโsda1 8:1 0 40G 0 part /
sdb 8:16 0 10G 0 diskHere sdb is empty and sda holds the running system. Check size and mount points every time.
Using fdisk
Start it on the whole disk, not on a partition:
$ sudo fdisk /dev/sdb
Command (m for help):Useful keys inside fdisk:
mshows helppprints the current tablegcreates a new empty GPT table,oa new MBR (dos) tablenadds a partitionddeletes onetchanges a partition typewwrites changes and exitsqquits without saving
Your edits stay in memory until you press w. If you make a mistake, press q.
For a new partition, accept the default first sector and give a size such as +5G for the last sector. Then check with p before writing.
Using parted
parted also handles MBR and GPT, and is convenient in scripts. Unlike fdisk, it applies changes to the disk immediately.
$ sudo parted /dev/sdb mklabel gpt
$ sudo parted /dev/sdb mkpart data ext4 1MiB 5GiB
$ sudo parted /dev/sdb printmklabel replaces the partition table. In mkpart, data is a GPT partition name and ext4 is only a type hint. It does not create a filesystem.
Graphical tools such as GParted do the same jobs. cfdisk offers a menu in the terminal.
After writing
The kernel usually notices the new table at once. If the disk is in use it may not, and you may need sudo partprobe /dev/sdb or a reboot. Confirm with lsblk:
$ lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb 8:16 0 10G 0 disk
โโsdb1 8:17 0 5G 0 partThe new partition still has no filesystem. That is the next step.
Try it
You can practise without a real disk. Make an empty file and partition that instead:
$ truncate -s 200M disk.img
$ fdisk disk.imgCreate a GPT table with g, add two partitions with n, check with p, then write with w. Delete disk.img when finished.
Check your understanding
Disk Partitioning
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.