How Can We Help?
How to mount USB drive in Linux
Objective
The following tutorial explains how to mount USB drive in Linux system using terminal and shell command line. If you are using desktop manager, you will most likely be able to use it to mount USB drive for you. Mounting a USB drive is no different than mounting a USB stick or even a regular SATA drive.
Conventions
- # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
- $ – requires given linux commands to be executed as a regular non-privileged user
Detecting a USB hard drive
After you plug in your USB device to your USB port, Linux system adds a new block device into /dev/ directory. At this stage, you are not able to use this device as the USB filesystem needs to be mounted before you can retrieve or store any data. To find out what name your block device file have you can run the fdisk -l command.
NOTE: fdisk command requires administrative privileges to access the required information. For this reason the command needs to be executed as a root user or with the sudo prefix:
# fdisk -l
OR
$ sudo fdisk -l
Upon executing the above command you will get an output similar to the one below:
Disk /dev/sdc: 7.4 GiB, 7948206080 bytes, 15523840 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
Disklabel type: dos
Disk identifier: 0x00000000
Device Boot Start End Sectors Size Id Type
/dev/sdc1 * 8192 15523839 15515648 7.4G b W95 FAT32
The above output will most likely list multiple disks attached to your system. Look for the USB drive based on its size and filesystem. Once ready, take a note of the block device name of the partition you intend to mount. For example /dev/sdc1 with FAT32 filesystem.
Create a Mount Point
Before we are able to use the mount command to mount the USB partition, we need to create a mount point. A mount point can be any new or existing directory within your host filesystem. Use the mkdir command to create a new mount point directory where you want to mount your USB device:
# mkdir /media/usb-drive
Mount the USB Drive
At this stage we are ready to mount our USB’s partition /dev/sdc1 into the /media/usb-drive mount point:
# mount /dev/sdc1 /media/usb-drive/
To check whether your USB drive has been mounted correctly execute the mount command again without any arguments and use grep to search for the USB block device name:
# mount | grep sdc1
/dev/sdc1 on /media/usb-drive type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=utf8,shortname=mixed,errors=remount-ro
If no output has been produced by the above mount command your USB partition is not mounted. Alternatively, double-check whether you have used a correct block device name in the above command.
Access the USB Data
If all went well, we can access the USB data simply by navigating to our previously created mount point /media/usb-drive:
# cd /media/usb-drive
Unmount the USB
Before we are able to unmount the USB partition we need to make sure that no process is using or accessing the mount point directory; otherwise we will receive an error message similar to the one below:
umount: /media/usb-drive: target is busy
(In some cases useful info about processes that
use the device is found by lsof(8) or fuser(1).)
Close your shell or navigate away from the USB mount point and execute the following linux command to unmount your USB drive:
# umount /media/usb-drive
Permanent Mount
In order to mount your USB drive permanently after reboot add the following line into your /etc/fstab config file:
/dev/sdc1 /media/usb-drive vfat defaults 0 0
However, the above mount line may fail if you add or remove additional drives from your Linux system. From this reason it is recommend to use partition UUID instead of a raw block device name. To do so, first locate a UUID of your USB drive:
# ls -l /dev/disk/by-uuid/*
lrwxrwxrwx 1 root root 10 Mar 27 23:38 /dev/disk/by-uuid/2016-08-30-11-31-31-00 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Mar 27 23:38 /dev/disk/by-uuid/3eccfd4e-bd8b-4b5f-9fd8-4414a32ac289 -> ../../sda1
lrwxrwxrwx 1 root root 10 Mar 27 23:38 /dev/disk/by-uuid/4082248b-809d-4e63-93d2-56b5f13c875f -> ../../sda5
lrwxrwxrwx 1 root root 10 Mar 28 01:09 /dev/disk/by-uuid/8765-4321 -> ../../sdc1
lrwxrwxrwx 1 root root 10 Mar 27 23:38 /dev/disk/by-uuid/E6E3-F2A2 -> ../../sdb2
Based on the above ls command output we can see that the UUID belonging to block device sdc1 is 8765-4321 thus our /etc/fstab mount line will be:
/dev/disk/by-uuid/8765-4321 /media/usb-drive vfat 0 0
Run a mount -a command to mount all not yet mounted devices.
# mount -a