How to Mount USB Drives in Linux

Configurare noua (How To)

Situatie

To use our USB Device, first, we need to identify it. Now there are a couple of different commands which can be used here. However, we will go with trusty old fdisk in command line which lists our USB Drive along with its model name, the drive’s capacity, and much more!

Once the USB drive has been plugged in, it will be registered as a new block device in / dev/ directory ( Remember that everything in Linux is a file!). To list all block devices, we can type:

$ lsblk

This should return something like follows :

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 929G 0 part /var/lib/lxd/storage-pools/default
└─sda3 8:3 0 2G 0 part [SWAP]
sdb 8:16 1 57.3G 0 disk
└─sdb1 8:17 1 57.3G 0 part

Here sda is the primary block device whereas sda1 sda2 and sda3 are the various partitions where the components of the running Operating System are located. Next comes sdbwhich represents our USB Drive as a block device. Likewise, sdb1 denotes the partition on the USB Drive in our case.

If you have multiple hard disks installed, it will be different. From the above output, we can also see the size of various partitions and their mount points. As one can see, our USB Drive is not mounted, but we’ll fix that in a minute.

Note that the name of our USB block device may be something other than sdb. Still, it’s almost always of the form sdX, where X usually is a smaller case alphabet like ‘b’, ‘c’, ‘d’, and so on but is seldom ‘a’ as it usually denotes the primary block device containing the Operating System. Also

Moving on, we can identify our USB Drive using the following :

$ sudo fdisk -l

In the output, we should get an output like this :

Disk /dev/loop0: 49.9 MiB, 52260864 bytes, 102072 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 /dev/loop1: 55.6 MiB, 58310656 bytes, 113888 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 /dev/loop2: 339.9 MiB, 356356096 bytes, 696008 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 /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: E3C13307-58B0-4C45-AD3F-DF4C1449D7D4

Device Start End Sectors Size Type
/dev/sda1 227328 20971486 20744159 9.9G Linux filesystem
/dev/sda14 2048 10239 8192 4M BIOS boot
/dev/sda15 10240 227327 217088 106M EFI System

Creating A Mount Point

Now that we have identified which Block Device we want to mount, we would need a place to mount it to. Usually, we make a folder under /mnt directory using the mkdir command:

$ sudo mkdir /mnt/USB

With this, we finally have a place to mount our block device. Onto the next step!

Mount The Block Device

Finally, we can now mount our USB Drive in the folder we made earlier! This can be easily done via the mount command. Following our example, remember that we had our USB Drive registered as the block device /dev/sdb? Remember how it had a partition /dev/sdb1? We cannot mount block devices, rather we shall mount this partition as follows:

$ sudo chown /dev/sdb1 /mnt/USB

And now, we can access the contents of our USB Drive right as if it were just another folder on our Desktop. We can also change the ownership of the folder where we mounted our USB Drive as such :

$ sudo chown $USER /mnt/USB/

After the above command, you no longer need to type sudo repeatedly whenever we move data to-and-from our USB Device! Just to validate that our USB Drive has been mounted correctly, we can return it to our good friend lsblk and it should show us an output like:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 512M 0 part
├─sda2 8:2 0 929G 0 part /home
└─sda3 8:3 0 2G 0 part [SWAP]
sdb 8:16 1 57.3G 0 disk
└─sdb1 8:17 1 57.3G 0 part /mnt/USB

As we can see, our USB Drive is mounted in the folder we previously created ( /mnt/USB ); hence, we have successfully mounted our USB Drive!

Bonus: How To Unmount USB Drive?

Once we finish all of our desired USB operations, unmounting our USB Drive before physically removing it from our machine is good practice. This can be achieved via the unmount command that umounts media attached to the system. Just simply type in:

$ sudo umount [FOLDER WHERE DRIVE IS MOUNTED]

Staying consistent with our previous example, this would translate to:

$ sudo umount /mnt/USB

With that, we are done and free to remove our physical media like usual!

Solutie

Tip solutie

Permanent

Voteaza

(2 din 5 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?