How-to

Mounting a Disk in Linux: lsblk, Partitioning, mkfs, mount and fstab

Updated: · Skillaria.Top

Mounting a disk in Linux means attaching the filesystem of a partition to a directory in the tree, for example /data. A new disk is found with lsblk, partitioned with fdisk or parted, formatted with mkfs.ext4, mounted with mount /dev/sdb1 /data, and to make the partition mount after a reboot you add a line with its UUID to /etc/fstab and check it with mount -a.

Linux has no drive letters: all storage devices are attached into a single directory tree that starts at /. Mounting a disk in Linux is the operation that "hangs" the filesystem of a partition onto an empty directory, the mount point. The path for a new disk is always the same: find the device (lsblk), partition it (fdisk or parted), create a filesystem (mkfs), mount it (mount) and make it permanent in /etc/fstab. We go through each step and also look at how to find out where the space went, using df and du.

How disks and partitions work in Linux

The kernel represents each disk as a device file in /dev. The names depend on the connection type:

NameWhat it isPartitions
/dev/sda, /dev/sdbSATA/SAS/USB disks, many virtual disks/dev/sda1, /dev/sda2
/dev/vdavirtio disks in KVM virtual machines/dev/vda1
/dev/nvme0n1NVMe SSD/dev/nvme0n1p1
/dev/mapper/...LVM logical volumes, encrypted volumes—

A disk is split into partitions, a filesystem (ext4, xfs, btrfs) is created on a partition, and only then is it mounted to a directory. Until a partition is mounted, its files are inaccessible, even if the disk is physically connected.

Viewing disks: lsblk and blkid

The first command when working with disks is lsblk. It shows the device tree, sizes and mount points:

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   40G  0 disk
├─sda1   8:1    0    1G  0 part /boot
└─sda2   8:2    0   39G  0 part /
sdb      8:16   0  100G  0 disk

Here we can see that sdb is a new 100 GB disk with no partitions and no mount point. Useful variants:

  • lsblk -f — the filesystem type, label and UUID of each partition.
  • lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT,MODEL — only the columns you need.
  • sudo blkid — UUID and filesystem type for all partitions.
  • sudo fdisk -l — partition tables of all disks with sector numbers.

If the disk was added to a virtual machine "hot" and lsblk does not see it, ask the kernel to rescan the bus: echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan.

Partitioning a disk: fdisk and parted

Before partitioning, double-check that you have chosen the right device: writing a partition table to the system disk will destroy data. Look at the size and the absence of mount points in the lsblk output.

GPT or MBR

For new disks use GPT: it supports disks larger than 2 TB and up to 128 partitions. MBR (dos) is needed only for old systems that boot through BIOS, where the bootloader requires it.

Partitioning with fdisk

Modern fdisk handles both GPT and MBR. The work is done interactively:

$ sudo fdisk /dev/sdb
Command (m for help): g        # create an empty GPT table
Command (m for help): n        # new partition
Partition number (1-128, default 1): Enter
First sector: Enter
Last sector, +/-sectors or +/-size{K,M,G,T,P}: Enter   # the whole disk
Command (m for help): w        # write the changes and exit

Until you press w, nothing is written to the disk, so q lets you exit without changes.

Partitioning with parted in one line

In scripts parted is more convenient, since it works without a dialog:

$ sudo parted -s /dev/sdb mklabel gpt
$ sudo parted -s /dev/sdb mkpart primary ext4 0% 100%
$ lsblk /dev/sdb
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0  100G  0 disk
└─sdb1   8:17   0  100G  0 part

Boundaries given as percentages produce correct partition alignment. If the new partition does not appear, run sudo partprobe /dev/sdb so the kernel rereads the table.

Creating a filesystem: mkfs

A partition is just a range of blocks. To store files, a filesystem must be created on it:

$ sudo mkfs.ext4 -L data /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 26214144 4k blocks and 6553600 inodes
...
Writing superblocks and filesystem accounting information: done

The -L option sets a label, by which the partition is easy to identify later. Which one to choose:

FilesystemCommandWhen it fits
ext4mkfs.ext4a universal default choice, can be shrunk
XFSmkfs.xfslarge files and volumes, the standard in RHEL; can only grow
Btrfsmkfs.btrfssnapshots, compression, subvolumes
vfat/exFATmkfs.vfat, mkfs.exfatflash drives for exchange with Windows and macOS

mkfs erases everything that was on the partition. The command does not ask for confirmation on an empty partition, so double-check the device name once more.

How to mount a disk in Linux with mount

Create a directory — the mount point — and attach the partition to it:

$ sudo mkdir -p /data
$ sudo mount /dev/sdb1 /data
$ df -h /data
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        98G   24K   93G   1% /data

mount detects the filesystem type by itself, but you can specify it explicitly: mount -t xfs. If the /data directory already contained files, they are not deleted after mounting; they are simply hidden under the new filesystem and reappear once it is unmounted.

Mount options

Options are passed with -o, separated by commas:

  • ro / rw — read-only or read and write.
  • noatime — do not update the access time on every read, which means fewer unnecessary writes.
  • noexec — forbid running programs from this partition (useful for /tmp and download directories).
  • nosuid, nodev — ignore SUID bits and device files.
  • remount — change options without unmounting: sudo mount -o remount,ro /data.

To see what is mounted and with which options, the findmnt command is convenient: findmnt /data or findmnt -t ext4.

Permissions on the mounted disk

The root of a new ext4 filesystem belongs to root, so an ordinary user cannot write there. Hand the directory over to the right user after mounting: sudo chown alice:alice /data. For vfat and exFAT there are no Unix permissions; the owner is set with the uid=, gid= and umask= options at mount time.

How to unmount a disk: umount and the target is busy error

Unmounting is done with the umount command (without the letter n) given a device or a directory:

$ sudo umount /data
umount: /data: target is busy.

The error means that some process is holding files on this filesystem or has it as its current directory — often your own terminal after cd /data. These help to find the culprit:

$ sudo lsof +f -- /data
$ sudo fuser -vm /data

Stop the process or leave the directory, then repeat. As a last resort use umount -l (lazy): the filesystem is detached from the tree immediately and is actually released when the processes close their files.

Mounting automatically at boot: /etc/fstab

The mount command works until the next reboot. To have the disk attached automatically, add a line to /etc/fstab. Refer to the partition by UUID, not by the name /dev/sdb1: device names can swap places when disks are added, while the UUID is permanent.

$ sudo blkid /dev/sdb1
/dev/sdb1: LABEL="data" UUID="3f1c9a2e-7b5d-4c1e-9a8f-2d6e4b0c1a77" TYPE="ext4" PARTUUID="..."

The line format has six fields:

# <device>                                 <mount point>  <type>  <options>         <dump> <fsck>
UUID=3f1c9a2e-7b5d-4c1e-9a8f-2d6e4b0c1a77  /data          ext4    defaults,noatime  0      2
FieldMeaning
deviceUUID=..., LABEL=... or a path
mount pointthe directory to mount to (must exist)
typeext4, xfs, swap, nfs, etc.
optionsdefaults plus what you need, for example noatime, nofail
dump

FAQ

How do I mount a USB flash drive in Linux?

Find it with lsblk (usually /dev/sdb1), create a directory and run sudo mount /dev/sdb1 /mnt/usb. In desktop environments flash drives are mounted automatically to /media/username or /run/media.

How do I mount an ISO image?

With sudo mount -o loop,ro image.iso /mnt/iso: the loop option attaches the file as a block device. The image is unmounted with a regular umount.

How do I connect a network share through fstab?

For NFS the line looks like server:/export /mnt/nfs nfs defaults,_netdev,nofail 0 0, for SMB — //server/share /mnt/smb cifs credentials=/root/.smbcred,_netdev,nofail 0 0. The _netdev option waits until the network is up.

How is UUID different from PARTUUID?

UUID belongs to the filesystem and changes when mkfs is run again, while PARTUUID belongs to the partition in the GPT table and survives reformatting. fstab usually uses UUID.

How do I check a filesystem for errors?

Unmount the partition and run sudo fsck -f /dev/sdb1 for ext4 or sudo xfs_repair /dev/sdb1 for XFS. Checking a mounted filesystem for writes is not allowed — it can damage the data.

Learn this topic on video

This topic is covered in depth with practice in the course “Linux: с нуля до уверенного администрирования”.

Go to course