قالب وردپرس درنا توس
Home / Tips and Tricks / Mounting and Unloading Storage Devices via the Linux Terminal

Mounting and Unloading Storage Devices via the Linux Terminal



File systems in Linux and Unix-like operating systems, such as macOS, can be mounted, mounted and re-mounted with the terminal. This is a powerful and versatile tool – here you will find everything you need to know.

The Linux File System

The file systems on Linux, macOS, and other Unix-like operating systems do not use separate disk storage device identifiers in the way that Windows does, for example. Windows assigns a drive letter such as C: or D: to each volume, and the file system for each volume is a directory tree that is under that drive letter.

On Linux, the file system is an all-in-one directory tree. The file system of a mounted storage device is grafted onto this tree, so it appears to be an integral part of a contiguous file system. The newly mounted file system can be accessed from the directory to which it is mounted. This directory is called the mount point for this file system.

Many file systems are automatically mounted as a storage volume at boot time or during operation, and are connected to the computer at runtime. Careful system administrators can disable the auto-provisioning features at runtime to control connections to the system.

This means that storage devices connected at runtime may not be deployed automatically and must be provisioned manually. If you deploy a file system manually, you can make decisions about that file system, such as: For example, where the mount point is and whether the file system should be read-only or read-only.

Whether or not This is Not Required You can use the commands mount umount and remount to control this important aspect of your Linux system.

Query your file system mount

Mount has a lot of options, but listing all mounted filesystems on your computer does not require any options at all. Just enter mount and press Enter:

 mount command in a terminal window

mount lists all the associated file systems in the terminal window.

 Output from mount in a terminal window.

It can be difficult to search this snapshot with data to find what you are looking for.

You can refine the output by listing mount to list only the filesystems that are of interest to you. The option -t (type) tells mount which type of file system to report.

  mount -t tmpfs 
  mount -t ext4 

  mount with the -t option in a terminal window

As an example, we have requested mount only tmpfs file systems. We get a much more manageable edition.

A tmpfs file system appears like a regular mounted file system, but is actually stored in a volatile memory – the -tmp stands for temporary – instead of on a persistent storage device.

You want to use the parameter tmpfs for the file type you are interested in.

We also issued a command to list ext4 file systems. This test computer has a single ext4 file system. It is located on device sda – the first storage device usually installed on the main hard disk – and is the root of the file system tree on / .

The other indicators mean:

  • rw : The file system is readable and writable.
  • relatime : The kernel uses an optimized schema to record file access and change metadata.
  • Errors = Remount -o : If a sufficiently fatal error is detected, the file system is re-mounted in read-only mode to allow diagnostics.

] RELATED: Which Linux file system should you use?

Interrogate Your File System with df

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

df without parameters, you will get the same information o problem as Mount Download. In Ubuntu Linux, for example, there is a pseudo-file system squashfs that was created for each application that was installed with the command snap . Who wants to see all these files?

To force df to ignore it – or another file system type – use the option -x (exclude):

  df -x squashfs 

  command df with the -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: Viewing Free Space and Disk Usage on the Linux Terminal

Reattaching All File Systems to fstab

There are entries for all file systems mounted at boot time In a file named fstab the file system table in / etc .

You can use mount to force an upgrade and reinsert all file systems listed in fstab . Under normal operating conditions this is not required. If you have problems with multiple file systems, it becomes something special.

You must use sudo to prompt for your password.

  sudo mount -a [19659042] mount -a command in a terminal window " width="646" height="97"/> 

On a properly working computer, this is somewhat insurmountable.

On a computer with file system issues, resolving the issues may help. If this is not the case, you will receive diagnostic messages on the screen and in the system logs to help you find the cause of the problem.

RELATED: What Is the Linux fstab file and how it works

Mounting an ISO image

It's easy to mount an ISO image with it You can access its contents as part of the file system. [19659004] This works with any ISO image. In this example, we happen to use a Tiny Core Linux ISO because it's small and fast to download. (A tiny Linux distribution with a graphical user interface (in 18 MB). You probably have .mp3 files larger than this.)

Issue this command in the same directory as the ISO image. Replace 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 it to use sudo you have to enter your password.

The -t (type) option tells mount what file system type we are mounting. Since it is an ISO file, we specify the type identifier iso9660 .

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

We use loop to force mount to use a loop device file to connect to our ISO image. With a loop device file, a file (such as the ISO image) can be deployed and treated as a storage device.

Device files are special files that are used as an interface, so connected devices look like normal files in system files. This is part of what in Linux is all about a file design philosophy.

