Linux系统 挂载与卸载分区

印迹发布于:2020-6-15 902

前言

1、挂载分区(或文件系统):使用mount命令挂载;

2、卸载分区:要移除USB磁盘、U盘、光盘和硬盘时,需要先卸载,使用umount命令。

一、Linux与Windows

1、Windows系统下的分区很明了:

    一个分区就是一个盘符;

    如果有可以兼容的文件系统,系统可以读写那个分区;

    如果是不兼容的文件系统,Windows会自动忽略这个分区。

2、Linux则像是一颗“大树”:

    如果有另外的分区或者硬盘,他们就会挂载到“枝干”的子目录中;

    这个子目录即所谓的挂载点;

    可以将分区挂载到任意目录中。

二、挂载分区(或文件系统)

1、挂载命令

# mount  [-t  文件系统类型]  设备名   挂载点目录

2、命令帮助信息

[root@localhost ~]# mount --help
Usage:
 mount [-lhV]
 mount -a [options]
 mount [options] [--source] <source> | [--target] <directory>
 mount [options] <source> <directory>
 mount <operation> <mountpoint> [<target>]

Options:

 -a, --all               mount all filesystems mentioned in fstab
 -c, --no-canonicalize   don't canonicalize paths
 -f, --fake              dry run; skip the mount(2) syscall
 -F, --fork              fork off for each device (use with -a)
 -T, --fstab <path>      alternative file to /etc/fstab
 -h, --help              display this help text and exit
 -i, --internal-only     don't call the mount.<type> helpers
 -l, --show-labels       lists all mounts with LABELs
 -n, --no-mtab           don't write to /etc/mtab
 -o, --options <list>    comma-separated list of mount options
 -O, --test-opts <list>  limit the set of filesystems (use with -a)
 -r, --read-only         mount the filesystem read-only (same as -o ro)
 -t, --types <list>      limit the set of filesystem types
     --source <src>      explicitly specifies source (path, label, uuid)
     --target <target>   explicitly specifies mountpoint
 -v, --verbose           say what is being done
 -V, --version           display version information and exit
 -w, --rw, --read-write  mount the filesystem read-write (default)
 -h, --help     display this help and exit
 -V, --version  output version information and exit

Source:

-L, --label <label>     synonym for LABEL=<label>
 -U, --uuid <uuid>       synonym for UUID=<uuid>
 LABEL=<label>           specifies device by filesystem label
 UUID=<uuid>             specifies device by filesystem UUID
 PARTLABEL=<label>       specifies device by partition label
 PARTUUID=<uuid>         specifies device by partition UUID
 <device>                specifies device by path
 <directory>             mountpoint for bind mounts (see --bind/rbind)
 <file>                  regular file for loopdev setup

Operations:

 -B, --bind              mount a subtree somewhere else (same as -o bind)
 -M, --move              move a subtree to some other place
 -R, --rbind             mount a subtree and all submounts somewhere else
 --make-shared           mark a subtree as shared
 --make-slave            mark a subtree as slave
 --make-private          mark a subtree as private
 --make-unbindable       mark a subtree as unbindable
 --make-rshared          recursively mark a whole subtree as shared
 --make-rslave           recursively mark a whole subtree as slave
 --make-rprivate         recursively mark a whole subtree as private
 --make-runbindable      recursively mark a whole subtree as unbindable
For more details see mount(8).

3、示例

    要求:插入光盘,将其挂载到“/mnt/cdrom”目录下,查看挂载的所有设备。

[root@localhost ~]# mkdir /mnt/cdrom    # 创建目录
[root@localhost ~]# df -h    # 显示所有文件系统的磁盘使用情况,并没有光盘的相关信息
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   22G  4.7G   18G  22% /
devtmpfs                 894M     0  894M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.9M  900M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/sda1               1014M  207M  808M  21% /boot
tmpfs                    182M     0  182M   0% /run/user/0
[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/    # 将光盘以只读的方式挂载
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# df -h    # 显示所有文件系统的磁盘使用情况,在最后一行找到/dev/sr0光盘的信息
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   22G  4.7G   18G  22% /
devtmpfs                 894M     0  894M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.9M  900M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/sda1               1014M  207M  808M  21% /boot
tmpfs                    182M     0  182M   0% /run/user/0
/dev/sr0                 4.3G  4.3G     0 100% /mnt/cdrom
三、卸载分区

1、要移除USB磁盘、U盘、光盘和硬盘时,需要先卸载。

2、卸载命令:

umount  挂载点目录或存储设备名

3、命令帮助信息

[root@localhost ~]# umount --help
Usage:
 umount [-hV]
 umount -a [options]
 umount [options] <source> | <directory>

Options:

 -a, --all               unmount all filesystems
 -A, --all-targets       unmount all mountpoins for the given device
                         in the current namespace
 -c, --no-canonicalize   don't canonicalize paths
 -d, --detach-loop       if mounted loop device, also free this loop device
     --fake              dry run; skip the umount(2) syscall
 -f, --force             force unmount (in case of an unreachable NFS system)
 -i, --internal-only     don't call the umount.<type> helpers
 -n, --no-mtab           don't write to /etc/mtab
 -l, --lazy              detach the filesystem now, and cleanup all later
 -O, --test-opts <list>  limit the set of filesystems (use with -a)
 -R, --recursive         recursively unmount a target with all its children
 -r, --read-only         In case unmounting fails, try to remount read-only
 -t, --types <list>      limit the set of filesystem types
 -v, --verbose           say what is being done
 -h, --help     display this help and exit
 -V, --version  output version information and exit
For more details see umount(8).

4、示例

    要求:将上一示例中的光盘卸载。

# 1、查看所有文件系统的磁盘使用情况

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   22G  4.7G   18G  22% /
devtmpfs                 894M     0  894M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.9M  900M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/sda1               1014M  207M  808M  21% /boot
tmpfs                    182M     0  182M   0% /run/user/0
/dev/sr0                 4.3G  4.3G     0 100% /mnt/cdrom

# 2、使用umount命令卸载光盘

[root@localhost ~]# umount /dev/sr0

# 3、检查磁盘使用情况,/dev/sr0光盘已经卸载

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   22G  4.7G   18G  22% /
devtmpfs                 894M     0  894M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.9M  900M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/sda1               1014M  207M  808M  21% /boot
tmpfs                    182M     0  182M   0% /run/user/0
四、小结

    挂载命令:mount [-t 文件系统类型] 设备名 挂载点目录

    查看系统文件的磁盘使用情况:df -h

    卸载命令:umount 挂载点目录或存储设备名

五、其它

设置系统启动时的自动挂载:

手动挂载的分区会在系统重启后失效,若用户需要永久挂载分区,则需要通过编辑/etc/fstab文件来实现。当系统启动的时候,系统会自动地从这个文件读取信息,并且会自动将此文件中指定的文件系统挂载到指定的目录。如:

/dev/vg0/lv_data  /data  ext4   defaults     0 0

http://www.virplus.com/thread-1336.htm
转载请注明:2020-6-15 于 VirPlus 发表

推荐阅读
最新回复 (0)

    ( 登录 ) 后,可以发表评论!

    返回