Formatting a MicroSD Card in Linux
Before you can use a MicroSD card with the Raspberry Pi, it needs to be formatted with at least one FAT32 partition.
Step 1: Check the Partition Table
Plug in your MicroSD card with its adapter, but do not mount any of the file systems on it. If your desktop environment automatically mounts and opens any windows, close the windows it has opened.
Start your command terminal, and become root using:
doas su -
If your distribution uses sudo instead, use:
sudo su -
Now figure out the device node of your MicroSD card by running:
dmesg
As long as you haven’t plugged in any additional removable devices, the device node should be toward the bottom of the dmesg output. On my system, the device is sda. However, your device might be sdb, sdc, or so forth, especially if you have a SATA hard drive in your computer. It is critical to get the device node right, otherwise you risk destroying your system!
To check the partition table, run:
fdisk -l /dev/sdX
Replace sdX with your device node.
The bottom of the output should look something like this:
Device Boot Start End Sectors Size Id Type /dev/sda1 2048 48410623 48408576 23.1G c W95 FAT32 (LBA)
If you have a single partition of type “W95 FAT32 (LBA),” then proceed to Step 3. Otherwise, you need to repartition the card, so go to Step 2.
Step 2: Repartition (if needed)
If you need to repartition your card, start fdisk like so:
fdisk /dev/sdX
Replace sdX with the correct device node if it is different! The fdisk program is a text-based program that uses commands to make partitioning changes on a device. Do the following:
- Type p and press Enter to show the starting partition table. Be sure that it matches what you saw in the previous step. If not, type q and press Enter to exit fdisk. Check that your device node was correct – you do not want to destroy your operating system drive by accident!
- Type o and press Enter to create a new DOS-style partition table.
- Type n and press Enter to create a new partition in the new table.
- We want a primary partition, so type p and press Enter.
- This new partition will be partition number 1 (so type 1 and press Enter).
- Just press Enter to accept the default starting sector (2048). This starting sector aligns the partition with the storage device for performance reasons.
- Press Enter again to accept the default ending sector, which will make the partition use the entire drive.
- If you are asked a question like “Do you want to remove the signature?”, answer y for yes.
- The new partition should have been created. Type p and press Enter to show it.
- The Raspberry Pi requires that the first partition contain a FAT, or MS-DOS, file system in order to be able to boot. We need to change the partition type away from a Linux partition for this purpose. Run the t command to change the partition type.
- Partition 1 should be selected automatically.
- For the type code, enter c and press Enter. This is the partition type code for “W95 FAT32 (LBA),” which is the one we want.
- Verify the new partition table one more time with the p command. You should have a single partition of type W95 FAT32 (LBA). If something doesn’t look right, go back to #2 and recreate your table.
- To save the partition table and exit fdisk, run the w command.
Step 3: Format the File System
Once you have a single partition of type “W95 FAT32 (LBA),” run the following command to format it:
mkfs.vfat -n SDCARD /dev/sdX
Replace sdX with the device node of your device!
Step 4: Reconnect Your Device
On some desktop environments (like KDE Plasma), you might see a notification that your newly formatted file system is available to open. On other desktops, unplugging your MicroSD card reader (or card, in the case of a built-in reader) and plugging it back in will let you access the new file system.
You can now put Raspberry Pi-specific files onto this MicroSD card, provided that whatever you’re trying to put on there can work with this single partition. For advanced use cases, you might want to use the Raspberry Pi Imager instead.