linux系统中挂载mount命令、umount命令

时间:2024-03-10 09:24:34

linux系统中硬盘、光驱等联系系统时,并不能直接访问,需要进行挂载,挂载可以理解为将硬盘、光驱设备与系统已有的目录进行关联

挂载是在使用硬件设备前所执行的最后一步操作。只需使用mount命令把硬盘设备或分区与一个目录文件进行关联,然后就能在这个目录

中看到硬件设备中的数据了。

mount命令用于挂载操作

1、查看挂载情况

 df 命名、mount命令、/etc/mtab等可以查看挂载信息,如df、

[root@linuxprobe ~]# df -h  ## 查看挂载信息
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
[root@linuxprobe ~]# tail -n 5 /etc/mtab  ## 查看挂载信息
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
sunrpc /proc/fs/nfsd nfsd rw,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
gvfsd-fuse /run/user/0/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=0,group_id=0 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0

 

 

2、mount挂载操作

格式:mount   挂载设备    挂载点  

挂载点为已存在的目录,如果目录中有内容,挂载后目录内的内容将不可用,只有卸载后才可以重新使用

[root@linuxprobe ~]# df -h  ## 查看挂载前的挂载情况
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
[root@linuxprobe ~]# tail -n 5 /etc/mtab  ## 查看挂载信息
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
sunrpc /proc/fs/nfsd nfsd rw,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
gvfsd-fuse /run/user/0/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=0,group_id=0 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0


[root@linuxprobe ~]# mkdir -p /media/mounttest  ## 创建挂载的测试目录,因为挂载前,准备挂载的目录应事先存在
[root@linuxprobe ~]# mount /dev/cdrom /media/mounttest/  ## 挂载操作, mount 挂载设备  挂载目录  ,/dev/cdrom为挂载设备(光盘),/media/cdrom为挂载目录
mount: /dev/sr0 is write-protected, mounting read-only
[root@linuxprobe ~]# df -h  ## 查看挂载后的挂载情况,可见多出最后一行
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
/dev/sr0                          3.5G  3.5G     0 100% /media/mounttest
[root@linuxprobe ~]# ll /dev/cdrom  ## /dev/cdrom 为 /dev/sr0的软链接
lrwxrwxrwx. 1 root root 3 Oct 23 16:18 /dev/cdrom -> sr0
[root@linuxprobe ~]# tail -n 5 /etc/mtab  查看挂载后信息
sunrpc /proc/fs/nfsd nfsd rw,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
gvfsd-fuse /run/user/0/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=0,group_id=0 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0
/dev/sr0 /media/mounttest iso9660 ro,relatime 0 0

 

 

 

 

 

3、umount 命令用于卸载

 umount格式: umount 挂载目录  或者  umount  挂载设备

[root@linuxprobe ~]# df -h  ## 查看挂载前挂载情况
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
/dev/sr0                          3.5G  3.5G     0 100% /media/mounttest
[root@linuxprobe ~]# umount /media/mounttest/  ## 卸载,umount 挂载目录
[root@linuxprobe ~]# df -h  ## 查看卸载后情况,少了最后一行挂载信息
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
[root@linuxprobe ~]# tail -n 5 /etc/mtab  ## 查看卸载后的情况,少了最后一行挂载信息
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
sunrpc /proc/fs/nfsd nfsd rw,relatime 0 0
/dev/sda1 /boot xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
gvfsd-fuse /run/user/0/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=0,group_id=0 0 0
fusectl /sys/fs/fuse/connections fusectl rw,relatime 0 0

 

4、umount + 挂载设备进行卸载

[root@linuxprobe ~]# mount /dev/cdrom /media/mounttest/  ## 先进行挂载
mount: /dev/sr0 is write-protected, mounting read-only
[root@linuxprobe ~]# df -h  ## 查看
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
/dev/sr0                          3.5G  3.5G     0 100% /media/mounttest
[root@linuxprobe ~]# umount /dev/cdrom  ## umount + 挂载设备进行卸载
[root@linuxprobe ~]# df -h  ##  查看
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot

 

 5、挂载目录如果有文件,挂载之后文件将不可用,因此挂载目录通常是空目录

