- Detect and mount USB devices in Linux from console
- Overview
- Understanding
- Listing
- 1. Identify the newly attached device
- 1.1 Using blk commands
- 1.2 Inspect Kernel ring buffer
- 1.3 dev-by- directories
- 2. Mount the device
- 3. Make it permanent
- 3.1 Mount a pendrive just by specifying a directory
- 4. Remove pendrive
- Optional
- Set pendrive label
- Summary
- Resources
- Articles
- Subcategories
- Linux Terminal and Console Explained For Beginners
- Terminal is An Interface Between Human and Computer
- The First Terminal
- Character Terminal vs Graphics Terminal
- Linux Terminal Emulators
- What is Console?
- Virtual Console
- What is a Shell
- The Distance Between Terminal and Host
- Wrapping Up
Detect and mount USB devices in Linux from console
Mount pendrives with commands in the Linux terminal
Overview
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:
Understanding
What does exactly mount means?
All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the filesystem found on some device to the big file tree. Conversely, the umount(8) command will detach it again. The filesystem is used to control how data is stored on the device or provided in a virtual way by network or another services.
To attach these new devices filesystems we use the mount command in the form: mount -t type device dir .
In the above command, Devices (block special devices 1 ) can be indicated in one of the following three ways:
- Filename
- using the filename that is associated with the device.
- e.g.: /dev/sdb2
- Filesystem label
- Using the label associated with the device.
- e.g.: PENDRIVE
- UUID
- Universally Unique IDentifier (UUID) Uniform Resource Namespace 2 .
- e.g.: ba108o135-80bf-1cci-b2za-082eafd02y0g
Listing
To list the currently mounted devices/filesystems, findmnt (find a filesystem).
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 (flash drive), 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 filesystem parameter: —fs to print info of each filesystem, LABELs and UUID s 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 sda , or 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: sda1 and sda2 .
Then we use the command blkid -p device to find out its UUID , label and more properties.
1.3 dev-by- 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?
Device names depend on which (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.
We will mount it at /media/usb-stick .
For this, create the directory: sudo mkdir /media/usb-stick
And then mount it with one of these methods, preferably UUID :
3. Make it permanent
There is a special file /etc/fstab , in which each line describes:
- what devices are usually mounted,
- where,
- 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..
3.1 Mount a pendrive just by specifying a directory
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
sudo umount /media/usb-stick
Optional
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
Or create filesystem:
And change the label
Summary
Summarizing the commands used above in a single list:
Resources
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 ↩︎
A Universally Unique IDentifier (UUID) URN Namespace https://tools.ietf.org/html/rfc4122 ↩︎
Marcelo Canina
Articles
- Adding a swap memory to Linux from command line in 6 steps April 2, 2020
- Free up space in Linux (Ubuntu) March 27, 2020
- Switch between languages in Linux with xkb. Write in multiple languages with same keyboard. March 21, 2020
- How to make Ubuntu display emojis February 12, 2020
- Detect and mount USB devices in Linux from console
- How to make screencasts in Ubuntu Linux January 21, 2019
- Using i3 window manager in Linux January 7, 2019
- Setting Up A Fresh Linux Server August 25, 2018
- How To Download A Website With Wget The Right Way June 30, 2017
- Replicate Installed Package Selections From One Ubuntu System To Another April 24, 2017
- Using Clamav Antivirus In Ubuntu January 25, 2017
- How to Type Spanish Characters, Accents and Symbols in Linux June 6, 2016
Subcategories
Ubuntu
- How to activate tap to click touchpad’s feature in Ubuntu in 4 steps March 4, 2021
- Difference between suspend and hibernate in Ubuntu and how to execute them from command line April 12, 2020
- Solving Google Chrome’s gpu-process error message in Ubuntu Linux January 7, 2019
- Solving Google Chrome’s secret service operation error message in Ubuntu Linux January 7, 2019
- Start Emacs In Ubuntu The Right Way June 10, 2017
Unix Shell
- Connect to a Bluetooth device from command line in Ubuntu Linux June 23, 2020
- Add Infolinks Script To An Existing Website From Console With Sed Command April 4, 2017
- How to change all files permissions to 644 and directories to 755 January 10, 2017
- Shell Redirect Output And Errors To The Null Device In Bash December 9, 2016
- Prevent Running Of Duplicate Cron Jobs December 8, 2016
- Delete All Backup Files Recursively In Bash November 28, 2016
- Bash Script to Find Out If MySQL Is Running Or Not November 9, 2016
A comprehensive guide to manually mount a USB pendrive in Linux.
Источник
Linux Terminal and Console Explained For Beginners
Terminal is the interface through which you type cryptic Linux commands, but is terminal just a window with command prompt on your Linux desktop? Well, the fact is, the terminal you are using now is probably not a real terminal. In this article, I will be explaining the real meaning of Linux terminal and console. Understanding this basic concept is important if you want to master Linux.
Terminal is An Interface Between Human and Computer
As a noun, terminal has many meanings. The Merriam-Webster dictionary gives us a good definition of terminal in the computing field:
a combination of a keyboard and output device (such as a video display unit) by which data can be entered into or output from a computer or electronic communications system.
Human and computer are two independent entities. Human need an interface to put information into the computer and read output from it. These days, input device of personal computers includes: keyboard, mouse, and microphone. Output device includes monitor and speaker. They are commonly called peripheral devices. But in the Unix and Linux world, input and output devices are called terminal. The opposite of terminal is the host, which includes devices like CPU, RAM, HDD and so on.
So now we have two equations.
The First Terminal
Personal computers came into daily lives in the late 1970s. Before then, we can only use expensive mainframes and minicomputers in big companies or universities. The PDP-7 minicomputer used by Ken Thompson and Dennis Ritchie (The two creators of Unix) cost 72,000 US dollars in 1965, and the GE-45 mainframe cost 10 million US dollars.
Ken Thompson and Dennis Ritchie wanted to make Unix become a multi-user operating system, which means each user would have a terminal to connect to the computer. Each user needs a monitor and keyboard. However, every computer devices, including monitor, were very expensive in those days. And keyboards were physically attached to computers. There was no detachable keyboards then.
So they found an alternate solution: use the cheap ASR33 teletype as terminal. Teletype (TTY) was mainly used to send and receive typed messages in telegraphy, but they can also be used to put information into computer and get information out from it. You use the keyboard to input information and print output on paper.
Many teletypes were connected to one Unix computer. Every user can log into Unix with their own username and password, sitting in front of their own teletypes. So Unix became the first multi-user operating system and ASR33 became the first Unix terminal. Due to this fact, the acronym TTY is commonly used to represent terminal in Unix/Linux.
Over the years, terminals became more and more advanced. In 1970, DEC (Dgital Equipment Corporation) introduced the VT05 video terminal. It has a monitor with a video screen for output.
VT05 video terminal
Character Terminal vs Graphics Terminal
Character terminal is also known as text terminal, which can only output text-based information. Teletypes are character terminals. The most famous character terminal is the VT100 made by DEC in 1978. It’s designed so well that it became a standard for later terminals.
Graphics terminal is better because it can also show graphics/pictures on the screen. The most famous graphics terminal is X Terminal, which provided the basic support for the X window system.
Linux Terminal Emulators
Now you know terminal is hardware. In the 1970s and 1980s, people type commands sitting in front of a real terminal. These days we often type commands in a software terminal, which is better known as terminal emulator or terminal emulation application. Sometimes people call them virtual terminal. They are not real terminals, but the operating system thinks they are.
If you use Gnome terminal, you can run the following command to read the manual of gnome-terminal .
It tells you that gnome-terminal is a terminal emulation application. It emulates Xterm.
Xterm is also a terminal emulator. It emulates the VT102 terminal made by DEC. Now you can still find Xterm in your Linux distribution.
What is Console?
In the 1970s and 1980s, terminal are often connected to the host by wire, but there’s a special terminal that is attached to the host. This special terminal is called console. It is used by administrators only. Each computer has only one console. There’s not much difference in appearance between standard terminal and the console, but console can do something that standard terminals can not do. For example, if the operating system failed to boot up, there would be error message displayed on the console, but not on other terminals. And if the operating system boots into single-user mode, you can only use console to login. Other terminals have no permission to login in single-user mode.
PDP-7 minicomputer console
Console and terminal are often seen as synonyms on personal computers. Your monitor and keyboard are the console, and also terminal. You can switch between a standard user and an administrator at any time.
Virtual Console
As a matter of fact, there are two types of terminal emulator. The first type is terminal window you open in a desktop environment. The other type is virtual console. In the 1970s and 1980s, people can often use only one terminal. Heavyweight people in a company or university can use multiple terminals, because they need to do many things with the operating system. So there would be several terminals on their desks.
Nowadays, we don’t need to put multiple terminals on the desk, because Linux can create multiple virtual terminals. One of them is graphics terminal, the other six is character terminal. The 7 virtual terminals are more commonly known as virtual consoles and they use the same keyboard and monitor. Physical console is the combination of your monitor and keyboard. When Linux boots up, it creates the 7 virtual consoles and by default brings you to the graphics console, i.e, the desktop environment. You can run the who command to check which virtual console you are using.
As you can see, I’m using the 7th virtual console (tty 7). If we want to switch to a character virtual console, simply press Ctrl+Alt+ F1 , or Ctrl+Alt+ F2 …. Ctrl+Alt+F6 . To switch back to the graphics virtual console, press Ctrl+Alt+F7 .
When is virtual console useful? If, for some reason, your desktop environment doesn’t respond or the icons/taskbar on your desktop suddenly disappear, you can switch to a character virtual console, and type commands to diagnose the problem.
Note: On Fedora, tty1 is the default graphical virtual console.
What is a Shell
Terminal is just a mechanism to transfer information. For the operating system to understand the information, a shell is needed. A shell in Linux is a program that interprets the commands you enter in a terminal window, so the operating system can understand what you want to do.
There are many shell programs, such as Bash, Zsh, Csh, Ksh etc. Bash is the default shell on most Linux distributions. When you open a terminal window, a Bash shell is automatically started. To check which shell you are using now, run the following command.
The Distance Between Terminal and Host
Terminals can be directly connected to a host on a laptop computer. Terminals can also be connected to a host by wire like a desktop computer. Actually, terminals can also connect to a host over the network. For example, if you have two computers in a LAN. One of them run Linux. You can use the other computer to log into the Linux computer using SSH, or VNC. Thus, the Linux computer is the host and the other computer is the terminal.
A virtual private server (VPS) running in a data center doesn’t have its own terminal (keyboard or monitor). Hosts without terminals are referred to as headless systems. If you have a VPS running in a data center, you can use SSH to remotely log into the VPS, so your local computer becomes a terminal of the VPS host.
Wrapping Up
I hope this article helped you understand Linux terminals, consoles and shells. As always, if you found this post useful, subscribe to our free newsletter to get more tips and tricks 🙂
Источник