Mount pendrives with commands in the Linux terminal
This is a small guide to detect a newly attached device to an USB port and mount it in Linux.
We will explore different strategies that will help when some of the tools isn't available.
First of all, some definitions:
What does exactly mount means?
To attach these new devices filesystems we use the
mount command in
mount -t type device dir.
In the above command, Devices (block special devices1) can be indicated in one of the following three ways:
- using the filename that is associated with the device.
- Filesystem label
- Using the label associated with the device.
- Universally Unique IDentifier (UUID) Uniform Resource Namespace 2.
To list the currently mounted devices/filesystems, findmnt.
For example, in Ubuntu
18.10 it shows:
There is also the old way, maintained for compatibility only but widely used: mount -l or just mount.
Now we have just connected a pendrive, how do we know which label or UUID it has to be able to mount the filesystem?
1. Identify the newly attached device
After we plug a pendrive, we need a method to locate the new device so we can get its label or UUID.
The following list shows the available alternatives to do it, any of them would be help you find the device information, listing most complete and easier to use first.
1.1 Using blk commands
The command lsblk prints all block devices (except RAM disks) in a tree-like format by default.
We can have a look at it to try to spot the new device with the
--fs to print info of each filesystem, LABELs
and UUIDs on available block devices.
To directly find out the connected pendrive, save the above listing in a temporal folder and then look for the differences with the same command after plugging the pendrive:
1.2 Inspect Kernel ring buffer
Immediately after plugging the device, we can examine the kernel ring buffer with the command dmesg
There we can look for the string of type
sdb, etc, that
will belong to the most recent connected device.
In this case we can spot the
sda: sda1 sda2 line that indicates it
has two partitions:
Then we use the command blkid -p device to find out its UUID, label and more properties.
1.3 dev-by- directories
Inspect the directories:
And a similar approach can be done to know which one was plugged in, saving the list before and after plugging the device:
2. Mount the device
Which method should we use?
The device name of disk partitions are unstable.
They depend on what (physical) slot you connect the device, and if there are other devices already attached or not, so the filename to refer to them may change over time.
Labels are more stable than filenames, but they may change or have the same name as other label.
The best way to mount a device is using the UUID, as you can be sure it won't change over time.
We will mount it at
Create directory: sudo mkdir /media/usb-stick
And mount it with one of these methods, preferably UUID:
sudo mount -t auto /dev/sda2 /media/usb-stick/ # mount using label # mount -L label sudo mount -L Ubuntu 18.10 # mount using UUID # mount -U uuid sudo mount -U BXXX-6XXX
3. Make it permanent
There is a special file
/etc/fstab, that each line describes:
- what devices are usually mounted
- using which options
After reboot each line will be mounted automatically if the device is connected.
As we are working mostly with pendrives, we should use the special option: nofail. This avoids reporting any errors for the device if it does not exist at booting time when it tries to mount them and probably the device won't be plugged..
Here we use the UUID to be sure we always refer to the same device.
# pendrive negro kingston 2gb UUID=2018-XX-XX-XX-44-30-00 /media/usb-stick auto defaults,nofail
3.1 Mount a pendrive just by specifying a directory
fstab knows that our USB-stick should be placed at
/media/usb-stick, we can mount the pendrive just by using that
directory the next time we plug the pendrive:
Personally, I like to have each pendrive or device identified like
/media/kingstone-2gb so I can easily mount it with the directory
name after plugging it.
4. Remove pendrive
When you want To finally remove the USB-stick, you need to release
it from the filesystem first with
sudo umount /media/usb-stick
Set pendrive label
To set create or rename the label of a pendrive there are several programs:
- e2label - Change the label on an ext2/ext3/ext4 filesystem
- tune2fs - adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems
- mke2fs - create an ext2/ext3/ext4 filesystem
To set a pendrive label as usb-stick
e2label /dev/sda1 usb-stick tune2fs -L usb-stick /dev/sda1
Or create filesystem:
mke2fs pubsw /dev/hdb1
And change the label
mke2fs -L pubsw /dev/hdb1
Summarizing the commands used above in a single list:
# find filesystem / list mount points findmnt # print info about all block device filesystems lsblk --fs #look for UUID mount -t auto -U XXXXXXXXXXX
block special file: A block special file is normally distinguished from a character special file by providing access to the device in a manner such that the hardware characteristics of the device are not visible. http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_79 ↩︎
- Detect and mount USB devices in Linux from console
- How to make screencasts in Ubuntu Linux
- Using i3 window manager in Linux
- Setting Up A Fresh Linux Server
- How To Download A Website With Wget The Right Way
- Replicate Installed Package Selections From One Ubuntu System To Another
- Using Clamav Antivirus In Ubuntu
- How to Type Spanish Characters, Accents and Symbols in Linux
- Solving Google Chrome's gpu-process error message in Ubuntu Linux
- Solving Google Chrome's secret service operation error message in Ubuntu Linux
- Start Emacs In Ubuntu The Right Way
- Add Infolinks Script To An Existing Website From Console With Sed Command
- How to change all files permissions to 644 and directories to 755
- Shell Redirect Output And Errors To The Null Device In Bash
- Prevent Running Of Duplicate Cron Jobs
- Delete All Backup Files Recursively In Bash
- Bash Script to Find Out If MySQL Is Running Or Not