[root@linuxprobe ~]# touch /media/mounttest/test{01..20}.txt   ### 在挂载目录常见测试文件
[root@linuxprobe ~]# ls /media/mounttest/  ## 查看
test01.txt  test03.txt  test05.txt  test07.txt  test09.txt  test11.txt  test13.txt  test15.txt  test17.txt  test19.txt
test02.txt  test04.txt  test06.txt  test08.txt  test10.txt  test12.txt  test14.txt  test16.txt  test18.txt  test20.txt
[root@linuxprobe ~]# df -h  ## 查看挂载情况
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
[root@linuxprobe ~]# mount /dev/cdrom /media/mounttest/  ## 挂载
mount: /dev/sr0 is write-protected, mounting read-only
[root@linuxprobe ~]# df -h  ## 查看挂载情况
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
/dev/sr0                          3.5G  3.5G     0 100% /media/mounttest
[root@linuxprobe ~]# ls /media/mounttest/  ## 所有的*.txtw文本消失
addons  EULA  images    LiveOS      Packages       repodata                 RPM-GPG-KEY-redhat-release
EFI     GPL   isolinux  media.repo  release-notes  RPM-GPG-KEY-redhat-beta  TRANS.TBL
[root@linuxprobe ~]# umount /media/mounttest/  ## 卸载
[root@linuxprobe ~]# ls /media/mounttest/  ## *.txt文本出现
test01.txt test03.txt test05.txt test07.txt test09.txt test11.txt test13.txt test15.txt test17.txt test19.txt
test02.txt test04.txt test06.txt test08.txt test10.txt test12.txt test14.txt test16.txt test18.txt test20.txt

 

6、对于经常使用的设备,可以设置为开机自动挂载,修改 /etc/fstab  配置文件即可

[root@linuxprobe ~]# cat /etc/fstab ## 查看当前情况

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0

其每行的格式为:

要挂载的设备或伪文件系统    挂载点    文件系统类型    挂载选项   转储频率   自检次序

UUID=6efb8a23-bae1-427c-ab10-3caca95250b1 /boot  xfs    defaults    0 0

    要挂载的设备或伪文件系统:设备文件、LABEL(LABEL="")、UUID(UUID="")、伪文件系统名称(proc, sysfs)

    挂载点:指定的文件夹

   挂载选项:defaults

    转储频率:

      0:不做备份

      1:每天转储

      2:每隔一天转储

    自检次序:

      0:不自检

      1:首先自检;一般只有rootfs才用1;

 

linux就该这么学p120

 

 

使用blkid查看要挂载设备的文件系统:

[root@linuxprobe ~]# blkid /dev/cdrom
/dev/cdrom: UUID="2014-05-07-03-58-46-00" LABEL="RHEL-7.0 Server.x86_64" TYPE="iso9660" PTTYPE="dos"

 

 

 

 

将 /etc/cdrom设置为开机自动挂载:

[root@linuxprobe ~]# mount /dev/cdrom /media/mounttest/  ## 挂载
mount: /dev/sr0 is write-protected, mounting read-only
[root@linuxprobe ~]# df -h  ## 查看
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.9M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
/dev/sr0                          3.5G  3.5G     0 100% /media/mounttest
[root@linuxprobe ~]# cat /etc/fstab  ## 查看修改前配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot xfs defaults 1 2
/dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0
[root@linuxprobe ~]# echo -e "/dev/cdrom\t/media/mounttest\tiso9660\tdefaults\t0\t0" >> /etc/fstab  ## 修改 /etc/fstab配置文件, 也可以使用vim编辑器直接修改
[root@linuxprobe ~]# cat /etc/fstab  ## 查看修改后的配置文件,多处最后一行

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot xfs defaults 1 2
/dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0
/dev/cdrom /media/mounttest iso9660 defaults 0 0

 

 

测试自动挂载情况(关机、开机即可,实际情况是每次启动系统,系统自动执行了mount -a 命令)

[root@linuxprobe ~]# cat /etc/fstab  ## 查看开机自启配置文件

#
# /etc/fstab
# Created by anaconda on Thu Oct 15 18:36:35 2020
#
# Accessible filesystems, by reference, are maintained under \'/dev/disk\'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel_linuxprobe-root /                       xfs     defaults        1 1
UUID=32635a67-1a0f-4df4-907f-f9bf12f87488 /boot                   xfs     defaults        1 2
/dev/mapper/rhel_linuxprobe-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/mounttest        iso9660 defaults        0       0
[root@linuxprobe ~]# df -h  ## 查看挂载情况
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.8M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
[root@linuxprobe ~]# mount -a  ## 开机挂载
mount: /dev/sr0 is write-protected, mounting read-only
[root@linuxprobe ~]# df -h  ## 查看挂载情况
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.4G   15G  20% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  994M   1% /dev/shm
tmpfs                             994M  8.8M  986M   1% /run
tmpfs                             994M     0  994M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot
/dev/sr0                          3.5G  3.5G     0 100% /media/mounttest

 

参考:https://blog.51cto.com/13869554/2154724

        https://blog.csdn.net/daydayup654/article/details/78788310