Chat Zalo Chat Messenger Phone Number Đăng nhập
Linux mount Command with Examples - phoenixNAP

Linux mount Command with Examples – phoenixNAP

Introduction

The Linux file system hierarchy is organized in a tree, with the file system starting from the root (/) directory. All other child file systems branch out from the root directory.

The mount command allows users to mount, that is, connect additional secondary file systems to a particular mount point on the currently accessible file system. The command passes the mounting instructions to the kernel, which completes the operation.

This tutorial will teach you the basics of connecting file systems on Linux using the mount command.

Prerequisites

A system running Linux.

  • Terminal access (Ctrl + Alt + T).
  • A user account with administrator privileges.
  • Linux

mount command syntax

The standard mount command syntax is: mount

-t [type] [device] [dir]

The command instructs the kernel to connect the file system located in [device] in the [dir] directory. The -t [type] option is optional and describes the file system type (EXT3, EXT4, BTRFS, XFS, HPFS, VFAT, and so on).

If the destination directory is omitted, mounts the file systems listed in the /etc/fstab file.

While the file system is mounted, the previous content, owner, and mode of the [dir]

directory are invisible, and the path name [dir] refers to the root of the file system. Output status The mount command returns one of the following values indicating the completion status of the process: 0.

  • Success.
  • 1. Incorrect invocation of commands or insufficient permissions.
  • 2. System error.
  • 4. Internal mount error.
  • 8. Operation interrupted by the user.
  • 16. Problems writing or locking the /etc/mtab file.
  • 32. Assembly failure.
  • 64. At least one assembly operation was successful, but not all.

mount command options The mount

command options also specify file system types, mount location, and type. The following table shows the most common mounting options:

-a-F

mount-a-h-l-L [label][label]-M-O [opts]-a-a[opts]-r-R-t [type]T-v -V

Run the mount man command for a complete list of file system-specific options, syntax forms, and mount options

.

Examples of Linux mount commands

The following describes the most common use cases for the

mount command. List of mounted file systems Run the mount command without any option to display all currently

mounted file systems

. The output also shows mount points and mounting options.

For example:

List specific

file systems

The -t option allows users to specify which file systems to display when running the mount command. For example, to display only ext4 file systems, run the following command:

mount -t ext4

Mount

a file system Mounting a file system

requires the user to specify the directory or mount point to which the file system will connect. For example, to mount the /dev/sdb1 file system in the /mnt/media directory, run: sudo mount /dev/sdb1 /mnt/media

To specify additional file system-specific mount options, pass the -o flag before the device name. Use the following syntax:

mount -o [options] [device] [dir]

See the man page or help file for a complete list

of available options. Mount File System with /etc/fstab The /

etc/fstab

file

contains lines that describe the mounting location of system devices and the options they are using. Generally, fstab is used for internal devices, such as CD/DVD devices and network shares (samba/nfs/sshfs). Removable devices are usually mounted by the gnome-volume-manager.

Providing only one parameter (either [dir] or [device]) causes mount to read the contents of the /etc/fstab configuration file to check if the specified file system appears in it. If the given file system is listed, mount uses the value for the missing parameter and the mount options specified in the /etc/fstab file.

The structure defined in /etc/fstab is: <file system> <mount point> <type> <options> <dump> <pass>

The following screenshot shows the contents

of the /etc/fstab file: To mount

a file system specified in the /etc/fstab file, use one of the following syntax:

mount [options] [dir] mount [options] [device] For [dir]

  • , specify the mount point.
  • For [device], specify the device ID.

See the man page for the mount or run man mount command for a complete list of file system-specific and filesystem-independent options.

Mount USB Drive

Modern Linux distributions automatically mount removable drives after insertion. However, if the automatic mounting fails, follow the steps below to mount the USB drive manually:

1. Create a mount point using the mkdir command: mkdir

/media/usb-drive

2. Locate the USB device and file system type. Run:

fdisk -l

3. Using the device identifier of the fdisk output, mount the USB drive with the following syntax

: sudo mount [identifier] /media/usb-drive For example, if the device appears as /dev/sdb1, run: sudo mount /

dev/sdb1 /media/usb-drive Mount

a CD-ROM Being a removable device, Linux also automatically mounts

CD-ROMs

. However, if mounting fails, mount a CD-ROM manually by running:

mount -t iso9660 -o ro /dev/cdrom /mnt Make sure that the

/mnt mount point exists for the command to work. If not, create one using the mkdir command.

ISO9660 is the standard file system for CD-ROM, while the -o ro options make mount treat it as a read-only file system.

Mount files

ISO

Mounting an ISO file requires mapping its data to a loop device. Attach an ISO file to a mount point using a loop device by passing the loop option

-or: sudo mount /image.iso /media/iso-file -o loop

Mount

an NFS

A Network File System (NFS) is a distributed file system protocol for sharing remote directories over a network. Mounting an NFS allows you to work with remote files as if they were stored locally.

Follow the steps below to mount a remote NFS directory on your system:

1. Create a mount point using the command mkdir: sudo mkdir

/media/nfs

2. Mount the NFS share by running:

sudo mount /media/nfs

3. To automatically mount the remote NFS share at boot, edit

the /etc/fstab file using a text editor of your choice: sudo vi /etc/fstab Add the following line to the file and replace remote.server:/dir

with the IP address or host name of the NFS server and the exported directory: remote.server:/dir

/media/nfs nfs defaults 0 0 Mount without superuser Although only one

superuser

file systems in the /etc/fstab file containing the user option can be mounted by any user of the system.

Edit the /

etc/fstab file using a text editor and in the <options> field, specify the user option. For example:

/dev/cdrom /cd iso9660 ro,user,noauto,unhide Adding the above line to /etc/fstab allows any user on the system to mount the

iso9660 file system from a CD-ROM device

.

Specifying the users instead of user option allows any user to unmount the file system, not just the user who mounted it.

Move

a mount

If you decide to move a mounted file system to another mount point, use the -M option. The syntax is:

mount -move [olddir] [newdir]

For [olddir], specify the current mount point. For [newdir], specify the mount point to which you want to move the file system.

When you move the mounted file system to another mount point, its contents appear in the [newdir] directory, but you do not change the physical location of the files.

How to Unmount

a File System

To unmount, that is, detach a connected file system from the System Tree, use the umount command. Disconnect the file system by passing its mount point or device name.

The syntax is

: umount [dir] or umount [device] For example, to detach a USB device listed as /dev/sdb1,

run: umount /dev/sdb1

While

busy with open files or ongoing processes, a file system cannot be separated and the process fails. If you are not sure what the file system is using, run the fuser command to find out: fuser

-m [dir

] For [dir], specify the mount point of the file system. For example:

fuser -m /media/usb-drive

The output lists the PIDs of the processes currently accessing the device. Stop the processes and unmount the file system.

Deferred

unmount

If you do not want to stop processes manually, use deferred unmount, which instructs the unmount command to detach the file system as soon as its activities are stopped. The syntax is:

umount -lazy [device]

Forced unmount

The –f (-force) option allows users to force a dismount. However, be careful when forcing a file system to be unmounted, as the process may corrupt the data it contains.

The syntax is:

umount -f [dir]

Conclusion

This tutorial showed how to use the mount command to attach multiple file systems to the directory tree and provided other practical examples. The tutorial also showed how to use the umount command to separate a file system.

Next, we recommend that you read about the fsck command to check and repair file systems, or see how to check disk space in Linux.

Contact US