This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

A new disk has to be partitioned, then formatted, then mounted before Linux can store files on it. This post shows the first step with fdisk, which ships with every distribution in the util-linux package, and then the other two briefly so you end up with a usable disk.

The original version of this post was written for IDE disks named /dev/hda, cylinder counts and MBR partition tables. All of that is history. Current fdisk creates GPT tables, takes sizes in gigabytes, and aligns partitions correctly by itself.

1. Find the right disk

This is the step that matters. Partitioning the wrong disk destroys its data, and fdisk will not stop you.

lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS,MODEL
NAME          SIZE TYPE FSTYPE MOUNTPOINTS MODEL
sda         476.9G disk                    Samsung SSD 860
├─sda1          1G part vfat   /boot/efi
└─sda2      475.9G part ext4   /
sdb           1.8T disk                    WDC WD20EZBX

The new disk is the one with the expected size and model and no partitions: here /dev/sdb.

Disk names:

  • sda, sdb, ... — SATA, SAS and USB disks. Partitions are sdb1, sdb2.
  • nvme0n1, nvme1n1, ... — NVMe SSDs. Partitions are nvme0n1p1, nvme0n1p2.
  • vda, vdb, ... — virtual disks in KVM virtual machines.

The letter order can change between boots, so check again with lsblk every time.

2. Start fdisk

sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0x8f3a51c2.

Command (m for help):

Nothing is written to the disk until you press w. Until then you can quit with q and nothing has changed.

The commands you need:

Key Action
m help
p print the partition table
g create a new, empty GPT partition table
o create a new, empty MBR (DOS) partition table
n new partition
d delete a partition
t change a partition's type
w write the changes and exit
q quit without saving

3. Create a GPT partition table

fdisk defaults to MBR on an empty disk. Use GPT instead unless the disk must boot an old BIOS-only machine. MBR is limited to disks of 2 TiB and four primary partitions; GPT has neither limit.

Command (m for help): g
Created a new GPT disklabel (GUID: 6C1F0E4A-...).

If the disk had partitions, this removes them all.

4. Create the partitions

One partition using the whole disk:

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

Created a new partition 1 of type 'Linux filesystem' and of size 1.8 TiB.

Press Enter three times to accept the defaults. The first sector, 2048, leaves the first 1 MiB free so the partition is aligned for SSDs and modern disks.

To make a partition of a given size instead, type it at the last sector prompt with a +:

Last sector, +/-sectors or +/-size{K,M,G,T,P} (...): +500G

Then run n again for the next partition and accept the default to use the rest of the space.

If the disk already had a file system, fdisk may ask whether to remove its signature. Answer Y.

5. Set the partition type (if needed)

New GPT partitions are of type Linux filesystem, which is right for ext4, XFS or Btrfs. Change it only for other uses, with t:

Command (m for help): t
Partition number (1,2, default 2): 2
Partition type or alias (type L to list all): swap
Changed type of partition 'Linux filesystem' to 'Linux swap'.

Useful aliases are linux, swap, uefi (an EFI system partition), raid and lvm. Type L for the full list.

6. Check and write

Command (m for help): p
Device          Start        End    Sectors   Size Type
/dev/sdb1        2048 1048578047 1048576000   500G Linux filesystem
/dev/sdb2  1048578048 3907028991 2858450944   1.3T Linux filesystem

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

lsblk should now show sdb1 and sdb2. If fdisk says the kernel could not re-read the partition table (because something on the disk was in use), run sudo partprobe /dev/sdb or reboot.

7. Format and mount

Create a file system on each partition. With a label, so it is easy to recognize later:

sudo mkfs.ext4 -L data /dev/sdb1

For a USB drive you want to share with Windows and macOS, use FAT32 or exFAT instead; see how to format a USB drive.

Mount it:

sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data

To mount it at every boot, add it to /etc/fstab by UUID. Device names can change between boots; UUIDs do not.

sudo blkid /dev/sdb1
/dev/sdb1: LABEL="data" UUID="3e6be9de-8139-4b3c-9106-a43f08d823a6" TYPE="ext4" PARTUUID="..."

Add this line to /etc/fstab:

UUID=3e6be9de-8139-4b3c-9106-a43f08d823a6  /mnt/data  ext4  defaults,nofail  0  2

nofail lets the system boot even if the disk is missing. The final 2 tells the boot process to check it with fsck after the root file system. Test the line before rebooting:

sudo umount /mnt/data
sudo mount -a

If mount -a gives no error and lsblk shows the partition mounted again, the fstab entry is correct.

For scripts: parted

fdisk is interactive. To partition from a script, parted does the same in one line. This creates a GPT table and a single partition that uses the whole disk:

sudo parted -s /dev/sdb mklabel gpt mkpart data ext4 1MiB 100%

-s means no questions, which also means no warning if /dev/sdb is the wrong disk.

Other tools

  • cfdisk — the same as fdisk with a menu interface in the terminal. Easier if you only do this once a year.
  • gdisk — a GPT-only version of fdisk, from before fdisk supported GPT.
  • GParted — a graphical editor that can also resize and move existing partitions.

See also

man fdisk, man parted and man fstab.