linux命令:mount和umount

Linux的使用过程中,我们应该接触或听过“挂载”、“挂载点”等词,刚开始接触时,理解很含糊,遂这篇文章呼之欲出,为的就是记录对“挂载”的学习轨迹和理解,方便查阅和回顾。

一、概念

Linux只有一个根目录(/),只能访问到属于根目录下的文件,而当文件不在根目录下时,如何才能访问呢??处理方式就是“挂载”!!即为将该文件关联到根目录下的某个目录,这种关联就是“挂载(mount)”,关联到的目录也就是“挂载点”,当我们要解除这种关联的操作就叫“卸载(umount)”。

1、对比下Windows上的挂载概念:
Windows上的“挂载”就是给磁盘分区提供一个盘符(C/D/E/G…)。例如我们将移动U盘插入系统后,系统会自动给该U盘分配一个盘符(可能是D/E…),盘符其实就是挂载,弹出U盘的操作即为“卸载”。

2、“挂载点”的目录需要满足以下要求:
(1)目录事先存在,可以用mkdir命令新建目录;
(2)挂载点目录不可被其他进程使用到;
(3)挂载点下原有文件将被隐藏。

3、在这里也对Linux中隐藏文件的相关知识简短概括:
(1)查看隐藏文件

lsattr

(2)删除隐藏文件

# 先通过chattr命令将文件的隐藏权限取消,再执行删除即可。
chattr -al filename
rm -f filename

4、这里也介绍下Linux中分区和目录的概念区分
(1)区别:
Linux的分区是物理上的概念,从物理上将存储空间分开;Linux的目录是逻辑上的概念,Linux的目录树实际上是一个分区之间的数据逻辑结构关系,不是物理结构;
(2)联系:
一个分区必须挂载在一个目录下才能使用,分区可以挂载到任何目录。
(3)总结:
分区是真正存放数据的地方,只有一份数据。目录是分区数据的逻辑映射,就像Windows系统中的快捷方式一样,分区的数据可以挂载到任意多个不同目录,这些目录就像不同名的快捷方式,都指向同样的分区数据
(4)应用:
手动挂载/卸载:
mount /dev/sda1 /paul(挂载分区/dev/sda1到/paul目录)
umount /paul(卸载/paul目录的分区)
开机自动挂载:
修改/etc/fstab配置文件,末尾添加一行。
/dev/sda1 /paul ext4 defaults 0 0

二、挂载(mount)与卸载(umount)命令详解

1.mount命令
用于将指定文件加载(挂载)到指定的加载点(目录),这个过程也叫挂载
注意:查看内核追踪到的已挂载的所有设备:cat /proc/mounts

(1)语法
mount [-t vfstype] [options] [parameter]

(2)vfstype(文件系统)说明:
-t vfstype 指定文件系统的类型,通常不必指定。mount 会自动选择正确的类型。

系统说明
iso9660光盘或光盘镜像
msdosDOS fat16文件系统
vfatWindows 9x fat32文件系统
ntfsWindows NT ntfs文件系统
smbfsMount Windows文件网络共享
nfsUNIX(LINUX) 文件网络共享

(3)options说明:

