How to Mount and Unmount Storage Devices from the Linux Terminal

Linux file systems and Unix-like operating systems such as macOS can be mounted, unmounted, and remounted using the terminal. It's a powerful and versatile tool, here's everything you need to know.

The Linux file system

Linux file systems, macOS and others Unix-like operating systems do not use separate volume identifiers for storage devices, as opposed to Windows. Windows assigns each volume a drive letter such as C: or D: and the file system for each volume is a directory tree below that drive letter.

On Linux, the file system is an all-in-one directory tree. A file system of a mounted storage device has been grafted onto this tree, so that it seems to be part of a consistent file system. The newly mounted file system will be accessible through the directory in which it is mounted. This directory is called the mount point of this file system.

Many file systems are mounted automatically on startup or on the fly as storage volumes connected to the computer during execution. Prudential system administrators can disable the automatic mount features at run time to control connections to the system.

This means that the storage devices connected during the run may not be mounted automatically and will require manual editing. Manually editing a file system allows you to make decisions about this file system, such as the location of the mount point and the status of the file system as read-only or read-only. writing.

Whether by necessity or choice, the mount, umount and remount commands let you take control of this important aspect of your Linux system.

Query your file system with mount

Mount has many options, but the list of all file systems mounted on your computer does not require any options. Just type mount and press Enter:

Mount command in a terminal window

mount lists all connected file systems in the terminal window.

mount output in a terminal window

It can be difficult to explore this data dump to find what you are looking for.

You can refine the result by asking mount to list only the file systems that interest you. The -t (type) option specifies the type of file system to report.

mount -t tmpfs
mount -t ext4

mount with the -t option in a terminal window

For example, we asked mount to list only tmpfs file systems. We get a much more manageable output.

A tmpfs file system appears as if it was a standard mounted file system, but it is actually stored in a volatile memory (tmp means temporary) and not on a storage device persistent.

You will want to replace the tmpfs parameter with the file type that interests you.

We also issued a command to list the ext4 file systems. On this test computer, there is only one ext4 file system, it is located on the sda ​​device (the first mounted storage device, usually the primary hard drive) and mounted on /, which is the root of the device. 39, file system tree.

The other indicators mean:

rw: The file system is readable and writable.
relatime: the kernel uses an optimized schema to record the metadata of access and file modification.
errors = remount -o: If a sufficiently serious error is detected, the file system will be raised to read-only to allow the diagnosis.

RELATED: Which Linux file system should you use?

Query your file system with df

The df command can also be used to view mounted file systems and where are their mounting points.

df used without parameters poses the same problem of information overload as editing. For example, in Ubuntu Linux, there is a squashfs pseudo-file system created for each application installed using the snap command. Who wants to see all this?

To force df to ignore them (or any other type of file system), use the -x (exclude) option:

df -x squashfs

Df command with the -x exclusion option in a terminal window

You can easily see file system names, their capabilities, used and available space, and their mount points.

RELATED: How to view available disk space and disk usage from the Linux terminal

Remount all file systems in fstab

All boot-mounted file systems have entries in a file called fstab, which is the file system table in / etc.

You can use mount to force a "refresh" and reassemble all the file systems listed in fstab. Under normal conditions of use, this is not necessary. This makes sense if you have problems with multiple file systems.

You will have to use sudo, so you will be prompted to enter your password.

sudo mount -a

mount -a command in a terminal window

Surely, on a computer running properly, it's a little disappointing.

On a computer with file system problems, however, reassembly can solve problems. If this does not happen, you will get at least diagnostic messages on the screen and in the system logs that will guide you in finding the cause of the problem.

RELATED: What is the Linux fstab file and how does it work?

Mount an ISO image

It is easy to mount an ISO image in order to access its contents as part of the file system.

It will work with any ISO image. In this example, we use a Tiny Core Linux ISO because it is very small and fast to download. (A very small Linux distribution with a graphical interface, 18 MB! You probably have larger .mp3 files than this.)

In the same directory as the ISO image, run this command. Replace the name of the ISO file you are mounting.

