How To Make Bootable Usb In Linux

How To Make Bootable Usb In Linux

Reading time1 min
#Linux#Technology#OpenSource#BootableUSB#CommandLine#LinuxTips

Mastering the Command Line: Creating a Bootable USB in Linux Without GUI Tools

With Linux, GUI utilities for creating bootable USB drives—Etcher, UNetbootin, even dd wrappers—abound. The real control, however, is at the command line: faster, scriptable, and less error-prone (except for user error). Bulk imaging, automation, and field repairs are easier when you know exactly what’s happening.

Critical path: correct device identification. An incorrect target wipes the wrong disk.
Typical context: new distribution install, live rescue, or provisioning a lab of machines.


Requirements

  • Any modern Linux distribution; demonstrated here on Ubuntu 22.04 LTS (all steps equally valid on Arch, Fedora, Debian ≥10).
  • ISO (e.g., ubuntu-22.04.4-desktop-amd64.iso).
  • USB device (≥4GB, USB 2.0 or higher).
  • Familiarity with basic shell commands (ls, grep, dd, etc).

Note: Use a reliable USB 3.0 drive to reduce write time and verify physical hardware write lock status, if applicable.


Step 1: Identify the Target Device

Plug in your USB stick.
Run:

lsblk -o NAME,RM,SIZE,MOUNTPOINT,LABEL

or:

sudo dmesg | tail

to see the most recent device attached.

Typical output:

NAME   RM SIZE MOUNTPOINT LABEL
sda     0 500G           
├─sda1  0 250G /         
└─sda2  0 250G /data
sdb     1  16G           
└─sdb1  1  16G /media/alice/USB

Here, sdb is the entire USB block device (removable, RM=1). USB device names can change after reboots or re-plugs—never assume.
For double-checking (especially for devices with a manufacturer label):

sudo fdisk -l | grep Disk

Gotcha: Some systems assign /dev/nvme* or /dev/mmcblk* to built-in storage: triple-check.


Step 2: Unmount ALL Partitions on the USB Device

Unmount all device partitions—writing to a mounted device can corrupt data, and some desktop environments automount USB drives.

sudo umount /dev/sdb1

If there are multiple partitions (rare but possible):

sudo umount /dev/sdb*

Mistake: Only unmount, do not format or remove the partition table at this point.


Step 3: Write ISO Directly with dd

dd(1): the sector-level imaging tool of choice. Unforgiving: typos here may destroy system data.

Command:

sudo dd if=~/Downloads/ubuntu-22.04.4-desktop-amd64.iso of=/dev/sdb bs=4M status=progress oflag=sync

Parameters explained:

  • if=: Full path to ISO image.
  • of=: Target device; use the whole device (e.g., /dev/sdb) not a partition (/dev/sdb1).
  • bs=4M: Reasonable speed/memory trade-off. Adjust if you see "Input/output error" on old hardware.
  • status=progress: Ongoing byte counter and throughput.
  • oflag=sync: Syncs output after each write. When left out, sync is especially critical at end.

Typical output:

2170552320 bytes (2.2 GB, 2.0 GiB) copied, 48.3205 s, 44.9 MB/s

Finish with:

sync

This flushes OS write buffers.

Known issue: Some USB drives cache aggressively and don’t finish writes until physically ejected—wait for the drive activity LED to stop blinking before removal.


Verifying the Written Image (Optional but Recommended)

For critical deployments (or just to avoid field headaches), verify written bytes.

ORIG=~/Downloads/ubuntu-22.04.4-desktop-amd64.iso
TARGET=/dev/sdb
ISO_SIZE=$(stat -c%s "$ORIG")

sudo dd if=$TARGET bs=4M count=$(( ($ISO_SIZE + 4194303)/4194304 )) | \
  head -c $ISO_SIZE | sha256sum
sha256sum "$ORIG"

Match hashes manually.

Side note: Some ISOs (notably older EFI-enabled distributions) deliberately leave the USB with unallocated trailing blocks; ignore byte mismatch warnings for these, but hashes should still align across the image payload.


Scripting for Efficient Batch Imaging

When making repeatable flash drives (for classrooms, labs, or support field kits), scripting is practical—but parameter confirmation is crucial.

Sample interactive script segment:

#!/usr/bin/env bash
set -euo pipefail
read -erp "Path to ISO: " ISO
lsblk -o NAME,SIZE,MODEL | grep -v loop
read -erp "Target device (e.g., /dev/sdc): " DEV

if mount | grep -q $DEV; then
    echo "Unmounting $DEV partitions"
    sudo umount ${DEV}?*
fi

echo "About to write $ISO to $DEV. Continue? (type 'CONFIRM'): "
read -r CONFIRM
[[ $CONFIRM = "CONFIRM" ]] || { echo "Aborted." ; exit 1; }

sudo dd if="$ISO" of="$DEV" bs=4M status=progress oflag=sync
sync
echo "Done. Remove and test the USB on target hardware."

Note: This script prints attached block devices for identification, but device remapping can happen if multiple identical USB sticks are inserted—cross-check before proceeding.


Field Notes

  • If your USB fails to boot (e.g., BIOS reports “Missing operating system”), verify the ISO image itself is hybrid-bootable (file ~/Downloads/some.iso); some distros require post-processing with isohybrid or syslinux.
  • USB 2.0 drives are slower and can time out with dd using too high a block size.
  • When scripting, check exit status: dd does not always return nonzero on write errors if interrupted.
MethodProsCons
GUIUser-friendlyAbstracts device detail, slow
ddPrecise, scriptableUnforgiving to mistakes

Summary

Command-line USB imaging with dd is essential for engineers who want reliability, full control, and the ability to automate at scale. Provided the correct device is targeted and all partitions are unmounted, failure rates are near zero.

General principle: always double-check, especially when devices appear/disappear dynamically (e.g., when re-plugging during scripting)—the block device path can and will change without warning.


No GUI window required—just certainty and speed at the terminal. Properly executed, this process streamlines everything from single-node installs to mass provisioning.