The mount command in Linux allows users to mount storage devices, such as hard disk partitions, USB drives, and network file systems, to a specified directory so that the contents can be accessed. The following describes how to use the mount command.
The basic syntax of the mount command is as follows:
mount [Options] [Device] [Mount point]
Note Device refers to the storage device to be mounted, for example, /dev/sdb1 (hard disk partition) or UUID=1234-5678.
Mount point: The directory to which the device is mounted, such as /mnt or /media/mydisk.
Suppose you currently have a hard disk partition /dev/sdb1 and want to mount it to the /mnt directory:
sudo mount /dev/sdb1 /mnt
sudo runs commands with administrator privileges. /dev/sdb1 indicates the device path. /mnt indicates the mount point.
After the USB drive is inserted, the system automatically assigns a device path (such as /dev/sdc1). You can view the device path using the lsblk command:
lsblk
Then mount it to the specified directory:
sudo mount /dev/sdc1 /media/usb
Assume that there is an NFS server nfs.example.com whose share path is /export/data. Run the following command to mount the server:
sudo mount nfs.example.com:/export/data /mnt/nfs
You can mount an ISO file as a virtual CD-ROM drive for easy access to its contents:
sudo mount -o loop /path/to/image.iso /mnt/iso
-o loop Specifies the use of a loop device to mount an ISO file.
If you want to specify the file system type (such as ext4, ntfs, vfat, etc.), you can use the -t option:
sudo mount -t ntfs /dev/sdb1 /mnt
Use the -o option to set mount parameters, such as read-only mount:
sudo mount -o ro /dev/sdb1 /mnt
Automatic mounting:
sudo mount -o auto /dev/sdb1 /mnt
Set user rights:
sudo mount -o uid=1000,gid=1000 /dev/sdb1 /mnt
Using the mount command without any parameters, you can view all currently mounted devices and file systems:
mount
Or use a more concise output:
df -h
To automatically mount devices when the system starts, you can add the mount information to the /etc/fstab file. Edit /etc/fstab. To open the /etc/fstab file:
sudo nano /etc/fstab
Add the following (using /dev/sdb1 as an example) :
/dev/sdb1 /mnt ext4 defaults 0 2
/dev/sdb1: indicates the device path. /mnt: mount point. ext4: indicates the file system type. defaults: Default mount options. 0: Whether to back up data (0 indicates no backup, 1 indicates backup). 2: indicates the order in which file systems are checked during startup (0 indicates no check, 1 indicates check during startup). After saving the file, the system will automatically mount the device at startup.
Unmount the device using the umount command:
sudo umount /mnt
Be sure to close all programs that are accessing the mount point before uninstalling, otherwise an error will be reported.
Assume that an external hard disk is connected, the device path is /dev/sdb1, and the file system is ntfs:
sudo mount -t ntfs /dev/sdb1 /media/external
To mount an NFS shared directory:
sudo mount -t nfs nfs.example.com:/export/data /mnt/nfs
Mount an ISO file to access its contents:
sudo mount -o loop /path/to/image.iso /mnt/iso
Permission issues: Mounting devices usually requires administrator permissions, so sudo is required.
Device path: Ensure that the device path is correct. You can view the device path by running lsblk or fdisk -l.
File system type: If you are not sure about the file system type, run lsblk -f or blkid to check the file system type.
Automatic mounting: When editing the /etc/fstab file, ensure that the format is correct to prevent system startup failure.
The mount command is an important tool for mounting file systems in Linux. By mastering basic syntax, common options, and automatic mount methods, you can easily manage storage devices and network file systems. Hopefully, this tutorial will help you get better at using the mount command.