sudo mount -t iso9660 -o loop TinyCore-current.iso / mnt

mounting an ISO image in a terminal window

Because we have to use sudo, you will have to enter your password.

The -t (type) option tells mount what type of file system we are mounting. This is an ISO file, so we provide you with the iso9660 specifier.

The -o (options) flag is used to transmit additional parameters to mount. Our parameter is the loop.

We use loop to force the mount to use a loop device file to connect to our ISO image. A loopback device file allows a file (such as the ISO image) to be mounted and processed as if it was a storage device.

The device files are special files used as the interface for the connected devices to appear as if it was a normal file system file. This is part of the everything under Linux is a file design philosophy.

There are many types of device files. We already saw one when we found that the only ext4 file system on this test machine was mounted on / and called sda.

To be more specific, this ext4 file system is located on a storage device connected to the file system via the / dev / sda device file and the file system of that storage device mounted on /.

We must of course provide the name of the ISO image, and we must tell mount where we want the file system to be mounted. We chose / mnt.

The ISO image is mounted. A reminder that ISO images are always mounted read-only appears in the terminal window.

Exploring the ISO image

Now that it is mounted, we can navigate the ISO image directories in the same way as any other part of the file system. List the files in the ISO image. He climbed to / mnt, remember.

ls / mnt
ls / mnt / cde /

list files on an ISO image mounted in a terminal window

Disassembling the ISO image

To dismount a mounted file system, use the umount command. Note that there is no "n" between the "u" and the "m" – the command is umount and not "disassemble".

You must tell umount which file system you are disassembling. Do it by providing the mount point of the file system.

sudo umount / mnt

Umount command in a terminal window

No news, good news. If there is nothing to report, all is well.

Create a mount point

You can create and use your own mount points. We will create one called isomnt and mount our ISO image on it. A mount point is just a directory. So we can use mkdir to create our new mount point.

sudo mkdir / media / dave / isomnt

mkdir in a terminal window

We can now use the same command format as before to mount our ISO image. This time, we will not mount it on / mnt, we will mount it on / media / dave / isomnt /:

sudo mount -r -t iso9660 -o loop TinyCore-current.iso / media / dave / isomnt /

ISO mount in / media / dave / isomnt in a terminal window

We can now access the mounted file system from our new mount point.

ls / media / dave / isomnt / optional / optional

Ls command on the new mount point in a terminal window

But these paths become very long. It will quickly become tiring. Let's do something about it.

Connect a mount point

You can link a mount point to another directory. The mounted file system can then be accessed through the original mount point or via the directory that is linked to it.

Here is an example worked. We will create a directory in our personal directory called iso. Next, we will link the mount point of the ISO / media / dave / isomnt image to the new iso directory of our home directory.

We will be able to access the ISO image from the original mount point / media / dave / isomnt and the new iso directory. The -B (bind) option requires the name of the mount point and the name of the directory to bind to.

mkdir iso
sudo mount -B / media / dave / isomnt / iso
ls iso
ls / media / dave / isomnt
cd iso
cd cde

connect mount points in a terminal window

Use umount with links

A file system whose mount point is linked to another directory requires disassembly of its mount point and the binding point.

Even if we dismount the file system from its original mount point, you can still access the file system from its linked directory. The file system must also be removed from this directory.

sudo umount / media / dave / isomnt
ls iso

sudo umount iso
ls iso

unmount linked mount points in a terminal window

Mount a floppy disk

A floppy disk drive (containing a floppy disk) is a storage device. This means that a sd device file (for storage device) will be used to connect to the physical device. We need to determine what is the next free SD device file. We can do this by channeling the output of df via grep and looking for entries containing "sd".

df | grep / dev / sd

piping df through grep in a terminal window

On this computer, only one sd device file is used. This is / dev / sda. The next published sd device file will be / dev / sdb. This means that when we connect the floppy drive to the computer, Linux will use / dev / sdb to connect to the floppy drive.

