In Linux systems, disk partitioning and mounting are fundamental operations for managing storage space. Imagine your computer’s hard drive as a large wardrobe; partitioning divides it into smaller compartments, while mounting connects these compartments to a directory (like a wardrobe entrance), allowing the system to recognize and use the space. Let’s quickly master this skill with the simplest steps!
Step 1: Check Your Disk “Inventory”¶
Before starting, confirm the disks and existing partitions recognized by the system.
-
View Disk List: Use
lsblkorfdisk -l(requires root privileges, e.g.,sudo fdisk -l).
In the output,/dev/sdarepresents the first SATA hard drive, and/dev/sda1is its first partition (the number1denotes the partition number; primary/extended partitions are other types). -
View Mounted Partitions: Run
df -hto see currently mounted partitions (e.g.,/dev/mapper/cl-rootis the root partition).
Step 2: “Cut the Cake” for the Disk — Create a New Partition¶
Assume you have a new disk /dev/sdb (unpartitioned) or want to add a partition to /dev/sda. Follow these steps:
-
Enter Partition Tool: Use
fdisk(requires root):
For example,sudo fdisk /dev/sdb(back up data before operating!). -
Create a New Partition:
- Typen(New) → Presspto select “Primary Partition” (recommended for beginners;eis for extended partitions, which we can ignore for now) → PressEnter(accept default partition type) → Specify the partition size (or pressEnterto use the remaining space).
- To specify a size (e.g., 20GB), enter+20G(units: G/M/K). -
Save and Exit: Type
w(Write) and pressEnterto confirm. The partition is now created!
Step 3: “Dress the Partition” — Format the Partition¶
After creating a partition, the system cannot use it directly. Formatting (like painting the wardrobe compartment) is required.
-
Choose a File System: Linux commonly uses
ext4(similar to Windows NTFS). Run:
sudo mkfs.ext4 /dev/sdb1(replace/dev/sdb1with your new partition). -
Warning: Formatting will erase data! Back up data before formatting!
Step 4: “Hang” the Partition in the System — Temporary Mount¶
To make the system accessible, mount the partition to a directory (e.g., /mnt/mynewdisk):
-
Create a Mount Point Directory:
Create an empty directory as the mount point:
sudo mkdir /mnt/mynewdisk. -
Mount the Partition:
Use themountcommand:
sudo mount /dev/sdb1 /mnt/mynewdisk.
- Now, accessing/mnt/mynewdiskwill show the new partition’s contents.
- Verify: Rundf -hto confirm/dev/sdb1is mounted at/mnt/mynewdisk.
Step 5: Permanent Mount — Remain Effective After Reboot¶
Temporary mounting only works during the current session. To make it persistent, modify the /etc/fstab file:
-
Get the Partition UUID (more stable, avoiding device name changes):
Runsudo blkid /dev/sdb1and find theUUID="xxxx-xxxx-xxxx-xxxx"(e.g.,UUID="a1b2c3d4-5678-90ef-ghij"). -
Edit the fstab File:
Open/etc/fstabwithsudo nano /etc/fstaband add the following line at the end:
UUID=a1b2c3d4-5678-90ef-ghij /mnt/mynewdisk ext4 defaults 0 0
- Format Explanation:
[UUID/Device Name] [Mount Point] [File System] [Mount Options] [dump] [fsck Order]defaults: Default mount options (read/write, no compression, etc.). Adjust as needed.
- Verify Mounting:
Runsudo mount -a; no errors mean the configuration is correct.
Summary & Notes¶
- Key Steps: Partition (create) → Format (paint) → Mount (connect) → Persist (fstab).
- Risk Warning: Always back up data before partitioning/formatting to avoid data loss from accidental deletion or formatting.
- Unmount a Partition: Use
sudo umount /mnt/mynewdisk(ensure the directory is not in use).
With these steps, you’ve successfully partitioned and mounted a Linux disk. Feel free to leave questions in the comments! See you next time!