File systems in Linux and Unix-like operating systems like macOS can be mounted, unmounted, and remounted using the terminal. This is a powerful and versatile tool—here’s everything you need to know.

The Linux File System

The file systems in Linux, macOS, and other Unix-like operating systems don’t use separate volume identifiers for storage devices in the way that, say, Windows does. Windows assigns each volume a drive letter such as C: or D: and the file system for each volume is a tree of directories sitting below that drive letter.

In Linux, the file system is an all-in-one directory tree. A mounted storage device has its file system grafted onto that tree so that it appears to be an integral part of one cohesive file system. The newly mounted file system will be accessible via the directory to which it is mounted. That directory is called the mount point for that file system.

Many file systems are auto-mounted at boot time or on-the-fly as storage volumes connected to the computer during runtime. Cautious system admins can turn off the runtime auto-mount features so that they can control connections to the system.

This means storage devices connected during runtime might not auto-mount and will require mounting manually. Mounting a file system manually lets you make decisions about that file system, such as where the mount point will be and whether the file system is going to be read-only or read-write.

Whether it is out of necessity or through choice, the mount, umount and remount commands give you the ability to take control of this important aspect of your Linux system.

Interrogate Your File System With mount

Mount has a great many options, but to list all of the mounted file systems on your computer requires no options at all. Simply type mount and hit Enter:

mount command in a terminal window

mount will list all of the connected file systems in the terminal window.

output from mount in a terminal window

It can be difficult to pick through that dump of data to find what you are looking for.

You can refine the output by asking mount to list only the file systems of interest to you. The -t (type) option tells mount what type of file system to report on.

mount -t tmpfs
mount -t ext4

mount with the -t option in a terminal window

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

tmpfs file system appears as though it were a regular, mounted file system but it is actually stored in volatile memory—the tmp stands for temporary—instead of on a persistent storage device.

You’ll want to substitute the tmpfs parameter for the file type in which you are interested.

We’ve also issued a command to list ext4 file systems. On this test computer, there is a single ext4 file system, it is on device sda—the first storage device mounted, usually the main hard drive—and mounted on /, which is the root of the file system tree.

The other indicators mean:

  • rw: The file system is readable and writable.
  • relatime: The kernel is using an optimized scheme to record file access and modification meta-data.
  • errors=remount -o: If a sufficiently serious error is detected, the file system will be remounted in read-only mode to allow diagnosis.

RELATED: Which Linux File System Should You Use?

Interrogate Your File System With df

The df command can also be used to display which file systems are mounted and where their mount points are.

df used with no parameters gives you the same information overload problem as mount. As an example, in Ubuntu Linux, there is a squashfs pseudo-file system created for each and every application that has been installed using the snap command. Who wants to see all of those?

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

df -x squashfs

df command with -x exclude option in a terminal window

You can easily see the names of the file systems, their capacities, used and free space, and their mount points.

RELATED: How to View Free Disk Space and Disk Usage From the Linux Terminal

Remounting All File Systems in fstab

All of the file systems mounted at boot time have entries in a file called fstab, which is the file system table located within /etc.

You can use mount to force a “refresh” and remount all the file systems listed in fstab. Under normal operating conditions this is not required. It really comes into its own if you have issues with multiple file systems.

You’ll need to use sudo, so you’ll be prompted for your password.

sudo mount -a

mount -a command in a terminal window

Admittedly, on a correctly operating computer, it is a little underwhelming.

On a computer with file system issues, however, the remount might clear the problems. If that doesn’t happen, then at least you will get diagnostic messages on the screen and in the system logs that will guide you to seek out the cause of the problem.

RELATED: What Is the Linux fstab File, and How Does It Work?

Mounting an ISO Image

It is easy to mount an ISO image so that you can access its contents as part of the file system.

This will work with any ISO image. In this example, we happen to be using a Tiny Core Linux ISO because it is conveniently small and quick to download. (A tiny Linux distribution with a GUI, in 18 MB! You probably have .mp3 files bigger than that.)

In the same directory as the ISO image, issue this command. Substitute the name of the ISO file that you are mounting.

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

