Linux Tips guides, explanations and reader checklists on whilenetworking.com

How to Create and Mount a New Partition or USB Drive in Linux

Expanding your storage capacity in Linux is a common task for system administrators, developers, and everyday users. Whether you have just purchased a new…

Expanding your storage capacity in Linux is a common task for system administrators, developers, and everyday users. Whether you have just purchased a new external hard drive, found an unused internal SATA port, or need to carve out extra space on an existing disk for a project file, understanding how to partition and mount volumes is essential. This guide provides a step-by-step, practical approach to creating partitions using fdisk or GParted, formatting them with the appropriate filesystem, and mounting them so your system can access the new data immediately.

Understanding the Workflow: Partitioning vs. Mounting

Before diving into commands, it is crucial to distinguish between partitioning and mounting. Partitioning involves dividing a physical disk into logical sections; this creates a container for data but does not make the data readable by the operating system yet. Mounting is the process of attaching that partition to a specific directory in your filesystem tree, making its contents accessible via a path like /mnt/data or /media/usb.

The workflow generally follows three stages:
1. Preparation: Identifying available hardware and deciding on partition size.
2. Creation and Formatting: Using tools to create the partition structure and writing a filesystem (like ext4 or exFAT).
3. Mounting: Linking the new volume to a directory so you can read and write files.

Identifying Your Storage Hardware

Most Linux distributions include built-in tools to view connected storage devices without installing additional software. The most common command is lsblk, which lists all block devices, their sizes, mount points, and filesystem types in a tree-like structure. Another useful tool is sudo fdisk -l, which provides detailed information about each disk, including partition tables.

When reviewing the output, look for your new device, often labeled as /dev/sdb or /dev/nvme1n1. Be extremely careful to match the size; if you see a 500GB drive but are looking at a 2TB drive, ensure you are selecting the correct identifier. Always double-check the device name before running any destructive commands like mkfs or fdisk, as accidental formatting of your root partition (/dev/sda) will result in total data loss.

Creating a New Partition with fdisk

For advanced users who prefer command-line tools, fdisk is the standard utility for managing partitions on traditional MBR disks. Start by running sudo fdisk /dev/sdX, replacing sdX with your target device (e.g., /dev/sdb). Once inside the interactive prompt, you can create a new primary partition:

  1. Type n and press Enter to add a new partition.
  2. Select p for a primary partition.
  3. Press Enter to accept the default partition number (usually 1).
  4. For the first sector, press Enter to use the default start point.
  5. For the last sector, you can type +100G to create a partition of exactly 100 gigabytes, or omit it to use the entire remaining space.

After defining the partition, type p to print the partition table and verify your changes. If everything looks correct, save the changes by typing w. The program will write the new partition table to disk and exit. You can now see the new partition (e.g., /dev/sdb1) listed in lsblk.

Formatting the Partition with a Filesystem

A raw partition is just empty space; you must format it with a filesystem to store files. The choice of filesystem depends on your needs:
* ext4: The default for most Linux systems, offering excellent performance and journaling.
* XFS: Excellent for large files and high throughput, often used in enterprise environments.
* exFAT: Ideal if you plan to use the drive with Windows or macOS alongside Linux, as it supports large files but lacks journaling.

To format a partition, use the mkfs command. For example, to create an ext4 filesystem on /dev/sdb1, run:

sudo mkfs.ext4 /dev/sdb1

If you choose exFAT for cross-platform compatibility, ensure you have the dosfstools package installed first (sudo apt install dosfstools or equivalent), then use sudo mkfs.exfat /dev/sdb1. Note that formatting erases all existing data on the partition. If you intended to create a new partition on an empty drive, this is expected.

Mounting the Partition Automatically

Once formatted, the partition needs to be mounted to become accessible. You can mount it temporarily using the mount command:

sudo mount /dev/sdb1 /mnt/data

This makes the contents available at /mnt/data. However, temporary mounts are lost after a reboot. To make the mount persistent across reboots, you must update the /etc/fstab file. First, determine the UUID of your new partition using sudo blkid. Then, add a line to /etc/fstab with the format:

UUID=your-uuid-here /mnt/data ext4 defaults,noatime 0 2

Replace your-uuid-here with the actual UUID found in the previous step. The defaults options are standard, but you might add noatime to improve performance by not updating access times on every read. If a typo exists in /etc/fstab, the system may fail to boot; always test the mount command before rebooting to verify syntax.

Managing USB Drives and Auto-Mounting

USB drives behave slightly differently than internal partitions because they are removable media. Many desktop Linux environments (like Ubuntu or Fedora) automatically detect and mount USB drives when plugged in, typically placing them under /media/username/drive-label. This behavior is managed by the udisks daemon.

If you prefer to manage USB drives manually or need to mount a USB stick on a server that lacks auto-mounting capabilities, follow the same steps as internal partitions: identify the device with lsblk, format it if necessary (using exFAT for Windows compatibility), and create a permanent mount point in /etc/fstab. However, be cautious with /etc/fstab entries for USB drives; since USB ports change or devices are unplugged, relying on UUIDs is safer than using device names like /dev/sdb1, which can shift if you add another internal drive later.

Common Mistakes and Security Considerations

When working with partitions, the most frequent error is targeting the wrong device. Always verify the disk size before formatting. Another common mistake is forgetting to set a UUID or label, making it difficult to identify the drive in /etc/fstab later. You can assign a readable label using sudo e2label /dev/sdb1 "MyData" for ext4 partitions.

Security considerations are also vital. Never run partitioning tools as root directly without caution; use sudo. Ensure that your filesystem is mounted with appropriate permissions. By default, new ext4 partitions are owned by root (root:root). If you want to allow your regular user to read and write files without switching to root every time, change the ownership using chown -R $USER:$USER /mnt/data after mounting. Additionally, consider enabling encryption (LUKS) if the partition contains sensitive data, though this adds complexity to the boot process and requires decryption keys at startup.

Conclusion

Mastering the creation and mounting of partitions in Linux empowers you to manage storage flexibly and securely. By following the steps to identify hardware, create partitions with fdisk, format with the correct filesystem, and configure /etc/fstab, you ensure your new drives are ready for immediate use. Whether expanding a system drive or setting up a portable USB backup, these skills are fundamental to Linux administration. Always remember to verify device sizes before formatting and test mount configurations to prevent boot failures.