There are many different types of device files. We saw one earlier when we found that the only ext4 file system on this test machine was mounted on / and was named sda . More precisely, the file system ext4 is located on a storage device containing the device file / dev / sda and the file system on this storage device at / with connected to the file system. .

Of course, we need to specify the name of the ISO image and tell mount where to mount the file system. We have chosen / mnt .

The ISO image is mounted. The terminal window displays a reminder that ISO images are always provided in read-only mode.

Browsing the ISO Image

After the image has been deployed, we can browse the directories in the ISO image in the same way as part of the file system. Let's list the files in the ISO image. It is mounted under / mnt .

  ls / mnt 
  ls / mnt / cde / 

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

Log off the ISO image

Use the command umount to undeploy a mounted file system. Note that there is no "n" between "u" and "m" - the command is umount and not "unmount".

You must tell umount which file system you are unmounting. Specify the mount point of the file system.

  Command sudo umount / mnt 

  umount in a terminal window

No message is good news. If there is nothing to report, everything is fine.

Creating a Mount Point

You can create and use your own mount points. We will create one with the name 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

Now we can use the same command format as before to mount our ISO image. This time we will not mount it on / mnt but 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 embedded file system from our new mount point. [19659015] ls / media / dave / isomnt / cde / optional

  ls command on new mount point in a terminal window

These paths, however, are very long. This gets boring quickly. Let's do something about it.

Binding a mount point

You can bind a mount point to another directory. The deployed file system can then be accessed either through the original mount point or through the directory attached to it.

Here is an example. In our home directory, we create a directory named iso . Then we bind the mount point of the ISO image / media / dave / isomnt to the new directory iso in our home directory.

We can access the ISO image via the original mount point / media / dave / isomnt and the new directory iso . The option -B (bind) 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 
  ls 
  cd cde [19659093] Bind mount points in a terminal window " width="646" height="352"/> 

Use umount with binds

For a file system whose mount point is bound to a different directory, you need to mount its mount point and cancel the bind point.

Even if you detach the file system from its original mount point, it can still access the file system from its bound directory. The file system must also be unsubscribed from this directory.

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

  Providing bounded mount points in a terminal window

Deploying a diskette

A diskette drive (with a diskette inside) is a storage device. This means that an SD device file (for a storage device) is used to connect to the physical device. We need to determine what the next free SD device file is. We can do this by directing the output of df through grep and looking for entries with "sd".

  df | grep / dev / sd 

  Routing df with grep in a terminal window

This computer uses a single SD device file. This is / dev / sda . The next SD device file output is / dev / sdb . In other words, when we connect the floppy drive to the computer, Linux uses / dev / sdb to create a connection to the floppy drive.

We communicate mount to mount the file system on the diskette in the floppy disk drive connected with / dev / sdb to the mount point / mnt .

Insert the floppy disk into the floppy disk drive and connect the floppy disk 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 Names

We can use the characters -l ] (label) option with mount to find out which label is attached to a file system. Labels are no more than any name. They have no functional purpose.

We use the option -t (type) to request mount to report only about vfat file systems. [19659015] mount -l -t vfat

  mount with the -l label option in a terminal window

The label is in square brackets at the end of the listing. The name for this floppy disk drive is NORTUN.

The floppy drive is accessed via the mount point / mnt .

  cd / mnt 
  ls 
  ls -l AMATCH.C 

  Contents of a disk in a terminal window

The disk contains source code files in C language. The date stamp of a file shows that it was last modified in October 1992. She is probably older than many of our readers. (Of course, the meaning of NORTUN as a term is lost in the fog of time.)

If we repeat our command df passed through the command grep to SD device files We will see that there are now two of them.

  df | grep / dev / sd 

  Routing df by grep with a floppy disk drive mounted in a terminal window

Our floppy drive is displayed as bound in / dev / sdb . The file system on the disk in the drive is mounted at / mnt .

To stop mounting the floppy disk, we use umount and pass the device file as a parameter. [19659015] sudo umount / dev / sdb

  Unmounting a Floppy Disk Drive in a Terminal Window

The umount Lazy Option

What happens if you (or another user) use the file system when unmounting? it? Unpairing fails.

  sudo umount / dev / sdb 

  umount fails in a terminal window

The user's current working directory is in the file system he is trying to provision. Linux is so smart that you do not lose sight of the branch you are sitting on.

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

  sudo umount -l / dev / sdb 
  ls 
  cd ~ 
  ls / mnt [19659142] umount -l lazy option in a terminal window " width="646" height="262"/> 

Although the command umount is issued, the file system is still mounted, and the user can list the files as usual.

Once the user changes the directory to its home directory, the floppy file system is freed and the provisioning is revoked. Attempting to list the files in / mnt does not yield results.

Mounting a Samba Share