mounting an ISO image in a terminal window

Because we need to use sudo you’ll need to enter your password.

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

The -o (options) flag is used to pass extra parameters to mount. Our parameter is loop.

We’re using loop to force mount to use a loop device file to connect to our ISO image. A loop device file allows a file (like the ISO image) to be mounted and treated as though it were a storage device.

Device files are special files used as an interface so that connected devices appear as though they were a normal file system file. This is part of the everything in Linux is a file design philosophy.

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

To be more accurate, that ext4 file system is on a storage device connected to the file system through the /dev/sda device file and the file system on that storage device mounted at /.

We have to provide the name of the ISO image of course, and we need to let mount know where we’d like the file system to be mounted. We have chosen /mnt.

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

Exploring the ISO Image

Now that it is mounted we can navigate the directories in the ISO image in the same way as any other part of the file system. Let’s list the files in the ISO image. It is mounted at /mnt remember.

ls /mnt
ls /mnt/cde/

listing the files on a mounted ISO image in a terminal window

Unmounting the ISO Image

To unmount 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 “unmount.”

You must tell umount which file system you are unmounting. Do so by providing the file system’s mount point.

sudo umount /mnt

umount command in a terminal window

No news is good news. If there’s nothing to report, then all went well.

Creating a Mount Point

You can create and use your own mount points. We’re going to create one called isomnt and mount our ISO image on that. 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

Now we can use the same command format as before to mount our ISO image. This time we won’t mount it on /mnt, we’ll mount it on /media/dave/isomnt/:

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

mounting ISO 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/cde/optional

ls command on new mount point in a terminal window

But those pathways are getting very long. That’s quickly going to become tiresome. Let’s do something about that.

Binding a Mount Point

You can bind a mount point to another directory. The mounted file system can then be accessed either through the original mount point or through the directory that is bound to it.

Here’s a worked example. We’ll create a directory in our home directory called iso. Then we’ll bind the mount point of the ISO image /media/dave/isomnt to the new iso directory in our home directory.

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

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

binding mount points in a terminal window

Using umount With Binds

A file system that has had its mount point bound to another directory requires unmounting from its mount point and the bind point.

Even if we unmount the file system from its original mount point, you can still access the file system from its bound directory. The file system must be unmounted from that directory also.

sudo umount /media/dave/isomnt
ls iso
sudo umount iso
ls iso

unmounting bound mount points in a terminal window

Mounting a Floppy Disk

A floppy drive (with a floppy disk in it) is a storage device. That means an sd (for storage device) device file will be used to connect to the physical device. We must establish which is the next free sd device file. We can do this by piping the output of df through grep and looking for entries with “sd” in them.

df | grep /dev/sd

piping df through grep in a terminal window

On this computer, there is a single sd device file in use. This is /dev/sda. The next sd device file issued will be /dev/sdb. That means 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 disk in the floppy drive that is connected to /dev/sdb to the /mnt mount point.

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

sudo mount /dev/sdb /mnt

mounting a floppy drive in a terminal window

File System Labels

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

We’re using the -t (type) option to ask mount to report on vfat file systems only.

mount -l -t vfat

mount with the -l label option in a terminal window

You’ll find the label in square brackets at the end of the listing. The label for this floppy drive is NORTUN.

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

cd /mnt
ls
ls -l AMATCH.C

contents of a floppy disk in a terminal window

The floppy contains C language source code files. The date stamp of one file shows it was last modified on October 1992. It is probably older than a lot of our readers. (Needless to say the meaning of NORTUN as a label is lost in the mists of time.)

If we repeat our df piped through grep command to list sd device files, we’ll see that there are now two of them.

df | grep /dev/sd

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

Our floppy drive is showing as mounted on /dev/sdb as we expected. The file system on the floppy disk in the drive is mounted at /mnt .

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

sudo umount /dev/sdb

unmounting a floppy drive in a terminal window

The umount Lazy Option

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

sudo umount /dev/sdb

umount failing in a terminal window

It failed because the current working directory of the user is within the file system he is trying to unmount. Linux is smart enough not to let you saw off the branch you’re sitting on.

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

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

