Linux mount Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mount command to attach file systems to specific locations in the directory structure. The lab covers the purpose and syntax of the mount command, as well as examples of mounting both local and remote file systems. You will learn how to mount an ext4 file system on a local device and how to mount a remote file system using NFS (Network File System).

The lab provides a step-by-step guide to help you understand the mount command and its practical applications in managing file systems on a Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/grep -.-> lab-422822{{"`Linux mount Command with Practical Examples`"}} linux/mkdir -.-> lab-422822{{"`Linux mount Command with Practical Examples`"}} linux/sudo -.-> lab-422822{{"`Linux mount Command with Practical Examples`"}} linux/df -.-> lab-422822{{"`Linux mount Command with Practical Examples`"}} linux/mount -.-> lab-422822{{"`Linux mount Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the mount Command

In this step, we will learn about the purpose and syntax of the mount command in Linux. The mount command is used to attach a file system to a specific location in the Linux directory structure, making it accessible to the system and users.

The basic syntax of the mount command is as follows:

sudo mount [-t type] [-o options] device directory

Here's what each part of the syntax means:

  • sudo: Runs the mount command with elevated privileges to access and mount file systems.
  • -t type: Specifies the file system type, such as ext4, nfs, vfat, etc.
  • -o options: Provides additional options for the mount operation, such as ro (read-only), rw (read-write), noatime, etc.
  • device: Represents the device file or network resource to be mounted, such as a partition, volume, or NFS share.
  • directory: Specifies the mount point, which is the location in the file system where the device will be attached.

Let's try a simple example of mounting a local file system:

sudo mount -t ext4 /dev/sdb1 /mnt

Example output:

No output, as the mount operation was successful.

In this example, we mount the ext4 file system on the /dev/sdb1 device to the /mnt directory.

Mount a Local File System

In this step, we will learn how to mount a local file system on your Linux system.

First, let's create a new directory to use as the mount point:

sudo mkdir /mnt/local

Now, let's mount an existing local file system to the mount point. For this example, we'll use the /dev/sdb1 device and mount it as an ext4 file system:

sudo mount -t ext4 /dev/sdb1 /mnt/local

Example output:

No output, as the mount operation was successful.

To verify that the file system is mounted correctly, you can use the mount command:

mount | grep /mnt/local

Example output:

/dev/sdb1 on /mnt/local type ext4 (rw,relatime)

This output shows that the /dev/sdb1 device is mounted on the /mnt/local directory as an ext4 file system.

Mount a Remote File System Using NFS

In this step, we will learn how to mount a remote file system using the Network File System (NFS) protocol.

First, let's create a new directory to use as the mount point for the remote NFS share:

sudo mkdir /mnt/nfs

Now, let's mount the remote NFS share to the /mnt/nfs directory. For this example, we'll use the NFS server address 192.168.1.100:/shared and mount it as an nfs file system:

sudo mount -t nfs 192.168.1.100:/shared /mnt/nfs

Example output:

No output, as the mount operation was successful.

To verify that the NFS file system is mounted correctly, you can use the mount command:

mount | grep /mnt/nfs

Example output:

192.168.1.100:/shared on /mnt/nfs type nfs (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.100,mountvers=4,mountport=20048,mountproto=udp,local_lock=none,addr=192.168.1.100)

This output shows that the remote NFS share 192.168.1.100:/shared is mounted on the /mnt/nfs directory.

Summary

In this lab, we learned the purpose and syntax of the mount command in Linux, which is used to attach a file system to a specific location in the directory structure. We practiced mounting a local file system by creating a new mount point directory and using the mount command with the appropriate options. We also verified the successful mount operation using the mount command. Additionally, we explored mounting a remote file system using NFS, which allows accessing shared directories over the network.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like