Samba is a set of software services that can be used to interchangeably share network shares between Linux and Unix-like operating systems and Windows operating systems.

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

There is a Samba share on a Raspberry Pi connected to the same network as the test computer. It's a directory called Backup that has the Samba name "share." Make an SSH connection to it and look at the contents of the shared directory. The shared directory is located on a USB flash drive 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 
  quitting 

  ssh to a raspberry pi in a terminal window

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

You enter your password and are authenticated. The terminal window prompt changes to pi @ marineville because it is connected to the Raspberry Pi.

You list the contents of the shared directory at / media / pi / USB64 / Backup . The contents are two directories, one with the designation Dave and one with the designation Pat . So now we know what to expect when we activate the Samba share.

You type exit to disconnect from the Raspberry Pi, and the command prompt changes back to dave @ howtogeek . [19659004] To use Samba you must install the package cifs-utils .

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

  sudo apt-get install cifs-utils 

  Installing Cifs in a Terminal Window

After completing the installation, hang the share with a command like the following, which specifies the IP address, the share name and adjust the deployment point (which must already exist) to your circumstances.

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

  Mount a samba share in a terminal window

Let's break down the parts of this command.

  • -t cifs ]: The file system type is cifs.
  • -o credentials = / etc / samba / creds, uid = 1000, gid = 1000 : The option parameters are the path to a file named creds which is secured and the username and contains the password for the Raspberry Pi user; The user ID (UID) and group ID (GID) used to set the owner and group of the root directory 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 named Backup . However, the name of the Samba share is share .
  • / media / dave / NAS : The name of the mount point. You must create your deployment point in advance.

Accessing our mount point at / media / dave / NAS we access the shared directory on the Raspberry Pi through the network. We can pat the two folders on the Raspberry Pi with the names dave and .

  cd / media / dave / NAS 

  accessing shared Samba directories in a terminal window ]

Creating and mounting a file system

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

With the option if (inputfile), we inform dd that the stream to be used by is nullable from / dev / zero as an input file.

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

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

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

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

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

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

With the command mkfs we can create a working file system in the file geek_fs . With the option -t (type) you can select the file system type . We create a ext4 system.

  mkfs -t ext4 ./geek_fs[19659184lightboxesthemkfscommandinaterminalwindow"width="646"height="292"/>

That's all it takes to have a working filesystem.

Hang it up / media / dave / geek and then use chown to assign the owner and group owners to access it.

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

  Mounting and setting access to a file system in a terminal window

Does this work? Let's go to the new file system and copy a file to display them.

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

  Searching the File System in a File in a Terminal Window

We were able to change the directory to the new file system and have successfully created a copy of / etc /. fstab file. It works!

If you use mount to list the attached file systems, but limit the output to -t (type) to ext4 We will see that there are now two mounted file systems ext4 .

  mount -t ext4 

  Listing ext4 file systems in a terminal window

Re-mounting a file system

Re-attaching a file system adds option -o to re-mount used. Typically, a file system is changed from a read-only (test) state to a read-only (production) state.

Hang the floppy drive again. This time we use the -r flag (read-only). Then we redirect mount through grep and look at the details of the floppy disk file system.

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

  Remounting a file system in a terminal window

As you can see from the ro highlighted, the file system is read-only

] -o remount With the option rw (Read / Write) allows us to unmount and mount the file system with the new settings in a single command.

  sudo mount -o remount, rw / mnt 

The repetition of the piping from mount to grep shows that ro by rw has been replaced (highlighted). The file system is now in read / write mode.

  mount | grep / mnt 

(Not) Moving a File System

Previously, you could unmount a file system with a single command and reattach it to another mount point.

The -M (move) option in mount is specifically designed to enable you to do this. However, it no longer works in Linux distributions that were switched to systemd . And those are the biggest names.

If we try to move a file system from / mnt to / geek it will fail and the error shown below will be displayed. The attempt to list the files in the files ystem to ./ geek does not lead to any result.

  sudo mount -M / mnt ./geek[19659016[19459087<movingafilesysteminaterminalwindow" width="646" height="197"/> 

You can work around this problem by using the previously used -B (bind) option bind the original mount point to the new mount point.

  sudo mount -B / mnt ./geek[19659016[ls/geek[19659208<Alsoiftheoriginalmountpointisnotenableditwillhavethesamepracticalresult[196659009]Closing notes 

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

  1. It can have unpredictable behavior.
  2. It was not consistent and had to be repeated every time it was restarted.

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

Apart from the systemd problems with the option -M (moving), you should uncomplicated the use of mount and umount Find. These are great commands to keep track of a corrupted system and reassemble the file system by hand.


Source link