mount 使用方法详解

int32位 posted @ Sep 21, 2014 10:27:02 AM in linux , 8562 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

mount是linux很常用的命令,用于挂载各种设备(包括本地block设备,NFS,虚拟设备等),umount用于卸载设备。如果挂载一个设备到一个目录中,则通过该目录可以访问设备的文件(必须有权限访问),而原来目录的内容会暂时性隐藏(不会覆盖,卸载后恢复可见)。

mount最常用的使用方式为:

mount device dir

比如 mount /dev/sda1 /mnt

这是mount会自动检测设备的文件系统(如果可能的话,比如fstab,mtab中配置有),如果检测失败,需要指定文件系统类型,使用-t选项指定,比如

mount -t btrfs /dev/sda1  /mnt

/etc/fstab是系统默认挂载的配置,在系统启动时会自动挂载该文件下的设备(设置选项noauto除外。

如果你修改了该文件,你也可以使用 mount -a使系统重新读取该文件进行挂载。

在/etc/mtab会记录当前的挂载状况(在/proc/mounts也记录),当使用mount命令不加任何参数时,默认打印该文件的内容。

你也可以使用-l选项打印当前的挂载状况,使用-t选项过滤打印的文件系统类型。

以前一个设备只能挂载在一个地方,从内核2.4以后,支持把一个目录挂载到另一个目录,这时相当于相同文件内容可以同时有多个访问点(这在chroot中很有用,想想我在新的root中怎么访问/proc /dev目录),使用--bind 或者-B选项挂载目录

比如

mount --bind -t tmpfs /dev/  newDev

mount --bind -t proc /proc newProc

注意使用bind选项只能挂载指定目录下的内容,如果该目录下又有子挂载点,不会自动挂载,使用rbind选项,可以递归挂载,甚至可以挂载/ 到另一个目录中。

比如

mount --rbind -t ext4 / newRoot

不过挂载了就不能卸载了,因为该目录正使用(busy),可以使用lsof命令查看(可以使用--move选项,见以下)。

有时我们不想有人bind,可以使用mount --make-unbindable或者--make-runbindable(递归式)选项设置。比如

mount --make-runbindable /

不能再bind根目录了。

--move选项可以把一个挂载点移动到另一个目录,如

mount --move -t ext4 /mnt /media

-r(或者-o ro)选项指定只读挂载,访问挂载点只能读取内容,不能写,-w(或者-o rw)指定挂载为读写方式,这也是默认方式。

-o 指定挂载选项(多个选项使用逗号分隔),以上已经介绍了ro和rw选项,下面简单介绍下挂载选项:

auto ,再/etc/fstab下指定,使用mount -a时或者系统启动时自动挂载,使用noauto相反。

defaults使用默认挂载选项,相当于rw,suid,dev,exec,auto,nouser,async

dev 说明这是字符设备或者块设备,而不是虚拟设备,相反nodev说明可能是虚拟设备

exec,在挂载点可以执行文件,noexec说明在挂载点上不能执行文件,有一次我在家目录下不小心指定了noexec,写了个c程序,运行./a.out出错,后面发现原来是指定了noexec选项

group 一般文件系统挂载只能root身份才有权限,在fstab下指定group,则属于设备组的用户可以挂载。

owner指定设备的所有者可以挂载,比如某设备属于Mary,则root和Mary都有权限挂载。

remount,重新挂载已经挂载的设备,用于覆盖原来的选项,比如原来的文件系统/dev/sda1是只读的,挂载在/mnt下,则可以使用

mount -o remount,rw /dev/sda1 /mnt 重新挂载,并且可读写。

user 用于fstab文件,任何普通用户都可以挂载该设备。

nouser用于fstab,表明只有root可以挂载,这是默认行为。

还有一些选项,专门针对指定的文件系统,比如ext2的errors。

mount还可以用于挂载loop设备,比如iso镜像,img文件等。此时需要指定文件系统类型(-t)和loop选项,比如

mount -t ext4 -o loop ubuntu.iso /mnt

mount -t iso9660 -o loop ubuntu.iso /mnt

卸载loop设备使用命令losetup -d 或者umount -d

试一下

dd if=/dev/zero of=tmp.img bs=500m count=2

mkfs.ext4 tmp.img

mount -t ext4 tmp.img /mnt

使用mount可以挂载iso文件,但磁盘格式为qcow2则不能直接挂载了,可以查看以前的文章挂载http://krystism.is-programmer.com/posts/47074.html。

 

 

 

 

 

转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
ADVICER 说:
2018年7月24日 20:09

[url=https://pauio.com/first/]퍼스트카지노[/url]
<a href="https://pauio.com/yesci/">예스카지노</a>
<a href="https://pauio.com/yeska/">YES카지노</a>
<a href="https://pauio.com/obamas/">오바마카지노</a>

mega888 apk 说:
2021年8月30日 00:13

This content is simply exciting and creative. I have been deciding on a institutional move and this has helped me with one aspect.

Satta king 说:
2021年9月25日 05:22

I use basically superior fabrics : you will discover these products by:

What Is CIF Number 说:
2023年1月25日 17:43

CIF abbreviated as Customer Information File is an official file which saved at Bank database when account created. These CIF files do contain important information about the SBI account holder along with the demographic information. What Is CIF Number State Bank of India does provide a unique CIF number to each CIF file when it created. So that it becomes easier to locate the file with respect to the customer account number. State bank of India is a leading bank in India with its branches around the world.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter