Introduction / Why This Is Needed
Disk management is one of the key tasks for a Linux system administrator. Without proper partitioning, formatting, and mounting, you won't be able to use new disks or redistribute space. This guide will walk you through all the basic steps: from viewing existing disks to setting up permanent mounting. After completing it, you'll be able to confidently work with block devices in Linux.
Requirements / Preparation
Before you begin, ensure that:
- You have access to a Linux terminal (Ubuntu, Debian, CentOS, or another systemd-based distribution).
- You have superuser (
sudo) privileges to perform disk operations. - Basic utilities are installed:
fdisk(usually in theutil-linuxpackage),lsblk,e2fsprogs(for ext4). They are present by default in most distributions. - Critically important: If the target disk contains important data, back it up. Partitioning operations are irreversible.
Step 1: Viewing Current Disks and Partitions
First, determine which disks and partitions already exist in the system. Use two main commands:
lsblk
This command outputs a tree of all block devices (disks, partitions) in a convenient format. Pay attention to the columns: NAME (device name, e.g., sda1), SIZE (size), MOUNTPOINT (mount point, if the partition is already mounted).
sudo fdisk -l
More detailed information. Shows partition tables for each disk, partition type, start/end. Requires sudo to read all devices.
Example lsblk output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 99.5G 0 part /
sdb 8:16 0 20G 0 disk
Here we see two disks (sda and sdb). sdb has no partitions and is not mounted—a candidate for work.
Step 2: Creating a New Partition on the Disk
Let's say we want to create a single partition using the entire disk sdb. Use fdisk (interactive) or parted (more modern). For simplicity and broad support, we'll use fdisk.
sudo fdisk /dev/sdb
You'll enter interactive mode. Type the following commands (each on a separate line, confirming with Enter):
n— create a new partition.p— partition type (primary).1— partition number (first).- Press
Entertwice to accept defaults (start and end of the disk). w— write changes and exit.
⚠️ Important: If the disk already has partitions,
fdiskmay ask you to delete them first (d). Be careful—this will destroy data.
Check the result:
lsblk /dev/sdb
Now you should see sdb1.
Step 3: Formatting the Partition to a Filesystem
The created partition sdb1 is still empty—it needs to be formatted. The choice of filesystem depends on your needs:
- ext4 — the most common, reliable, suitable for most tasks.
- xfs — efficient for very large disks and files, often used on servers.
- btrfs — modern, with snapshot and compression support.
For a general case, use ext4:
sudo mkfs.ext4 /dev/sdb1
The process will take a few seconds. The mkfs command is a frontend for specific utilities (mkfs.ext4).
💡 Tip: To find the partition's UUID (a unique identifier that doesn't change upon reconnection), run
sudo blkidafter formatting. You'll need it for/etc/fstab.
Step 4: Mounting the Partition Manually
Now the partition is ready for use. Create a directory that will be the mount point:
sudo mkdir -p /mnt/mydata
Mount the partition:
sudo mount /dev/sdb1 /mnt/mydata
Check:
df -h
You should see /dev/sdb1 in the list with the mount point /mnt/mydata. The partition is available for read/write.
⚠️ Important: This is a temporary mount. After a reboot, the partition will not mount automatically. For permanent mounting, proceed to the next step.
Step 5: Setting Up Automatic Mounting via /etc/fstab
The /etc/fstab file (file systems table) contains information about all permanent filesystems. Add a line for our partition there.
Open the file in an editor (e.g., sudo nano /etc/fstab) and add this line at the end:
UUID=your-partition-uuid /mnt/mydata ext4 defaults 0 2
How to get the UUID? Run sudo blkid and copy the value for /dev/sdb1 (e.g., UUID="a1b2c3d4-...").
Field explanation:
UUID=...— the partition's unique identifier (more reliable than/dev/sdb1, since device names can change)./mnt/mydata— the mount point (must exist).ext4— filesystem type.defaults— standard options (rw,suid,dev,exec,auto,nouser,async).0— do not dump (fordump, usually 0).2— filesystem check order at boot (1 for root/, 2 for others).
Check fstab correctness without risk:
sudo mount -a
If there are no errors, the configuration is correct. The partition will mount automatically on every boot.
Verifying the Result
- Ensure the partition is mounted:
mount | grep mydataordf -h. - Try creating a file:
sudo touch /mnt/mydata/test.txt. - Reboot the system (
sudo reboot), and after booting, check that the partition is mounted again (df -h).
Possible Issues
- Error "mount: /mnt/mydata: special device /dev/sdb1 does not exist." — The partition was not created or an incorrect device name is specified. Check
lsblk. - Error "wrong fs type, bad option, bad superblock" — The filesystem does not match what's specified in
fstabor the partition is not formatted. Ensure the type infstab(ext4) matches the actual one (sudo blkid). - Partition does not mount after boot — Check
/etc/fstabsyntax (spaces/tabs), existence of the mount point, and that theautooption is present indefaults. - Access error when mounting without
sudo— Infstab, for a partition that should be mountable by a regular user, add theuseroption instead ofdefaults(but this reduces security).