In a Linux GUI, we usually take it for granted that when we insert a USB flash drive, hard drive or even a DVD disk, they just appear ready for use. But under the hood, there is a process where the device is mounted (prepared for use) and a mount point is assigned. When using a server or remote connection, there is no guarantee that the device will be automatically available, so how can we connect our own devices?
In this guide, we will look at various ways to mount and unmount disks and disk images. We will be using a variety of approaches and tools, including both terminal emulator commands and GUI tools.
All commands in this guide will work on most Linux machines. We used an Ubuntu 20.04 install, but you could run this guide on Raspberry Pi. All instructions are executed through the terminal. You can open a terminal window on most Linux computers by pressing ctrl, alt and t.
Unmounting and Mounting Disks Using GUI Tools
In most cases, when you plug in a drive, such as a flash drive, the system will recognize it and automatically mount the drive. Sometimes this may not happen, and knowing how to manually mount and unmount a drive can be a useful skill.
1. Plug in your USB flash drive and let it mount automatically. You should see an icon appearing as a shortcut to the drive, or by opening File Explorer you will find that the flash drive is installed.
2. Click Windows / Super click and find “disk”. Select the Disks utility.
3. Choosing the right USB drive. Click on the square stop button icon to unmount the drive. You will see your main system drive(s) as well as the flash drive you just inserted, so double check before you take any action.
4. Press the mount button to remount the drive. When the flash drive is disconnected, the mount button icon changes to a triangular “play” icon. Pressing this button will reconnect the flash drive.
5. Unmount the drive to prepare for the next part of the tutorial. For the next part of the tutorial, it’s useful to have the flash drive connected but not mounted, so unmount it by clicking the unmount button in Disk Utility again.
Identify and mount disk using Linux terminal
Using the command line interface (CLI) in the terminal emulator allows us to have more control over the timing of disks being mounted and the position in the file system to which they are mounted.
1. Identify USB drive with lsblk command.In the results, you may well see a lot of entries marked “loop”. However, you are looking for results that are listed as sda or sdb to identify the physical drives attached to your system. Comparing the listed capacity of connected drives can often help you determine the name of the target drive. In our case, we can identify our USB stick as sdb1.
2. Create a directory to mount the USB drive. When an external drive is automatically mounted, it is often mounted inside the media directory. However, using the CLI, we can create and specify a directory where we will mount our flash drive. Note that we need to invoke root permissions with sudo to create a directory inside the media directory.
sudo mkdir /media/pendrive
3. Mount the USB drive to the /media/pendrive directory using the mount command. The mount command has the following syntax; sudo mount /path/to/drive /path/to/mountpoint.
sudo mount /dev/sdb1 /media/pendrive
4. Check that the drive is remounted. run lsblk. Note that the last column of the lsblk output shows the mount point of the specified device, if the mount point is listed, then the device is confirmed to be mounted.
Unmounting a disk in Linux using the umount command
Unmounting a drive is done with the umount command, and when invoked, it safely removes the drive from the system, allowing us to eject the drive and use it on another machine.
1. Unmount the drive using the umount command. Pay attention to the spelling umount as a common mistake is for people to type “unmount”. Using the umount command, we only need to specify the location of the mount point and the name of the drive we want to unmount.
sudo umount /media/ pendrive
2. Check that the drive is unmounted with lsblk. Note that in the lsblk output, the last column lists the mount points of the detected devices, if there is no mount point listed, the device is unmounted.
Mounting a disk image to view content on Linux
It is possible to mount a disk image so that it appears as a disk accessible only for reading. This is a useful method if you want to copy some content from a disk image, or if you just want to check the contents of a disk image. In the following example, we have used the downloaded disk image Puppy Linux distribution but this method will work with any disk image, including images created from disks for backup purposes.
1. Create a directory called iso in the directory media to mount the disk image. Again, this could be anywhere on the filesystem, but we created a directory named iso in the media directory.
sudo mkdir /media/iso
2. Mount the ISO disk image using the mount command and the loop argument. We need to run this command as root, so we use sudo. The use of the mount command is similar to the previous use and includes the path to the image and the path to the mount point we created in the previous step.We also add the -o loop argument to create a loop device that tricks the operating system into believing it’s a real disk and not an image.
sudo mount-output loop Boot/fossapup64-9.5.iso /media/iso
3. Unmount the ISO with umount. Once again, when using the umount command, we only need to specify the mount point of the disk or disk image we want to unmount.
sudo umount /media/iso
With these methods, you now have more control over mounting and unmounting drives in Linux and some skills that can help the next time the mounted drive fails. will automatically mount properly. The ability to mount a disk image using the loop device is very useful when examining old backup images of previously used systems, or when you want to familiarize yourself with the contents of a Linux distribution image for study or study.