选项说明
-V显示版本
-l显示已加载的文件系统列表
-h显示帮助信息并退出
-v冗长模式,输出指令执行的详细信息
-n加载没有写入文件“/etc/mtab”中的文件系统
-r将文件系统加载为只读模式
-a自动挂载所有支持自动挂载的设备;(定义在了/etc/fstab文件中,且挂载选项中有“自动挂载”功能

(4)parameter说明:

选项说明
设备文件名(device)指定要加载的文件系统对应的设备名
加载点(dir)指定加载点目录

(5)实例:

mount /a/aa.txt /b
#将aa.txt挂载到b目录
mount  -t nfs  192.168.x.x:/a  /b
#nfs挂载

2.umount命令
Linux系统中,umount命令用于卸载已经加载的文件系统,利用设备名或挂载点都能umount文件系统,不过最好还是通过挂载点卸载,以免使用绑定挂载(一个设备,多个挂载点)时产生混乱现象。

(1)语法
umount [options] [parameter]

(2)options说明:

选项说明
-V显示版本
-h显示帮助信息并退出
-v冗长模式,输出指令执行的详细信息
-n加载没有写入文件“/etc/mtab”中的文件系统
-r将文件系统加载为只读模式
-w将文件系统加载为读写模式
-a自动卸载所有支持自动挂载的设备;(定义在了/etc/fstab文件中,且挂载选项中有“自动挂载”功能
-t<文件系统类型>仅卸载选项中指定的文件系统

(3)parameter说明:

选项说明
文件系统指定要卸载的文件系统或对应的设备文件名

(4)实例:

#通过设备名卸载:
umount –v /dev/sda1
#通过挂载点卸载
umount –v /mnt/cdrom

三、开机自动挂载

如果想实现开机自动挂载某设备,只需修改**/etc/fstab文件(文件挂载的配置文件)**即可。

[admin@localhost mnt]$ sudo cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Nov 21 18:16:53 2014
#
# 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
#
UUID=6634633e-001d-43ba-8fab-202f1df93339 / ext4 defaults,barrier=0 1 1

#如上,每行定义一个要挂载的文件系统
#每列内容含义如下:
#1、要挂载的设备或伪文件系统---UUID=6634633e-001d-43ba-8fab-202f1df93339
#     设备文件、LABEL(LABEL="")、UUID(UUID="")、伪文件系统名称(proc, sysfs)
#2、挂载点---/
#     指定的文件夹
#3、文件系统类型---ext4
#4、挂载选项---defaults,barrier=0
#     defaults
#5、转储频率---1
#     0:不做备份
#     1:每天转储
#     2:每隔一天转储
#6、自检次序---1
#     0:不自检
#     1:首先自检;一般只有rootfs才用1;

四、/etc/fstab文件、/etc/mtab文件和/proc/mounts文件

首先附上man counts对这几个文件相关的解释:
The /etc/fstab, /etc/mtab and /proc/mounts files.
The file /etc/fstab (see fstab(5)), may contain lines describing what devices are usually mounted where, using which options. The default location of the fstab(5) file could be overridden by --fstab command line option (see below for more details).The command
mount -a [-t type] [-O optlist]
(usually given in a bootscript) causes all filesystems mentioned in fstab (of the proper type and/or having or not having the proper options) to be mounted as indi‐cated, except for those whose line contains the noauto keyword. Adding the -F option will make mount fork, so that the filesystems are mounted simultaneously. When mounting a filesystem mentioned in fstab or mtab, it suffices to give only the device, or only the mount point.
The programs mount and umount maintain a list of currently mounted filesystems in the file /etc/mtab. If no arguments are given to mount, this list is printed.
The mount program does not read the /etc/fstab file if device (or LABEL, UUID, PARTUUID or PARTLABEL) and dir are specified. For example:
mount /dev/foo /dir
If you want to override mount options from /etc/fstab you have to use:
mount device|dir -o
and then the mount options from command line will be appended to the list of options from /etc/fstab. The usual behaviour is that the last option wins if there is more duplicated options.
When the proc filesystem is mounted (say at /proc), the files /etc/mtab and /proc/mounts have very similar contents. The former has somewhat more information, such as the mount options used, but is not necessarily up-to-date (cf. the -n option below). It is possible to replace /etc/mtab by a symbolic link to /proc/mounts, and espe‐cially when you have very large numbers of mounts things will be much faster with that symlink, but some information is lost that way, and in particular using the “user” option will fail.

简而言之如下:
fstab为设备启动时挂载的文件系统列表,其中包含了什么设备以及什么挂载方式;
mtab是mount和umount维护的一份已挂载文件系统的列表;

越大气越成功,越努力越幸运!

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值