We will tell mount to mount the file system on the floppy diskette disk connected to / dev / sdb at mount point / mnt.

Insert the floppy disk into the drive and connect it to a USB port on the computer. Issue the following command:

sudo mount / dev / sdb / mnt

mounting a floppy disk drive in a terminal window

File System Labels

We can use the -l (label) option with mount to find out which label, if any, is attached to a file system. Labels are just arbitrary names. They have no functional purpose.

We use the -t (type) option to tell mount to report on vfat file systems only.

mount -l -t vfat

mount with the -l label option in a terminal window

You will find the label in square brackets at the end of the list. The label of this floppy disk drive is NORTUN.

We can access the floppy drive via the / mnt mount point.

cd / mnt
ls -l AMATCH.C

contents of a floppy disk in a terminal window

The floppy disk contains Language c source code files. The date stamp of a file indicates that it was last modified in October 1992. It is probably older than many of our readers. (Needless to say, the meaning of NORTUN as etiquette is lost in the mists of time.)

If we repeat our df command via the grep command to list the sd device files, we will see that there are now two.

df | grep / dev / sd

piping df through grep with a floppy drive connected in a terminal window

Our floppy drive appears as mounted on / dev / sdb as expected. The file system on the diskette of the drive is mounted on / mnt.

To unmount the floppy disk, we use umount and pass it the device file as a parameter.

sudo umount / dev / sdb

to unmount a floppy disk drive in a terminal window

The umount Lazy option

What happens if you (or another user) use the file system when you try to unmount it? Disassembly will fail.

sudo umount / dev / sdb

umount failed in a terminal window

It failed because the current working directory of the user is in the file system that he is trying to unmount. Linux is smart enough not to let you saw the branch you are sitting on.

To overcome this, use the -l (lazy) option. This causes umount to wait for the file system to be unmounted safely.

sudo umount -l / dev / sdb
cd ~
ls / mnt

umount -l lazy option in a terminal window

Even if the umount command is issued, the file system is always mounted and the user can list the files normally.

As soon as the user changes directory and returns to his home directory, the floppy file system is released and unmounted. Trying to list the files in / mnt does not produce any results.

Mount a Samba share

Samba is a set of software services that provide interchangeable access to network shares between Linux and Unix operating systems and Windows operating systems.

The Samba configuration is beyond the scope of this article. However, if you have authorized access to a Samba share available to you, here is how you can mount it on Linux.

A Raspberry pie connected to the same network as the test machine has a Samba share. This is a directory called Backup that gave the name Samba "share". Let's do one SSH connect to it and look at the contents of the shared directory. The shared directory is on a USB key mounted on the Pi.

The user name is pi and the network name of the Raspberry Pi is marineville.local.

ssh pi@marineville.local
ls / media / pi / USB64 / Backup

ssh to a raspberry pi in a terminal window

The user issues the SSH command and is prompted to enter his Raspberry Pi password.

They provide their password and are authenticated. The terminal window prompt changes to pi @ navville because it is connected to the Raspberry Pi.

They list the contents of the shared directory in / media / pi / USB64 / Backup. The content consists of two directories, one called dave and the other pat. We now know what to expect when we mount the Samba share.

They type exit to disconnect from Raspberry Pi and the prompt becomes dave @ howtogeek again.

To use Samba, you must install the cifs-utils package.

Use apt-get to install this package on your system if you are using Ubuntu or another Debian-based distribution. On other Linux distributions, use the package management tool of your Linux distribution instead.

sudo apt-get install cifs-utils

to install cifs in a terminal window

When the installation is complete, mount the share using the following command, changing the IP address, share name, and mount point (which must already exist) to suit your situation.

sudo mount -t cifs -o identifiers = / etc / samba / creds, uid = 1000, gid = 1000 // NAS

to mount a samba share in a terminal window

Let's break up the parts of this command.

