How To Format Usb On Linux

How To Format Usb On Linux

Reading time1 min
#Linux#USB#CommandLine#LinuxUSB#USBFormatting#CLI

Mastering USB Formatting on Linux: A Command-Line Approach Beyond GUI Tools

Formatting USB drives on Linux comes up in automation pipelines, recovery operations, and when prepping devices for cross-platform compatibility. Relying on GUI utilities (e.g., GParted, Disks) is inflexible for headless, remote, or minimal environments. Direct manipulation via command-line utilities offers speed, control, and the capacity to script repeatable workflows.

Below: precise methods for identifying, partitioning, and formatting USB drives using coreutilities like lsblk, fdisk, parted, and mkfs. Example workflows assume a modern Linux (tested on Ubuntu 22.04 LTS, kernel 5.15+).


Device Identification (Critical)

First, prevent data loss: positively identify the target USB device. Mistakes here are destructive.

lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,MOUNTPOINT

Sample output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0   500M  0 part /boot
└─sda2   8:2    0 465.3G  0 part /
sdb      8:16   1  14.5G  0 disk 
└─sdb1   8:17   1  14.5G  0 part /media/usb1

RM=1 denotes removable media; typically, the newly inserted USB device. Double-check with dmesg | tail -20 after plug-in for kernel logs:

[12345.678901] sd 8:0:0:0: [sdb] 15124992 512-byte logical blocks: (7.75 GB/7.21 GiB)

Note: On multi-disk systems, avoid parallel operations during identification.


Unmount All USB Partitions

Proceed only after confirming the device is not in use. OS automounts /dev/sdb1 to /media/usb1 by default in most desktops. Use:

sudo umount /dev/sdb1

If the device presents multiple partitions:

lsblk /dev/sdb
sudo umount /dev/sdb*

Failure to unmount results in mkfs errors such as:

mkfs.vfat: /dev/sdb1 is mounted; will not make a filesystem here!

Partition Table: Rebuild From Scratch

Interactive vs. scripted approaches each have use cases.

Classic: fdisk (Alert: destructive, not undoable)

sudo fdisk /dev/sdb

Typical sequence:

  • o – new DOS partition table (GPT with g if >2TB).
  • n – new partition: type p, partition number 1, accept defaults for full-drive usage.
  • t – change type: enter c for W95 FAT32 (LBA).
  • w – write and exit.

Side note: Some newer hardware (e.g., UEFI-only systems) may require GPT. In that case:

  • Replace o with g for GUID Partition Table (GPT).
  • Partition type may need to be adjusted (see parted below).

Automated / Scripted: parted

For headless workflows or scripting:

sudo parted /dev/sdb --script -- mklabel msdos mkpart primary fat32 1MiB 100%
  • mklabel msdos: MS-DOS (MBR) partition table.
  • For GPT, switch to mklabel gpt.

Format Partition with the Correct Filesystem

File system should match target workload. The -n flag gives a label, which aids in automounting and diagnostics.

FAT32: Cross-platform, <4GB file limit

sudo mkfs.vfat -F32 -n USBSTICK /dev/sdb1

Note: Use dosfstools >= 4.1 for reliability on >32GB devices, otherwise error:
mkfs.fat: warning - FAT32 selected, but size is under 512MB

exFAT: For drives >32GB or files >4GB (requires exfatprogs on newer distributions)

sudo mkfs.exfat -n USBSTICK /dev/sdb1

If not installed:

sudo apt install exfatprogs

ext4: Native Linux, metadata journaling

sudo mkfs.ext4 -L LINUXDATA /dev/sdb1

Gotcha: Not readable by Windows/macOS natively. Avoid for cross-platform sharing.


Confirm with lsblk -f and Mount Point Test

Verify result and UUID:

lsblk -f /dev/sdb

Sample:

NAME   FSTYPE LABEL     UUID                                 MOUNTPOINT
sdb                                                            
└─sdb1 vfat   USBSTICK  6E7A-EF20                            

Test mounting:

sudo mount /dev/sdb1 /mnt
touch /mnt/testfile
ls /mnt
sudo umount /mnt

Look for write errors; check dmesg for I/O faults (possible with aging flash).


Script It: Bulk Formatting (Advanced Automation)

To wipe and format multiple USB sticks for lab setups:

#!/bin/bash
set -e

DEVICE=$1  # e.g., /dev/sdc; always verify
[ -z "$DEVICE" ] && { echo "Specify device, e.g., /dev/sdc"; exit 1; }

sudo umount ${DEVICE}?* || true
sudo parted $DEVICE --script -- mklabel msdos mkpart primary fat32 1MiB 100%
sleep 1  # Wait for partition table to settle
sudo mkfs.vfat -F32 -n USBLAB ${DEVICE}1

Call as:

chmod +x bulk_format.sh
./bulk_format.sh /dev/sdc

Note: sleep avoids race conditions sometimes triggered by rapid partition table changes.


Non-Obvious Case: Bootable Device Imaging (dd Caveat)

Typical boot media prep:

sudo dd if=./archlinux-2024.04.01-x86_64.iso of=/dev/sdb bs=8M status=progress oflag=sync

Do not format before dd imaging; images overwrite the MBR.
Afterwards, accidental writes via mkfs will destroy the image.


Practical Considerations

  • Some USB sticks (especially branded as “secure” or with hardware encryption) may refuse partition table changes; check dmesg output for write protect errors.
  • Old/cheap USB media often presents as removable but fails after write:
    Buffer I/O error on device sdb1, logical block 1234
  • For batch provisioning: pre-label with device UUIDs for inventory tracking.
  • Udev rules can automate mountpoints post-format.

Formatting via Linux CLI is reliable—once device selection is triple-checked. Filesystem choice has downstream effects: file size limits, cross-OS mounts, and performance. GUI tools merely execute variations of these commands under the hood.

For further automation, integrate these steps into CI/CD post-image scripts when prepping golden master USBs for field deployment.


Note: No single approach is perfect. Alternatives like wipefs, sgdisk, or lsusb might be called for in specialized workflows—not detailed here.