The file system on Linux and Unix-based operating systems like macOS can be mounted (attached) or unmounted through the Terminal. In this article, Mytour will guide you on how to mount and attach storage devices via Linux Terminal.
1. File System on Linux
File systems on Linux, macOS, and other Unix-based operating systems don't use drive identifiers like Windows. Windows assigns a letter, such as C: or D:, for each drive, and the file system for each drive is a hierarchy of folders beneath that drive letter.
On Linux, the file system is one unified tree of directories. A storage device, when mounted, has its file system integrated into the directory tree to signify its essential part in the attached file system.
The newly mounted file system can be accessed through the directory it's mounted on. This directory is called the mount point (empty folder) for the file system.
Many file systems are automatically mounted during startup or when a drive is connected to the computer while it's running. System admins, if they wish, can disable, turn off the auto-mount feature to control connections to the system.
This means that storage devices connected while the computer is running won't automatically mount and require manual mounting. Manual file system mounting allows users to choose the mount point directory location and set the file system to read-only or read-write mode.
2. Listing mounted file systems using the mount command
To list all file systems mounted on the computer, simply enter the command mount and press Enter.
The mount command will display all connected file systems on the Terminal window.
If desired, you can fine-tune the output by requesting mount to only list the file systems you are interested in. The -t (type) option specifies the type of file system to be mounted.
mount -t tmpfs
mount -t ext4
For example, suppose you want the mount command to only list tmpfs file systems. In this way, you can easily manage the output.
The tmpfs file system will appear as if it is a mounted file system, but in reality, it is stored in volatile memory. 'tmp' stands for temporary, and instead of being stored on a persistent storage device, it is kept in volatile memory.
If desired, you can replace the tmpfs parameter with the file type you are interested in.
In this example, Mytour uses the command to list all ext4 file systems. In the test computer, there is only one ext4 file system located on the sda device - the first storage device mounted, typically the main drive and mounted on /, the root of the file system tree.
Meanings of other indicators:
- rw: The file system can be read and written to.
- relatime: The kernel is using an optimization scheme to record file access permissions and modify meta data.
- errors=remount -o: If a serious error is detected, the file system will be remounted in read-only mode to allow for diagnosis.
3. List mounted file systems using df command
In addition to the mount command, we can also use the df command to display mounted file systems and the locations of mount points.
The df command without parameters provides us with a plethora of information, including details about file systems like the squashfs file system on Ubuntu Linux, a pseudo file system created for applications built with the snap command. We may not need to bother with this information.
To make df ignore this information or other types of system information, we use the -x (exclude) option:
df -x squashfs
Now you can easily see the names of the file systems, their sizes, free space, used space, and the mount point directories.
4. Remount All File Systems Using fstab
Every file system mounted during startup has a file called fstab, a file system table located in /etc.
We can use the mount command to force a 'refresh' and remount all file systems listed in fstab. This is useful in case of issues with multiple file systems.
Use sudo, and you will receive a password prompt:
sudo mount -a
In case of file system issues on your computer, remounting helps resolve the issues. If not, at least you'll receive diagnostic messages, and system logs will guide you to find the root cause of the problem.
5. Mount ISO Image
The steps to mount an ISO image and access its contents as part of the file system are quite straightforward. In the example below, Mytour uses the Tiny Core Linux ISO due to its small size and quick download process.
In the same directory as the ISO image, run the command below. Remember to change the name of the ISO file you want to mount:
sudo mount -t iso9660 -o loop TinyCore-current.iso /mnt
Since sudo is used, you'll be prompted to enter your password.
The -t (type) option indicates the type of file system being mounted. This is an ISO file, so you provide the iso9660 type identifier.
The -o (options) flag is used to pass additional parameters to mount. The parameter in this example is loop.
Use loop to force mount to use a loop device file connected to the ISO image. A loop device file allows a file (such as an ISO image) to be mounted and treated as if it were a storage device.
Loop devices are special files used as interfaces to connected devices, making them appear as normal file system files.
There are various types of loop device files; in this example, only the ext4 file system is mounted and referred to as sda.
To be more precise, the ext4 file system resides on a storage device connected to the file device /dev/sda and the file system on the storage device is mounted at /.
Of course, we need to provide the ISO image name and inform mount about the location of the mounted file system. In this example, Mytour chooses /mnt.
Once the ISO image is mounted, a message will be displayed on the Terminal window stating that the ISO image is mounted in read-only mode.
6. Search for the ISO Image
Once mounted, we can navigate to directories within the ISO image just like a part of the file system. Note that the ISO image file is mounted at /mnt.
ls /mnt
ls /mnt/cde/
Unmount ISO image
To unmount the previously mounted file system, we use the unmount command.
Firstly, notify the unmount command about the file system we want to unmount. To achieve this, we only need to provide the mount point of the file system.
sudo umount /mnt
If there are no messages on the screen, it means everything is okay.
Create Mount Point
If desired, you can create and use a separate mount point. Let's call it isomnt and mount the ISO image onto it. Essentially, a mount point is just a directory, so we can use mkdir to create a new mount point.
sudo mkdir /media/dave/isomnt
The next step involves using the same command format as before to mount the ISO image. However, this time, we won't mount the file into /mnt but into /media/dave/isomnt/:
sudo mount -r -t iso9660 -o loop TinyCore-current.iso /media/dave/isomnt/
Now you can access the mounted file system from the new mount point directory.
ls /media/dave/isomnt/cde/optional
7. Link Mount Point
We can link the mount point to another directory. The mounted file system can then be accessed either through the original mount point or through the directory linked to it.
For example, let's create a new directory named iso in the home directory, then link the mount points of the ISO image /media/dave/isomnt to the iso directory in the home directory.
We can 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 link.
mkdir iso
sudo mount -B /media/dave/isomnt/ iso
ls iso
ls /media/dave/isomnt
cd iso
ls
cd cde
8. Use umount to Unlink Mount Point
Unmounting a file system with a mount point linked to another directory requires unmounting it from the mount point directory and the original bind point.
Even after unmounting the file system from the original mount point directory, we can still access the file system from the linked directory. Additionally, the file system must also be unmounted from that directory.
sudo umount /media/dave/isomnt
ls iso
sudo umount iso
ls iso
Mount Floppy Disk
Floppy disk is also a storage device. This means a storage device file, abbreviated as sd (storage device), will be used to connect to the physical device.
We need to set up the next sd device file. To do this, we pipe the output of df through grep and search for entries containing 'sd.'
df | grep /dev/sd
In this example, there is a single sd device file used, which is /dev/sda. The next sd device file would be /dev/sdb. This means when we connect a floppy disk to the computer, Linux will use /dev/sdb to connect to the floppy drive.
We will instruct mount to mount the file system onto the floppy disk connected to the mount point /dev/sdb to the /mnt.
Connect the floppy drive through a USB port on the computer, then run the command below:
sudo mount /dev/sdb /mnt
9. File System Labeling
We can utilize the -l (label) option with mount to find labels attached to the file system (if any).
Use the -t (type) option to instruct mount to report only on vfat file systems.
mount -l -t vfat
The labels are enclosed in square brackets at the end of the list. The label for the floppy drive is NORTUN.
Accessing the floppy drive through the mount point /mnt:
cd /mnt
ls
ls -l AMATCH.C
The floppy disk contains source files written in the C programming language. The date stamp indicates the files were last modified in October 1992.
If we repeat df through the greb command to list sd device files, we will see 2 floppy drives.
df | grep /dev/sd
The floppy drive displayed is mounted on /dev/sdb. The filesystem on the floppy drive is mounted at /mnt.
To unmount the floppy drive, we use umount and provide the device file as a parameter.
sudo umount /dev/sdb
10. Lazy Unmount Option
In the case of an unmounted filesystem, the process will fail.
sudo umount /dev/sdb
The process fails because the user's current working directory is within the filesystem you're trying to unmount. Linux is smart enough not to allow users to see the branch they're standing on.
To overcome this issue, we use the -l (lazy) option. This option makes umount wait until the filesystem can be safely unmounted.
sudo umount -l /dev/sdb
ls
cd -
ls /mnt
Even though the umount command is running, the filesystem is still mounted, and users can list files as usual.
Right after the user switches to the home directory, the floppy filesystem will be released and unmounted. Trying to list files in /mnt will return no results.
11. Mount Samba Share
Samba is a set of software services that allow sharing networks between Linux and Unix operating systems and Windows operating systems.
To mount a Samba Share on Linux, follow the steps below:
The Raspberry Pi is connected to the same network as the test device with an integrated Samba Share. The Samba contains a mirror folder named 'share.' Your task is to establish an SSH connection to it and view the contents of the share folder. This folder is located within the USB drive mounted on the Pi.
The username is pi, and the network name of the Raspberry Pi is marineville.local:
ls /media/pi/USB64/Backup
exit
When running the SSH command, you'll be prompted to enter the Raspberry Pi's password.
After entering the password and being authenticated, the command prompt on the Terminal window changes to pi@marineville as it is connected to the Raspberry Pi.
The contents of the share folder are listed at /media/pi/USB64/Backup. The content includes 2 folders, one named dave and one named pat.
Enter Exit to disconnect from the Raspberry Pi, and the command prompt will change to dave@howtogeek.
To use Samba, we need to install the cifs-utils package:
If using Ubuntu or Debian-based distributions, use apt-get to install this package on your system. On other Linux distributions, use the respective Linux distribution package management tool.
sudo apt-get install cifs-utils
Once the installation process is complete, use the command below to mount the share folder, replacing the appropriate IP address, share name, and mount point:
sudo mount -t cifs -o credentials=/etc/samba/creds,uid=1000,gid=1000 //192.168.4.13/share /media/dave/NAS
In the above command:
-t cifs: File system type is cifs.
-o credentials=/etc/samba/creds,uid=1000,gid=1000: These optional parameters point to a file called creds, secured and containing the username, password of the Raspberry Pi user, User ID (UID), and Group ID (GID) used to establish ownership and group of the root file system.
//192.168.4.13/share: the network location of the device with the integrated Samba Share and the Samba name of the shared folder. The root directory of the Share folder is named Backup, but the Samba name of the shared folder is set to Share.
/media/dave/NAS: the name of the mount point. Note that you must create the mount point beforehand.
By accessing the mount point at /media/dave/NAS, we can reach the shared folders on the Raspberry Pi over the network. You'll find 2 folders on the Raspberry Pi named dave and pat.
cd /media/dave/NAS
12. Create and Mount File System
We can use the dd command to create an image file, then use mkfs to create a file system and mount it.
Use the if (input file) option to instruct dd to use a stream of zero values from /dev/zero as the input file.
The newly created output file (of - output file) is named geek_fs.
We use the bs (block size) option to request a block size of 1MB.
Use the count option to instruct dd to include 20 blocks in the output file.
dd if=/dev/zero of./geek_fs bs=1M count=20
The created image file contains nothing but zero values.
Next, use the mkfs command to create a file system inside the geek_fs file. The -t (type) option allows choosing the type of file system. In this example, Mytour creates an ext4 file system.
mkfs -t ext4 ./geek_fs
To mount the image file to /media/dave/geek, then use chown to set ownership and group to allow file access:
sudo mount ./geek_fs /media/dave/geek
sudo chown dave:users /media/dave/geek
To explore the new file system and copy content within a file:
cd /media/dave/geek
cp /etc/fstab .
ls -l
If using mount to list mounted file systems but limiting the output to ext4 file systems, we use the -t (type) option, and the result shows 2 ext4 file systems mounted.
mount -t ext4
13. Remounting the File System
To remount the file system, we use the option -o remount. This process is performed to change the file system from read-only to read-write state.
Assuming remounting the floppy drive, we will use the -r (read-only) flag, then mount through grep to search for details of the floppy drive file system:
sudo mount -r /dev/sdb /mnt
mount | grep /mnt
As you can observe, ro is marked to indicate that the file system is mounted in read-only mode.
Use the option -o remount with the rw (read-write) flag to unmount and remount the file system with the new settings:
sudo mount -o remount,rw /mnt
Iterating through the mount process via grep, you can see ro replaced with rw, indicating that the file system is now in read-write mode.
mount | grep /mnt
14. Move File System
If desired, you can unmount and remount the file system on a different mount point with just one command.
The -M (move) option in mount allows you to achieve this. However, this option is no longer functional in Linux distributions; instead, we use systemd.
Attempting to move the file system from /mnt to ./geek will result in failure, displaying an error message on the screen.
sudo mount -M /mnt ./geek
ls ./geek
To address this issue, we use the -B (bind) option beforehand to link the original mount point to the new mount point.
sudo mount -B /mnt ./geek
ls ./geek
In this article, Mytour has guided you on how to mount and attach storage devices on the Linux Terminal. If you have any questions or queries, such as how to compare text files on the Linux Terminal, feel free to leave your comments below the article.
