How to mount USB drive on Linux Systems.

Today tutorial will explain how to mount USB drive or a regular SATA drive in Linux system using terminal and shell command line.

How to mount USB drive on Linux Systems.



Step 1 - Detecting USB drive

After you plug in your USB device... Linux system will add new block device into /dev directory. Note that, at this stage , you will not be able to use this device as the USB filesystem needs to be mounted before you can retrieve or store any data. To find out the name of your block device run the following command sudo fdisk -l

NOTE: fdisk command required administrative privileges to access the required information, thus from this reason the commands needs to be executed as a root user or with sudo

The sample output should be like this –

Disk /dev/sdb: 7.21 GiB, 7739768832 bytes, 15116736 sectors
Disk model: ImationFlashDriv
Units: sectors of 1 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc3072e18

Device Boot Start End Sectors Size Id Type
/dev/sdb1 8064 15116735 15108672 7.2G b W95 FAT32

Take a note of the block device name of the partition you intent to mount. For example in our case that will be /dev/sdb with FAT32 filesystem.

Step 2 - Create a mounting point

We need to create a mount point. Mount point can be any new or existing directory within your system. We will use the mkdir command to create a new mount point directory where you want to mount your USB device:

sudo mkdir /mnt/usb-drive-name

Step 3 - Mounting the USB Drive

We are now ready to mount our USB's partition /dev/sdb1 into /mnt/usb-drive-name mount point as follow:

sudo mount /dev/sdb /mnt/usb-drive-name/

To verify whether your USB drive has been mounted correctly execute mount command again without any arguments and use grep to search for USB block device name:

sudo mount | grep sdb

/dev/sdb1 on /mnt/usb-drive-name type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,errors=remount-ro)

Step 4 - Formatting the USB Drive

You must unmount the device first to format the USB device, using the following command –

sudo umount /dev/sdb

Format vs Fat FileSystem

To format USB with vFat File System, use the following command –

sudo mkfs.vfat /dev/sdb

Format NTFS FileSystem
To format a USB Flash Drive with NTFS file system, use the following command –

sudo mkfs.ntfs /dev/sdb

Format EXT4 FileSystem
To format a USB with EXT4 file system, use the following command –

sudo mkfs.ext4 /dev/sdb

When you are asked to confirm your choice, just press "y" and wait as the selected partition would be wiped.

Step 5 - Accessing your USB Data

We can now access our USB data simply by navigating to our previously created mount point /mnt/usb-drive-name:

cd /mnt/usb-drive-name

Step 6 - Automount

In order to automatically mount a drive at boot time run the the following command to see the name of your drive, its UUID (Universal Unique Identifier) and file system type.

sudo blkid

/dev/mmcblk0p1: LABEL_FATBOOT="system-boot" LABEL="system-boot" UUID="B726-57E2" TYPE="vfat" PARTUUID="ab86aefd-01"
/dev/mmcblk0p2: LABEL="writable" UUID="483efb12-d682-4daf-9b34-6e2f774b56f7" TYPE="ext4" PARTUUID="ab86aefd-02"
/dev/sda: UUID="0d28feb1-b6c4-4788-a4db-22238b82f638" TYPE="ext4"
/dev/sdb: UUID="1a5e99de-399b-4d46-b8c6-ac2e51df1fe9" TYPE="ext4"

As you can see the UUID of our USB Drive /dev/sdb is 1a5e99de-399b-4d46-b8c6-ac2e51df1fe9 and ext4 the file system.

Edit /etc/fstab File


The fstab file typically lists all available disks and disk partitions, and indicates how they are to be initialized or otherwise integrated into the overall system's file system.
Run the following command to edit the /etc/fstab file.

sudo nano /etc/fstab

Now, we need to append one line of code at the end of the file.
The format of this line of code is as follows:

UUID=<uuid-of-your-drive> <mount-point> <file-system-type> <mount-option> <dump> <pass>

Note that you must separate these items with Tab key.

In our case, it will looks like this

Save and close the file and run the following command to see if it works.

sudo mount -a

Congratulations!
You now know "How to mount USB drive in a Linux System"