Linux common commands - mount command (1)

Mounting is the process of linking the device file name with the created empty directory, which is called mounting.

1. Basic format of mount command

All storage devices in Linux must be mounted before they can be used, including hard disks.

  • Command name: mount
  • The path where the command is located: /bin/mount
  • Execute permissions: all users

The specific format of the command is as follows:

#查询系统中已经挂我的设备,-l会显示卷标名称
[root@localhost ~ ] # mount [-l]

2. Practice:

Entering the command directly mountis to query the mounted devices that already exist in the system.

[root@localhost ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

其中只有
/dev/sda3 on / type ext4 (rw)-->/dev/sda3挂载到根目录下,文件系统是ext4,权限是读写。
/dev/sda1 on /boot type ext4 (rw)
有用,其他都是干扰项.

还要注意,swap分区是不需要挂载点的,是给系统内核直接访问的分区,mount命令是查看不到的。

3. mount -aOrder

#依据配置文件/etc/fstab的内容,自动挂载
[root@localhost ~ ] # mount -a

The a of -a is auto, which means automatic.
The hard disk of the Linux system is automatically mounted at startup. However, mobile storage devices such as CDs and U disks are not recommended to be automatically mounted after booting.
If the removable storage devices such as CD and U disk are set to automatically mount when booting, once you forget to put the CD or U disk when starting up, the system will unconditionally search for this partition. If it cannot find it, the system will fail to start. Such errors are actually not difficult to fix, but they must be operated on this machine.
The automatic mounting of the Linux system is carried out automatically according to the /etc/fstab file. This file is a very fragile file. Once this file is written incorrectly, the system will start reporting an error.
[root@localhost ~]# vim /etc/fstab

The mount -acommand can be regarded as scanning the /etc/fstab file to see if there is any typo in the content, and if it is wrong, an error will be reported.
In Linux systems after Red Hat 6, the fault tolerance of the /etc/fstab file has been significantly enhanced. Before Red Hat 5, a misspelling of a letter was not allowed. After Red Hat 6, except for the key position information, no errors will be reported, and other errors will not be reported. Therefore, sometimes when scanning with commands, errors may not occur, so you cannot blindly trust the results of mount -acommands mount -a. Just remember this. (I will talk about how to fix the file if there is an error later)

4. Mount command format

[root@localhost ~ ] # mount [-t 文件系统] [-L卷标名] [-o特殊选项] \ 
设备文件名 挂载点

注意:在Linux系统中\ 的意思表示换行符, 代表上边两行是一行内容。

选项:
-t文件系统:加入文件系统类型来指定挂载的类型,可以ext3、ext4、iso9660等文件系统。(不写也没事,Linux系统默认光盘U盘都能自动识别)。
-L卷标名:挂载指定卷标的分区,而不是安装设备文件名挂载。(现在基本上用不到了,可以不关注。)
-o特殊选项:可以指定挂载的额外选项,比如读写权限、同步异步等,如果不指定则默认值生效。

example

[root@localhost ~ ] # mount -t iso9660 /dev/sr0 /mnt/cdrom/

-o special option description

Let's first check the /etc/fstab file mentioned in the previous article, the automatic mount configuration file of the Linux system.

The red box in the above figure is the default permission for partition mounting. The following details the permissions to mount.

  • atime/noatime: update access time/do not update access time. Whether to update the access time of the file when accessing the partition file, the default is to update.
  • async/sync: asynchronous/synchronous, the default is asynchronous.
  • auto/noauto: automatic/manual, when the mount-a command is executed, whether the content of the /etc/fstab file will be automatically mounted, and the default is automatic.
  • exec/noexec: Execute/not execute, set whether to allow executable files to be executed in the file system, the default is exec to allow.
  • rw/ro: read-write/read-only, whether to have read-write permission when the file system is mounted, the default is rw.
  • suid/nosuid: With/without SUID authority, set whether the file system has SUID and SGID authority, the default is yes.
  • user/nouser: Allow/disallow ordinary users to mount, set whether the file system allows ordinary users to mount, the default is not allowed, only root can mount partitions.
  • defaults: Define the default value, which is equivalent to the seven options of rw, suid, dev, exec, auto, nouser, and async.
  • remount: Remount the mounted file system, generally used to specify and modify special permissions.
  • usrquota: Write means that the file system supports user disk quota, which is not supported by default.
  • grpquota: Write means that the file system supports group disk quotas, which are not supported by default.
Note:
The defaults permission represents the default value among the top 7 permissions. This default value generally does not need to be modified, and the default option is fine.
For exec/noexec example, if you select noexec, the files in the entire partition cannot be executed. If the root directory is defined as noexec, the entire system cannot be started, even if you want to modify it, because the mount command cannot be executed.

Exercise: Explain exec/noexecOptions

#1、查看系统中已经挂载的文件系统,注意有虚拟文件系统
#命令结果是代表:/dev/sda3分区挂载到/目录,文件系统是ext4,权限是读写
[root@localhost] # mount
/dev/sda3 on/type ext4(rw)proc on/proc type proc(rw)
sysfs on/sys type sysfs(rw)
devpts on/dev/pts type devpts(rw,gid=5,mode=620)
tmpfs on/dev/shm type tmpfs(rw)
/dev/sdal on/boot type ext4(rw)
none on/proc/sys/fs/binfmt_misc type binfmt_misc(rw)
sunrpe on/var/lib/nfs/rpe_pipefs type rpc_pipefs(rw)


#2、修改特殊权限
#我们查看到/boot分区已经被挂载,而且采用的defaults 选项,那么我们重新挂载分区,并采用 noexec
[root@localhost ~ ] # mount -o remount,noexec /boot(临时生效)

然后用mount命令查询一下分区,boot分区多了noexec权限
/dev/sdal on /boot type ext4 (rw,noexec)

#权限禁止执行文件执行,看看会出现什么情况(注意不要用根分区做试验,#不然系统命令也不能执行了)。
# 执行一个shell脚本
[root@localhost boot]#./hello.sh
-bash:./hello.sh:权限不够

# 再修改回来权限,就可以执行了。
[root@localhost ~ ] # mount -o remount,exec /boot

This exercise also has to remember remountwhat it does and how to use it.

3. CD mount

The premise of CD mounting is still to specify the device file name of the CD. Different versions of Linux have different device file names:

  • For systems before CentOS5.x, the CD device file name is /dev/hdc
  • For systems after CentOS6.x, the CD device file name is /dev/sr0

No matter which system has a soft connection /dev/cdrom, and the device file name that can be used as a CD.

You can see that /dev/cdrom is the soft link of /dev/sr0 (equivalent to the shortcut of Windows system). It is recommended to use the source file dev/sr0.
The Linux system provides us with three empty folders by default in the root directory:

  • media (recommended to mount the CD)
  • misc (network storage disk is recommended)
  • mnt (recommended to hang U disk or mobile hard disk).

I am used to creating empty directories in the mnt folder to mount external devices. The following summarizes the steps to mount the disc.
1. Create an empty directory named cdrom in the mnt folder

[root@localhost /]# mkdir /mnt/cdrom

It is not recommended to create too many first-level directories in the root directory, which is inconvenient for management.
2. Find the device file name of the CD.
For systems after CentOS6.x, the device file name of the CD is /dev/sr0. This is fixed, just remember it.
In the system before CentOS5.x, the file name of the CD device is /dev/hdc, because in the system long ago, the hard disk and the CD system were uniformly named. It was thought that the system had two hard disks, hda and hdb, and the default recognition of the CD was hdc. But the demand for storage space is getting bigger and bigger, resulting in two hard disks can not meet the demand, you need to continue to add new hard disks, at this time, the recognition of the device file name of the CD will automatically change backwards, if there are three hard disks, the device file name of the CD is hdd.
At this time, it will lead to the fact that the device file of the CD is not fixed, which will cause me to mount the CD on a computer, so I have to first check what the device file name of the CD is, and then mount it. Therefore, in the system after CentOS6.x, the device file name of the CD is fixed as sr0, the device file name of the first CD-ROM is sr0, the device file name of the second CD-ROM is sr1, and so on. The advantage of this is that it has nothing to do with the device file name of the hard disk.
3. Mount the disc

[root@localhost ~ ] # mount -t iso9660 /dev/sr0 /mnt/cdrom/

注意:
-t文件系统:加入文件系统类型来指定挂载的类型,
可以ext3、ext4、iso9660等文件系统。
挂载光盘就用iso9660类型的文件系统类型。
也可以直接省略-t iso9660,
挂载光盘时候系统自动添加。

[root@localhost ~ ] # mount /dev/sr0 /mnt/cdrom/

Executing the above command will report an error, saying that you must specify the file system. as follows:

[root@localhost /]# mount /dev/sr0 /mnt/cdrom/
mount: you must specify the filesystem type

We add the file system and execute it again. as follows:

[root@localhost /]# mount -t iso9660  /dev/sr0 /mnt/cdrom/
mount: no medium found on /dev/sr0

This time the error message is accurate, telling us that no disc was found in /dev/sr0.
You need to add a cd file to the virtual machine software and put it in the CD-ROM drive.

Enter the CD mount command again.

[root@localhost /]# mount /dev/sr0 /mnt/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only

There is another line that reports an error saying that /dev/sr0 wants to read and write permissions, but it is now given read-only permissions. Because the use of the disc is write-once, after that it can be read-only. So this line reports an error, which can be considered as a sign that the disc is mounted correctly.
Finally execute mountthe command to confirm.

4. To access the data in the closed disk,
you need to go to the mount point to access the data in the CD, which is in the /mnt/cdrom/ directory.

Here are the contents of the disc.
5. Uninstall the mount point
Whether it is a mounted CD or a USB flash drive, it needs to be uninstalled after use. If you change the cd without uninstalling, there will be problems. There is a certain probability, if you don't uninstall and change the CD several times, you will find that no matter what disc you put in, the data you read out is not the data in the CD you put in. It is the content in the tool vmtools that comes with the virtual machine. After that, even if you uninstall the disc, it will no longer work.
The solution at this time is to remove the existing CD-ROM drive in the virtual machine and add a new CD-ROM drive.

Unmount the mount point

#因为设备文件名和挂载点已经连接到一起,卸载哪一个都可以
[root@localhost ~ ] # umount /dev/sr0
[root@localhost ~ ] # umount /mnt/cdrom
卸载
[ root@ localhost cdrom]# umount /dev/sr0
umount:/mnt/cdrom: device is busy.
(In some cases useful info about processes that use the device is found by 1sof(8) or fuser(1))

上边报错说设备正忙,以为此时位置正在光盘里边cdrom目录中,要退出在进行卸载。
注意:卸载的时候需要退出光盘目录,才能正常卸载。

6. Why use an empty directory for the mount point

#在/mnt/cdrom目录中创建两个文件abc,bcd
[root@localhost cdrom] # ls
abc  bcd

#在/mnt/cdrom目录上挂载光盘
[root@localhost cdrom] # mount /dev/sr0 /mnt/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
可以看到看到光盘是可以挂载到有文件的目录上。

#查看/mnt/cdrom目录中的文件
[root@localhost cdrom]# ls /mnt/cdrom/
CentOS_BuildTag  isolinux                  RPM-GPG-KEY-CentOS-Debug-6
EFI              Packages                  RPM-GPG-KEY-CentOS-Security-6
EULA             RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Testing-6
GPL              repodata                  TRANS.TBL
images           RPM-GPG-KEY-CentOS-6
文件夹中并没有之前的abc,bcd文件了。而只有光盘中的数据。

#把光盘挂载点卸载了
[root@localhost cdrom]# umount /dev/sr0 

#再次查看/mnt/cdrom目录中的文件
[root@localhost cdrom]# ls /mnt/cdrom/
abc  bcd
发现abc,bcd文件又出现了。

It is because when /mnt/cdrom is used as a directory, there are blocks inside to store data. When I use /mnt/cdrom as a mount point, I use this directory as an intervention point to enter another partition (the CD is also a storage space, which is an independent space), so the access point of /mnt/cdrom as a directory is no longer there, but the data inside is not deleted, so once the /mnt/cdrom mount point is uninstalled, the /mnt/cdrom directory returns to the original location, and the data in the original file can be read again.
This is why it is necessary to use an empty directory as a mount point, because once a directory with data is used as a mount point, the data in the original directory cannot be viewed or deleted, which takes up system resources and is unreasonable.

Focus: Supporting learning materials and video teaching

So here I have also carefully prepared the detailed information of the above outline, which is linked below

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/126273936