Most modern Linux and Unix distros are mostly plug and play when it comes to using data storage devices like the USB flash drive or an external hard drive. Some of the desktop environments like the KDE or Gnome will also auto mount the device when it is plugged in and make it writable as well.
That being the case, if you have an older system then the system may not recognize any of the data storage devices unless it is explicitly mounted. Another situation is when you are logged in remotely into a machine using ssh or telnet or if your machine does not have a desktop environment installed, then knowing how to mount and read from a USB flash drive might be helpful.
Another scenario where this comes quite handy is when your KDE or Gnome would not start up for any reason and you have to get some configuration files or packages copied and installed from the console to get the environment to run again. If you run the unstable testing version of the Gentoo distribution while compiling all the bleeding edge softwares willy-nilly, you will have this scenario more often than you think.
Required Permissions for mounting the USB drive
In order to mount an USB flash drive and for that matter any data storage device, you need to have the appropriate permissions. The easiest way to make sure that you have all the required permissions is to use the superuser credentials. If you prefer not to use the superuser account or do not have access to the super user, then you will have to make sure that you have the permissions to access and mount the USB drives on the machine.
If there is going to be only a specific number of flash drives that will be used at one time and also if you know the specifics of the drive then adding them to the fstab configuration will enable it to be auto-mounted when plugged in. But, if you want permissions to mount the drive manually as an user, then it is a little more complicated. It involves playing around with the sudoers configuration files (eg. /etc/sudoers) and is beyond the scope of this particular post.
The easiest way as I mentioned earlier is to use the super user credentials. If you are not the super user, then contact the administrators to grant you permissions.
Finding the device name
Next, you need to find if the plugged in device has been detected by the system and if so, which device name the USB flash drive is attached to. There are several ways to find this as explained below.
lsusb: This is a quick handy command that lets you verify that the flash drive was detected by the machine. This does not exactly show you the device name that you are looking for, but is a quick way to see if the drive is detected. In the output of the command, look for the flash drive name and description. If you are unable to find or identify it, then remove the drive and execute the lsusb command. Then plug in the drive, execute the command again and compare the output to the previous one.
/proc/scsi/scsi: Another method to check if the drive is identified correctly is to check the /proc filesystem for scsi devices. You can view all the currently identified SCSI devices by printing out the /proc/scsi/scsi.
bash$ cat /proc/scsi/scsi
outputs something like :
If your device is still not getting identified, then your best bet is to start a scan of all scsi devices. You can do this with the rescan-scsi-bus command, and your device should be identified.
fdisk -l: This command lists all the partitions on all the devices. You should be able to identify the device name from the output of the command. Look for something like /dev/sdc1 or /dev/sdd1 towards the end of the output.
dmesg | grep sd: This command will print out the attached devices on /dev/ with the prefix sd, which is usually the default. You are basically looking for lines which says something like “Attached SCSI removable disk“, so a more specific grep expression might be dmesg | grep SCSI.
From this output, you will see that the device name we are looking for is /dev/sde which was the last USB drive or a removable disk to be plugged in.
Mounting the device
Once you have successfully identified the device name, then you need to mount the file system on the device so that you can start accessing it. In order to do that, you need a mount directory or a mount point as it is called. This can be any directory on your file system or you can create new one for this. Here we create a new directory called myusbdrive.
bash$ mkdir /mnt/myusbdrive
Now we can mount the device file system to this folder using the mount command. The general syntax of the mount command is “mount something somewhere“. So, to mount the usb device to myusbdrive folder,
bash$ mount /dev/sde1 /mnt/myusbdrive
If the filesystem on the usb drive is identified by the system, then it will use that to load it. Sometimes, the system cannot identify the file system format correctly or you want to force the system to use a particular FS format. You can specify almost any of the file system formats like vfat, ext3, reiserfs etc while you mount the drive
bash$ mount -t vfat /dev/sdc1 /mnt/myusbdrive
Once the device has been mounted, it can then be used just as any other Linux folder or directory. You can copy, delete and create files into the drive just as you would do with a directory. Once you have completed the work on the USB flash drive or the data storage device, you want to unmount it gracefully so that you can remove the flash drive. This is similar to the eject function in Windows.
Also, remember that it is possible to mount the same device onto multiple locations.
Unmouting the drive
To unmount the device, you can use the umount command to do so. You cannot un-mount the file system if it is in a ‘busy‘ state such as a file being open or a copy or delete command is still executing.
bash$ umount /mnt/myusbdrive
You should always un-mount using the mount point, the directory where the device was mounted. Specifying the device name (eg; /dev/sdc1) will also work, but it will fail if the device have been mounted to multiple directories.