How to create FAT32 USB drive on Linux

In short, steps to create USB drive on Linux are: delete all, create partition, set partition type, mark it as active and write changes to disk. After new partition is craeted then with mkfs command it’s only needed to create FAT32 file-system. This may sound scary, but the only tricky part is to use correct device name (in my case it was sdg).

When USB drive is plugged, kernel will write lines to the /var/log/message so it can be watched with “tail -f” or USB drive can be listed in “/dev/disk/by-id/” directory. Here is how to list symbolic links for mounted drives (drives can be listed by-path and by-uuid as well):

> ls -la /dev/disk/
> ls -la /dev/disk/by-id/

After USB drive was located, the next step is to delete all existing partitions. USB drive name should be checked twice because in your case it can be named differently. So, please be very careful otherwise you can lose valuable data from other partitions without (or minimal) chance to save them back. www.redips.net accepts no responsibility for any consequences if you choose to proceed.

1. First list existing partitions (here is example of my 4GB USB drive):

> fdisk /dev/sdg
Command (m for help): p

Disk /dev/sdg: 3874 MB, 3874488320 bytes
42 heads, 8 sectors/track, 22521 cylinders, total 7567360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006301d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1            2048     7567359     3782656    b  W95 FAT32

2. Now partition 1 can be deleted and it will not be listed any more:

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): p

Disk /dev/sdg: 3874 MB, 3874488320 bytes
42 heads, 8 sectors/track, 22521 cylinders, total 7567360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006301d

   Device Boot      Start         End      Blocks   Id  System

3. After USB drive is wiped out, next step is to create a new primary partition:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-7567359, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-7567359, default 7567359): 
Using default value 7567359
Partition 1 of type Linux and of size 3,6 GiB is set

Command (m for help): p

Disk /dev/sdg: 3874 MB, 3874488320 bytes
42 heads, 8 sectors/track, 22521 cylinders, total 7567360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006301d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1            2048     7567359     3782656   83  Linux

4. By default, partition type will have id 83 (Linux) and this should be changed to FAT32. Type t command and set partition id as b (all partition codes can be listed with L command):

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (W95 FAT32)

Command (m for help): p

Disk /dev/sdg: 3874 MB, 3874488320 bytes
42 heads, 8 sectors/track, 22521 cylinders, total 7567360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006301d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1            2048     7567359     3782656    b  W95 FAT32

5. Partition should be set as active or USB drive will not be attached to the file-system when plugged in. To set active partition type in a command and choose first partition number:

Command (m for help): a
Partition number (1-4): 1

Command (m for help): p

Disk /dev/sdg: 3874 MB, 3874488320 bytes
42 heads, 8 sectors/track, 22521 cylinders, total 7567360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006301d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1   *        2048     7567359     3782656    b  W95 FAT32

6. Write changes to disk and exit from fdisk utility

Command (m for help): w
The partition table has been altered!

7. Now FAT32 file-system can be created on USB drive from command line with mkfs (once again be sure that you typed in correct USB device name):

> mkfs -t vfat /dev/sdg1
mkfs.vfat 3.0.12 (29 Oct 2011)

8. Eject and reinsert USB drive, it should be empty and mounted. Here is df -hT output:

> df -HT
Filesystem     Type      Size  Used Avail Use% Mounted on
rootfs         rootfs     30G  5,6G   23G  20% /
devtmpfs       devtmpfs  1,8G     0  1,8G   0% /dev
tmpfs          tmpfs     1,8G  868K  1,8G   1% /dev/shm
tmpfs          tmpfs     1,8G  1,3M  1,8G   1% /run
/dev/sda1      ext2       30G  5,6G   23G  20% /
tmpfs          tmpfs     1,8G     0  1,8G   0% /sys/fs/cgroup
tmpfs          tmpfs     1,8G     0  1,8G   0% /media
/dev/sdb5      ext4      204G   41G  153G  22% /home
/dev/sdb1      ext4       20G  9,1G  9,7G  49% /var
/dev/sdb3      ext4      4,0G  137M  3,7G   4% /tmp
/dev/sdg1      vfat      3,7G  4,0K  3,7G   1% /run/media/dbunic/DF85-CAF4

3 thoughts on “How to create FAT32 USB drive on Linux

Leave a Comment