umount -l lazy option in a terminal window

Even though the umount command is issued, the file system is still mounted, and the user can list the files as normal.

As soon as the user changes directory to their home directory, the floppy file system is released and is unmounted. Trying to list the files in /mnt produces no results.

Mounting a Samba Share

Samba is a set of software services that allow network shares to be accessed interchangeably between Linux and Unix-like operating systems, and Windows operating systems.

Setting up Samba is beyond the scope of this article. But, if you have authorized access to a Samba share that has been made available to you, this is how you can mount it in Linux.

A Raspberry Pi connected to the same network as the test machine has a Samba share on it. It is a directory called Backup that has given the Samba name of “share.” Let’s make an SSH connection to it and look at the contents of the shared directory. The shared directory is on a USB stick mounted on the Pi.

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

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

ssh to a raspberry pi in a terminal window

The user issues the SSH command and is prompted for their Raspberry Pi password.

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

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

They type exit to disconnect from the Raspberry Pi and the prompt changes back to dave@howtogeek.

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

Use apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.

sudo apt-get install cifs-utils

installing cifs in a terminal window

When the installation has completed, mount the share with a command like the following, changing the IP address, share name and mount point (which must already exist) to suit your circumstances.

sudo mount -t cifs -o credentials=/etc/samba/creds,uid=1000,gid=1000 //192.168.4.13/share /media/dave/NAS

mounting a samba share in a terminal window

Let’s break down the parts of that command.

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

By accessing our mount point at /media/dave/NAS we are accessing the shared directory on the Raspberry Pi across the network. We can see the two folders on the Raspberry Pi called dave and pat.

cd /media/dave/NAS

accessing samba shared directories in a terminal window

Creating and Mounting a File System

You can use the dd command to create an image file, then use mkfs to create a file system inside it. That 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 stream of zero values from /dev/zero as the input file.

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

We’re using 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

using dd to create a filesystem in a file in a terminal window

That creates our image file for us. It contains nothing but zero values.

We can create a working file system inside the geek_fs file using the mkfs command. The -t (type) option allows us to select the file system type. We’re creating an ext4 system.

mkfs -t ext4 ./geek_fs

the mkfs command in a terminal window

That’s all it takes to have a working file system.

Let’s mount it on /media/dave/geek and then use chown to set the owner and group ownerships to allow access to it.

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

mounting and setting access to a filesystem in a terminal window

Does it work? Let’s change into the new file system and copy in a file to see.

cd /media/dave/geek
cp /etc/fstab .
ls -l

Exploring filesystem in a file in a terminal window

We were able to change directory into the new file system, and we successfully made a copy of the /etc/fstab file. It’s working!

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

mount -t ext4

listing ext4 filesystems in a terminal window

Remounting a File System

Remounting a file system uses the -o remount option. It is typically done to change a file system from a read-only (testing) state to a read-write (production) state.

Let’s mount our floppy drive again. This time we’ll use the -r (read-only) flag. Then we’ll pipe mount through grep and look at the details of the floppy file system.

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

remounting a file system in a terminal window

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

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

sudo mount -o remount,rw /mnt

Repeating the piping of mount through 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) Moving a File System

You used to be able to unmount a file system and remount it on another mount point with a single command.

The -M (move) option in mount exists specifically to allow you to do that. But it no longer works in Linux distributions that have moved over to systemd. And that’s most of the big names.

If we try to move a file system from /mnt to ./geek, it fails and gives the error message shown below. Trying to list the files in the files ystem through ./geek returns no results.

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

moving a file system in a terminal window

The workaround is to use the -B (bind) option that we used earlier to bind the original mount point to the new mount point.

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

Apart from not freeing up the original mount point, this will have the same practical outcome.

Final Observations

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

  1. It may have unpredictable behavior.
  2. It was not persistent and would need to repeat 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 that system.

Apart from the systemd issues with the -M (move) option, you should find the use of mount and umount straightforward. These are great commands to have up your sleeve when faced with a damaged system, and you have to start piecing the file system back together by hand.





Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here