How to Format a USB Pendrive in Linux (Engineer’s Command-Line Guide)
Formatting USB storage devices—pendrives, external SSDs, backup sticks—directly from the Linux command line is often faster, more reliable, and far more precise than clicking through a GUI. Engineers dealing with mixed-OS environments or prepping boot media already know: graphical tools fail unpredictably. The CLI rarely misstates intentions.
Below, you’ll find exact steps (verified with Debian 12, util-linux 2.38
, and dosfstools 4.2
), edge considerations, and tips on establishing FS consistency across environments.
Device Detection: Pinpoint Your USB
A single error here wipes the wrong disk. Half the format failures I’ve seen trace back to unclear device identification.
Insert the pendrive, then:
lsblk -p -o NAME,SIZE,RM,MODEL,MOUNTPOINT
Sample output:
NAME SIZE RM MODEL MOUNTPOINT
/dev/sda 512G 0 Samsung_SSD_860 /, /home, /boot
/dev/sdb 16G 1 SanDisk_Cruzer /media/deploy/USB16
├─/dev/sdb1 16G 1 /media/deploy/USB16
The pendrive is /dev/sdb
. Don’t trust df -h
; partition names shift after reboots.
Caution: Matching the MODEL field sometimes surfaces when you have multiple disks of the same size.
Unmount All Mountpoints
Don’t skip this: formatting a mounted filesystem triggers errors like:
mkfs.vfat: /dev/sdb1 contains a mounted filesystem
Standard umount (for all present partitions):
sudo umount /dev/sdb1
For drives with multiple partitions, repeat for each /dev/sdbX
.
(Optional, Nuclear) Zero the Partition Table
If legacy partitioning or “phantom” partitions persist, a surgical wipe is best:
sudo dd if=/dev/zero of=/dev/sdb bs=1M count=10 status=progress
Note: This will obliterate the first 10MB—enough for GPT/MBR/EFI traces. For secure disposal, increase count
or use shred
.
If your OS automounts after insertion, expect dmesg noise or auto-remounts immediately after. Disable automount or be quick.
Create a New Partition Table
MBR vs. GPT? FAT32 only supports MBR, while exFAT and ext4 handle GPT. For maximum compatibility (e.g., BIOS boot disks), stick with MBR.
Partitioning Example (fdisk, MBR):
sudo fdisk /dev/sdb
At the prompt:
o
— clear to DOS (MBR) tablen
,p
, ENTER — new primary partition, default number and sectors (full drive)t
,b
— type W95 FAT32 for cross-compatibility.w
— write changes
Tip: For bootable USBs, set the partition bootable with a
before writing.
GPT Alternative: Use g
for a GPT table. Required for >2TB sticks.
Create Filesystem
Here’s where intended usage dictates choice:
Filesystem | Command | Use Case |
---|---|---|
FAT32 | sudo mkfs.vfat -F32 /dev/sdb1 | Legacy BIOS, universal, <32GB |
exFAT | sudo mkfs.exfat /dev/sdb1 | Modern OS, >4GB files |
NTFS | sudo mkfs.ntfs -Q /dev/sdb1 | Windows native |
ext4 | sudo mkfs.ext4 /dev/sdb1 | Linux native, journaling |
For exFAT, install tools:
sudo apt-get install exfatprogs # Ubuntu ≥20.04
Old Ubuntu: exfat-utils
.
Side note: Formatting with -n <label>
embeds a volume label. Not all tools accept the same flags.
Verify Filesystem
Check that the device reports the intended FS:
lsblk -f /dev/sdb1
Sample:
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb1 vfat USBDATA 3C1A-2B9D
Or, for blunt verification:
sudo file -s /dev/sdb1
Yields:
/dev/sdb1: DOS/MBR boot sector; FAT (32 bit);...
Label the Filesystem (Optional)
Volume labels are not cosmetic; they appear in mount dialogs and scripts.
- For FAT32:
sudo dosfslabel /dev/sdb1 ENGINEERUSB
- exFAT:
sudo exfatlabel /dev/sdb1 XFER_2024
- ext4:
sudo e2label /dev/sdb1 linux_backup
Common Pitfalls and Pro Tips
- Auto-mount interference: Gnome and KDE often re-mount unmounted devices instantly. Unmount, format, then unplug/replug if in doubt.
- Hidden Windows partitions: If drive won't mount in Windows after using GPT, use MBR and set partition type.
- Performance tuning: For ext4 with SSD pendrives, consider
-O ^has_journal
for reduced FS wear. - mkfs errors: If
mkfs
fails with "Permission denied", double-check the block device: OS often changes device name after replug.
Quick Reference Table
Action | Command Example |
---|---|
List disks | lsblk -p -o NAME,MODEL,SIZE,RM,MOUNTPOINT |
Unmount | sudo umount /dev/sdXn |
Wipe first sectors | sudo dd if=/dev/zero of=/dev/sdX bs=1M count=10 |
New MBR table | sudo fdisk /dev/sdX |
Format FAT32 | sudo mkfs.vfat -F32 /dev/sdXn |
Format exFAT | sudo mkfs.exfat /dev/sdXn |
Format NTFS | sudo mkfs.ntfs -Q /dev/sdXn |
Format ext4 | sudo mkfs.ext4 /dev/sdXn |
Label FAT32 | sudo dosfslabel /dev/sdXn LABEL |
Label exFAT | sudo exfatlabel /dev/sdXn LABEL |
Label ext4 | sudo e2label /dev/sdXn LABEL |
Notes from the Field
Formatting via CLI offers complete visibility into each stage and avoids the “unknown error” GUI problem. For field deployments or rescue workflows, scripting these steps ensures reproducibility at scale. Not perfect: some cheap USB devices occasionally report successful writes but silently discard data—always verify with md5sum
or similar if data integrity matters.
For further automation (bulk formatting/testing), consider using parted --script
, wipefs
, or udev
rules—however, manual fdisk remains more reliable for single-drive ops and safeguards against batch mistakes.
Further Topic?
If precise mounting, multi-partition USB layouts, or automated provisioning pipelines for boot media are required, these can be covered separately—each topic introduces its own subtle complexities.