-t cifs: the type of file system is cifs.
-o credentials = / etc / samba / creds, uid = 1000, gid = 1000: The options settings are the path to a file called secure creds that contains the user name and the Raspberry Pi user's password. The user ID (UID) and group ID (GID) used to set the owner and group of the root of the file system.
// The network location of the device where the Samba share and Samba name of the shared directory are located. The root of the share is a directory called Backup, but its share name Samba is set to share.
/ media / dave / NAS: name of the mount point. You must create your mount point in advance.

By accessing our mount point in / media / dave / NAS, we access the Raspberry Pi's shared directory on the network. We can see the two folders on the Raspberry Pi called dave and pat.

cd / media / dave / NAS

access samba shared directories in a terminal window

Creating and editing a file system

You can use the dd command to create an image file, and then use mkfs to create a file system inside it. This file system can then be mounted. This is a good way to practice and experiment with mount.

We use the if (input file) option to tell dd to use the zero value stream of / dev / zero as the input file.

The de (output file) is a new file called geek_fs.

We use the bs (block size) option to request a block size of 1 MB.

We use the count option to tell dd to include 20 blocks in the output file.

dd if = / dev / zero of./geek_fs bs = 1M count = 20

use dd to create a file system in a file in a terminal window

This creates our image file for us. It contains only null values.

We can create a working file system in the geek_fs file with the help of the mkfs command. The -t (type) option allows us to select the type of file system. We create an ext4 system.

mkfs -t ext4 ./geek_fs

the mkfs command in a terminal window

This is all that is needed to have an operational file system.

Let's get it to / media / dave / geek then use chown to set the owner and owners of the group to allow access.

sudo mount ./geek_fs / media / dave / geek
sudo chown dave: users / media / dave / geek

mounting and configuring access to a file system in a terminal window

Does it work? Let's go to the new file system and copy it to a file to see.

cd / media / dave / geek
cp / etc / fstab.

ls -l

Exploring the file system in a file in a terminal window

We were able to change directories in the new file system and we managed to copy the file / etc / fstab. It works!

If we use mount to list mounted filesystems while limiting its output to ext4 file systems using the -t (type) option, we'll see that there are now two mounted ext4 filesystems.

mount -t ext4

list ext4 file systems in a terminal window

Reassemble a file system

Reassembling a file system uses the -o remount option. It is usually done to change a file system from a read-only state (test) to a read-write (production) state.

Let's go back to our floppy drive. This time, we'll use the -r option (read-only). Then we will channel the assembly via grep and examine the details of the floppy disk file system.

sudo mount -r / dev / sdb / mnt
mount | grep / mnt

to remount a file system in a terminal window

As you can see, the highlighted ro indicates that the file system is mounted read-only.

Using the -o remount option with the rw (read-write) flag, we can dismount and remount the file system with the new parameters, all in one command.

sudo mount -o remount, rw / mnt

Repeating the mount piping via grep shows us that the ro has been replaced by rw (highlighted). The file system is now in read-write mode.

mount | grep / mnt

(Not) move a file system

You could unmount a file system and move it to another mount point with one command.

The -M (move) option in mount exists specifically to allow you to do this. But that does not work anymore in Linux distributions that have moved at systemd. And that's most of the big names.

If we try to move a file system from / mnt to ./geek, this fails and gives the error message below. Trying to list files in ystem files via ./geek does not work.

sudo mount -M / mnt ./geek
ls. / geek

to move a file system in a terminal window

The solution is to use the -B (bind) option previously used to link the original mount point to the new mount point.

sudo mount -B / mnt ./geek
ls. / geek

In addition to not releasing the original mount point, this will have the same practical result.

Concluding remarks

Using the –make-private option, it was possible to force the move on systemd versions of Linux. This technique is not presented here for two reasons.

He may have unpredictable behavior.
This was not persistent and would need to be repeated at each reboot.

Devuan Linux uses SysV init not systemd. A computer was loaded with the latest version of Devuan and tested. The -M (move) option worked as expected on this system.

Aside from systemd problems with the -M (move) option, the use of mount and umount should be simple. These are excellent commands to master when dealing with a damaged system, and you have to start rebuilding the file system by hand.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.