19
votes
Formatting USBs with live Linux... what's the right way?
I hope I am blind to something here... but every time I want to wipe a USB that had a linux distro on it (Mint, in this case) I have to hop into Windows and use DISKPART. Just now the stupid thing didn't say it was read only (by flags), but I had to clear that anyway before it would clean
I am wiping this to create a macOS installer. I tried to clear it on MacOS, EndeavourOS (with partitionmanager), and Windows (Explorer before DISKPART)
All this said, I love DISKPART and I will never speak ill of it. Its simple and effective and just works.
You can do as root or via sudo:
dd if=/dev/zero of=/dev/sdX bs=1M count=1
where sdX stands for your USB flash (ie. sdc). Don't mess that up or you can destroy data on any of your disks in your PC! You won't be asked ANYTHING when you execute the command, it will get done!
This basically deletes the partition table of the disk, which in turn makes it completely blank (well, data is still written...). You have to partitipn and format it after doing that. You can use parted, fdisk or cfdisk to partition and then:
mkfs.fat -F 32 /dev/sdX#
where sdX stands for drive and # is the number of partition you want to format (ie. sdc1). You can use mkfs.exfat if you need exFAT instead of FAT32.
If you have anything to ask, do so. You want to be sure before you do anything.
FYI, you don't actually need
ddfor this. You can just runcat /dev/zero > /dev/sdX(and burn ISOs withcp file.iso /dev/sdX). Unless you actually need something that requires specific block sizes, which you usually don't, you don't need dd.https://www.vidarholen.net/contents/blog/?p=479
You can even run
curl ... > /dev/sdXto just directly flash an ISO from a URL!https://astrid.tech/2026/03/24/0/curl-to-dev-sda/
cc @archevel, @tomf
The
count=1in grandparent does save you from writing to the entire USB drive, which the suggestedcatcommand doesn't do.Only zeroing out the first megabyte is sufficient to save yourself some warnings in partition editors while saving one write cycle over the bulk of the drive, and also saves a few seconds to minutes of waiting depending on the size and speed of things.
As Vidar's post mentions, not all uses of
ddare useless.You should always check your sums, though, especially with large downloads and especially with security exploits landing so frequently in this pre-singularity age.
Nice approach!
I just described how I would do it. You can do it in many ways, obviously.
Nice! I didn't know that worked. I would caution against piping curl output directly for the same reason you really shouldn't use it to pipe things into
shwhich seem to be popular for some tools as an install method. I prefer downloading and a quicksha256sumto verify. But, nice to know anyway!ddalso hasstatus=progress, which can be nice for slow flash drives. It also has handy flags for syncing, otherwise you think you're done with the copy then have to wait ages for your eject to complete.pvis probably my favorite replacement, as it has both progress built-in and a--syncflag.ok! yeah -- that totally makes sense. I don't get why the GUIs can't do the same. Excellent write up!
Does gparted not work or come installed with the Live USB?
probably -- but this is clearing the stick to use it for something else :)
GUIs can't do many things because even though they pften use the same commands in the background, their developer didn't add all the possibilities of the said command-line program.
I used some GUI programs back in the day to do video re-encoding (no editing, just encode to different format) and since some didn't allow for custom bitrate, some didn't allow for custom resolutions, some didn't allow for specific codecs... I just learned how to use ffmpeg in command line. Since then, I don't bother with GUI programs when I just need to re-encode video.
I learned a lot since my beginning days with Linux. And the most useful thing I learned is to use command line when possible.
If I just need an install medium and not a portable USB where I can store things I usually just do:
And it's ready to go. No need to zero anything out unless you are using an old drive with sensitive info on. This is only useful if you don't intend to use it as a storage device though and only as an install media.
When you need to make bootable USB stick, your command is the easiest way. It will dump whatever is on the stick and make it bootable OS one.
I suppose my instructions could be called "get such USB stick back to being mass storage".
This certainly works and is the traditional way of doing it, but it’s such a dangerous command, it’s a single character difference between setting up a Linux usb and formatting your computer.
These days we have GUI programs which make it clear you are targeting a 32gb external usb rather than “sdb1”. Personally I use the raspberry pi imager app but there are many other similar programs.
That's why I said user must be certain what they are doing. It is fast way to hell! And to heaven as well! The destination lies in that one character.
This is what I love about Linux command line - you execute it, it gets done. It can nuke your PC or even server, but it just gets done.
With great power comes great responsibility.
Ventoy is really worth checking out as a modern way for bootable usbs. You just copy the iso on the ventoy partition of the usb and it works. Supports both windows and linux isos.
https://www.ventoy.net/en/index.html
This is the best one right here. I tried it once and I'm not going back.
I have gotten a lot of good use out of gparted although admittedly not in the past 5-10 years. I have always used it with the Linux system rescue as a bookable CD, which probably dates my usage. It's a great tool for expanding Windows partitions without reinstalling Windows. I've even used it to copy a windows partition onto a new drive and then expand it. I like the visual aspect because it's easier for me to be sure that I'm doing what I think I'm doing. Also a lot of the operations you can stack up and visualize before you actually commit to the operation.
If it often mounts RO, it’s possible the stock’s flash is failing and the internal controller blocks any writes to it. Doesn’t happen very often but if the stick is old and has been used a lot, it’s a possibility.
Mint has built-in software that should let you do it:
I swear by https://github.com/ifd3f/caligula, it's a whole lot nicer and simpler than
dd.Wouldn't Rufus be able to do it? You can tell it to not flash anything on it and just format as FAT32.
I haven't touched any other disk utility since Rufus came out (but tbh I also try out fewer and fewer distros as time goes on, Fedora and Zorin are good enough for my purposes).
in this case I am wiping it but not for a distro. Rufus or Etcher would likely do it, though.
This creates a new GPT partition table with a linux partition:
Then you can do mkfs on /dev/sdX1.
You could also do
sudo wipefs -a /dev/sdXto clear the device but the above--zap-allalready includes this step. If you havesgdiskyou don't really needwipefs.If you want a bootable USB just do:
And this should do all that you need to do for you as long as you used /dev/sdX and not /dev/sdX1.