挂载目录
磁盘设备挂载概念
a)Linux 系统文件目录(一部分)
b) U盘文件系统目录
# 查看根目录下的文件夹及文件
root@ubuntu:~# ls /
bin cdrom etc lib lib64 lost+found mnt proc run snap swapfile tmp var
boot dev home lib32 libx32 media opt root sbin srv sys usr
挂载
linux 下设备不挂载不能使用,不挂载的设备相当于没门没窗的监狱(进不去出不来),挂载相当于给设备创造了一个入口(即挂载点,一般为目录)
例如 linux 访问U盘数据
挂载通常是将一个 存储设备 挂接到一个已经存在的 目录上,访问这个 目录 就是访问该存储设备的内容。
对于 linux 系统来说,一切皆文件,所有文件都放在以 根目录 为起点的树形目录结构中,任何硬件设备也都是文件形式。
如图所示,是U盘存储设备和 linux 系统自己的文件系统结构,此时 linux 想要使用U盘的硬件设备,必须将 linux 本身的目录,和硬件设备的文件目录合二为一,此过程就称之为 挂载 。
挂载操作会隐藏原本 linux 目录中的文件,因此选择 linux 本身的目录,最好是新建空目录,用于挂载。
挂载完之后,这个目录被称之为 挂载点
mount 命令
mount 命令可以 将指定的文件系统 挂载到 指定目录(挂载点,linux 系统上的一个文件夹) ,在 linux 系统下必须先挂载后才能访问设备资源。
文件系统挂载原理实战
# 查看根目录下的文件夹及文件
root@ubuntu:~# ls /
bin cdrom etc lib lib64 lost+found mnt proc run snap swapfile tmp var
boot dev home lib32 libx32 media opt root sbin srv sys usr
# 查看磁盘分区情况
root@ubuntu:~# fdisk -l | grep sd
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 1054719 1050624 513M EFI System
/dev/sda3 1054720 209713151 208658432 99.5G Linux filesystem
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
/dev/sdb1 2048 526335 524288 256M 83 Linux
/dev/sdb2 526336 209715199 209188864 99.7G 5 Extended
/dev/sdb5 528384 105385983 104857600 50G 83 Linux
/dev/sdb6 105388032 209715199 104327168 49.7G 83 Linux
Disk /dev/sdc: 120 GiB, 128849018880 bytes, 251658240 sectors
/dev/sdc1 34 976562 976529 476.8M Linux filesystem
/dev/sdc2 978944 97656831 96677888 46.1G Linux filesystem
# 列出机器上包含sd设备的参数
root@ubuntu:~# lsblk -f | grep sd
sda
├─sda1
├─sda2 vfat FAT32 4D72-6786 505.9M 1% /boot/efi
└─sda3 ext4 f4b2aa80-fa0b-417e-ab7b-fa27caf2f921 82.3G 10% /var/snap/firefox/common/host-hunspell
sdb
├─sdb1
├─sdb2
├─sdb5
└─sdb6
sdc
├─sdc1 ext4 1.0 52572ca0-5fbf-4604-b1df-e887a0af43dd
└─sdc2 ntfs 0B145F533BB37153
# 查看文件的分区
root@ubuntu:~# ls /dev/sd*
/dev/sda /dev/sda2 /dev/sdb /dev/sdb2 /dev/sdb6 /dev/sdc1
/dev/sda1 /dev/sda3 /dev/sdb1 /dev/sdb5 /dev/sdc /dev/sdc2
# U盘
将 linux 本身的普通文件夹和U盘的设备文件合二为一,这个过程就是挂载的过程、
挂载完之后,linux 上的普通文件夹,被称之为挂载点。
root@ubuntu:~# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=1943476k,nr_inodes=485869,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=396356k,mode=755,inode64)
/dev/sda3 on / type ext4 (rw,relatime,errors=remount-ro)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k,inode64)
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=28210)
tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,noexec,relatime)
configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime)
ramfs on /run/credentials/systemd-sysusers.service type ramfs (ro,nosuid,nodev,noexec,relatime,mode=700)
/var/lib/snapd/snaps/bare_5.snap on /snap/bare/5 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core_16202.snap on /snap/core/16202 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core_16574.snap on /snap/core/16574 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core20_1169.snap on /snap/core20/1169 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core22_1033.snap on /snap/core22/1033 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core20_2105.snap on /snap/core20/2105 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/firefox_3779.snap on /snap/firefox/3779 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gnome-3-38-2004_143.snap on /snap/gnome-3-38-2004/143 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/firefox_631.snap on /snap/firefox/631 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/snap-store_557.snap on /snap/snap-store/557 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/dev/sda3 on /var/snap/firefox/common/host-hunspell type ext4 (ro,noexec,noatime,errors=remount-ro)
/var/lib/snapd/snaps/gnome-3-38-2004_76.snap on /snap/gnome-3-38-2004/76 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gtk-common-themes_1519.snap on /snap/gtk-common-themes/1519 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/snap-store_959.snap on /snap/snap-store/959 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gtk-common-themes_1535.snap on /snap/gtk-common-themes/1535 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core22_1122.snap on /snap/core22/1122 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gnome-42-2204_141.snap on /snap/gnome-42-2204/141 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/dev/sda2 on /boot/efi type vfat (rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /run/user/126 type tmpfs (rw,nosuid,nodev,relatime,size=396352k,nr_inodes=99088,mode=700,uid=126,gid=132,inode64)
gvfsd-fuse on /run/user/126/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=126,group_id=132)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=396352k,nr_inodes=99088,mode=700,inode64)
# root@ubuntu:~# mount -l
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=1943476k,nr_inodes=485869,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=396356k,mode=755,inode64)
/dev/sda3 on / type ext4 (rw,relatime,errors=remount-ro)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k,inode64)
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=28210)
tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,noexec,relatime)
configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime)
ramfs on /run/credentials/systemd-sysusers.service type ramfs (ro,nosuid,nodev,noexec,relatime,mode=700)
/var/lib/snapd/snaps/bare_5.snap on /snap/bare/5 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core_16202.snap on /snap/core/16202 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core_16574.snap on /snap/core/16574 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core20_1169.snap on /snap/core20/1169 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core22_1033.snap on /snap/core22/1033 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core20_2105.snap on /snap/core20/2105 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/firefox_3779.snap on /snap/firefox/3779 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gnome-3-38-2004_143.snap on /snap/gnome-3-38-2004/143 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/firefox_631.snap on /snap/firefox/631 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/snap-store_557.snap on /snap/snap-store/557 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/dev/sda3 on /var/snap/firefox/common/host-hunspell type ext4 (ro,noexec,noatime,errors=remount-ro)
/var/lib/snapd/snaps/gnome-3-38-2004_76.snap on /snap/gnome-3-38-2004/76 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gtk-common-themes_1519.snap on /snap/gtk-common-themes/1519 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/snap-store_959.snap on /snap/snap-store/959 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gtk-common-themes_1535.snap on /snap/gtk-common-themes/1535 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/core22_1122.snap on /snap/core22/1122 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/var/lib/snapd/snaps/gnome-42-2204_141.snap on /snap/gnome-42-2204/141 type squashfs (ro,nodev,relatime,errors=continue,threads=single,x-gdu.hide)
/dev/sda2 on /boot/efi type vfat (rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /run/user/126 type tmpfs (rw,nosuid,nodev,relatime,size=396352k,nr_inodes=99088,mode=700,uid=126,gid=132,inode64)
gvfsd-fuse on /run/user/126/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=126,group_id=132)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=396352k,nr_inodes=99088,mode=700,inode64)
挂载演练,mount 命令
mount 命令能够将指定的文件系统挂载到指定的目录上(挂载点,linux 系统上的一个文件夹)
1.一个新的硬盘插到机器上,分区之后,针对分区格式化文件系统,ntfs ,ext3 ext4 xfs.....
2.此时还未挂载分区,分区还只是一个封闭的空间,无法读取。
3.针对分区和 linux 的文件夹,合并,关联,挂载,即可通过 访问被挂载的文件夹,即可看到磁盘的数据~~~
mount 命令常用参数
-l | 显示系统上所有挂载的设备信息 |
---|---|
-t | 指定设备的文件系统类型,如果不指定,mount 自动选择挂载的文件系统类型 |
-o | 添加挂载的功能选项,用的很多 |
-r | read 挂载后的设备,是只读的(比如U盘上的资料,放电脑上给别人看看,不允许写入) |
w | 读写参数, -o rw 权限,允许挂载后读写操作 |
mount 的 -o 参数详解
async | 以异步的方式处理文件系统 IO,加速写入,数据不会同步的写入到磁盘,写入到缓冲区的,提高系统性能,可能会损失数据安全性 |
---|---|
sync | 所有的 IO 操作(就是从磁盘读取数据,写入数据)同步处理,数据同步写入到磁盘,性能较弱,提升数据读写的安全性 |
atime/noatime | 文件被访问的时候,是否修改其时间戳,能够提升磁盘 IO 速度 |
auto/noauto | 可以通过 -a 参数自动挂载,或者不自动挂载 |
defaults | 这个默认参数,涵盖了 rw suid dev exec auto nouser async等参数 |
exec/noexec | 是否允许执行挂载点内的可执行命令,使用了 noexec ,提升磁盘安全性 |
ro | 只读 |
rw | 读写 |
对于 centos7 新出现的 mount 选项有如下参数
attr2 | 在磁盘上存储内连扩展属性,提升磁盘性能 |
---|---|
inode64 | 允许在文件系统的任意位置创建 inode |
noquota | 强制关闭文件系统的限额功能 |
mount 命令挂载案例
mount 你要挂载的设备 挂载点
# 查看包含sd设备的分区情况
root@ubuntu:~# fdisk -l | grep sd
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 1054719 1050624 513M EFI System
/dev/sda3 1054720 209713151 208658432 99.5G Linux filesystem
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
/dev/sdb1 2048 526335 524288 256M 83 Linux
/dev/sdb2 526336 209715199 209188864 99.7G 5 Extended
/dev/sdb5 528384 105385983 104857600 50G 83 Linux
/dev/sdb6 105388032 209715199 104327168 49.7G 83 Linux
Disk /dev/sdc: 120 GiB, 128849018880 bytes, 251658240 sectors
/dev/sdc1 34 976562 976529 476.8M Linux filesystem
/dev/sdc2 978944 97656831 96677888 46.1G Linux filesystem
# 查看包含sd设备的磁盘格式化文件系统情况
root@ubuntu:~# lsblk -f | grep sd
sda
├─sda1
├─sda2 vfat FAT32 4D72-6786 505.9M 1% /boot/efi
└─sda3 ext4 f4b2aa80-fa0b-417e-ab7b-fa27caf2f921 82.2G 11% /var/snap/firefox/common/host-hunspell
sdb
├─sdb1
├─sdb2
├─sdb5
└─sdb6
sdc
├─sdc1 ext4 1.0 52572ca0-5fbf-4604-b1df-e887a0af43dd
└─sdc2 ntfs 0B145F533BB37153
# 挂载没有格式化文件系统的设备 /dev/sdb5 可以发现没有格式化文件系统的设备是没发挂载的
root@ubuntu:~# mount /dev/sdb5 /mnt
mount: /mnt: wrong fs type, bad option, bad superblock on /dev/sdb5, missing codepage or helper program, or other error.
root@ubuntu:~# mount -l | grep sdb5
root@ubuntu:~# lsblk -f | grep sd
sda
├─sda1
├─sda2 vfat FAT32 4D72-6786 505.9M 1% /boot/efi
└─sda3 ext4 1.0 f4b2aa80-fa0b-417e-ab7b-fa27caf2f921 82.2G 11% /var/snap/firefox/common/host-hunspell
sdb
├─sdb1
├─sdb2
├─sdb5
└─sdb6
sdc
├─sdc1 ext4 1.0 52572ca0-5fbf-4604-b1df-e887a0af43dd
└─sdc2 ntfs 0B145F533BB37153 # 第一步 挂载 /dev/sdb5 到 /mnt文件夹下,然后可以写入数据
mkfs.ntfs /dev/sdb5
mount /dev/sdb5 /mnt
root@ubuntu:~# mkfs.
mkfs.bfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.ntfs
mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.vfat
# 对设备 /dev/sdb5 进行格式化文件系统
root@ubuntu:~# mkfs.ntfs /dev/sdb5
Cluster size has been automatically set to 4096 bytes.
Initializing device with zeroes: 100% - Done.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.
# 查看包含sd设备的磁盘格式化文件系统情况 可以发现设备 /dev/sdb5 已经成功格式化文件系统了
root@ubuntu:~# lsblk -f | grep sd
sda
├─sda1
├─sda2 vfat FAT32 4D72-6786 505.9M 1% /boot/efi
└─sda3 ext4 1.0 f4b2aa80-fa0b-417e-ab7b-fa27caf2f921 82.2G 11% /var/snap/firefox/common/host-hunspell
sdb
├─sdb1
├─sdb2
├─sdb5 ntfs 414C964962354BE7
└─sdb6
sdc
├─sdc1 ext4 1.0 52572ca0-5fbf-4604-b1df-e887a0af43dd
└─sdc2 ntfs 0B145F533BB37153
# 将设备 /dev/sdb5 挂载到 /mnt 下
root@ubuntu:~# mount /dev/sdb5 /mnt
# 查看挂载情况,发现设备 /dev/sdb5 已经成功挂载到 /mnt 目录下面
root@ubuntu:~# mount -l | grep sdb5
/dev/sdb5 on /mnt type fuseblk (rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096)
第二步 进入 /mnt 文件夹 写入数据
root@ubuntu:~# cd /mnt
root@ubuntu:/mnt# ls
root@ubuntu:/mnt#
# 挂载完之后,查看所有挂载磁盘使用量情况
root@ubuntu:/mnt# df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 388M 1.9M 386M 1% /run
/dev/sda3 ext4 98G 11G 83G 12% /
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 388M 72K 387M 1% /run/user/126
tmpfs tmpfs 388M 56K 388M 1% /run/user/0
/dev/sdb5 fuseblk 50G 66M 50G 1% /mnt
# 查看所有保护sd设备磁盘使用情况
root@ubuntu:/mnt# df -hT | grep sd
/dev/sda3 ext4 98G 11G 83G 12% /
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
/dev/sdb5 fuseblk 50G 66M 50G 1% /mnt
# 对磁盘进行写入并查看写入文件的大小
root@ubuntu:/mnt# pwd
/mnt
root@ubuntu:/mnt# echo {1..5000} > test.txt
root@ubuntu:/mnt# ls
test.txt
root@ubuntu:/mnt# du -h *
24K test.txt
# 追加写入,再次查看文件大小
root@ubuntu:/mnt# echo {1..900000} >> test.txt
root@ubuntu:/mnt# du -h *
6.0M test.txt
# 查看所有挂载磁盘使用量情况
root@ubuntu:/mnt# df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 388M 1.9M 386M 1% /run
/dev/sda3 ext4 98G 11G 83G 12% /
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 388M 72K 387M 1% /run/user/126
tmpfs tmpfs 388M 56K 388M 1% /run/user/0
/dev/sdb5 fuseblk 50G 72M 50G 1% /mnt
root@ubuntu:~# mkfs.minix /dev/sdb6
21856 inodes
65535 blocks
Firstdatazone=696 (696)
Zonesize=1024
Maxsize=268966912
root@ubuntu:~# lsblk -f | grep sd
sda
├─sda1
├─sda2 vfat FAT32 4D72-6786 505.9M 1% /boot/efi
└─sda3 ext4 1.0 f4b2aa80-fa0b-417e-ab7b-fa27caf2f921 82.1G 11% /var/snap/firefox/common/host-hunspell
sdb
├─sdb1
├─sdb2
├─sdb5 ntfs 414C964962354BE7 49.9G 0% /mnt
└─sdb6 minix 1
sdc
├─sdc1 ext4 1.0 52572ca0-5fbf-4604-b1df-e887a0af43dd
└─sdc2 ntfs 0B145F533BB37153
第三步 取消挂载
umount /mnt # 注意此时 /dev/sdb5 设备必须没有人在用,才能取消挂载
root@ubuntu:/mnt# umount /mnt
umount: /mnt: target is busy.
# 退出 /mnt 再次执行 umount /mnt 取消挂载成功
root@ubuntu:/mnt# cd
root@ubuntu:~# umount /mnt
# 查看挂载情况,发现设备 /dev/sdb5 已经成功取消挂载到的 /mnt 目录即没有任何挂载信息
root@ubuntu:~# mount -l | grep sdb5
root@ubuntu:~# cd /mnt
root@ubuntu:/mnt# ls
root@ubuntu:/mnt# pwd
/mnt
# /dev/sdb5 磁盘分区设备,数据都在这里
# /mnt 只是一个普通文件夹,只是相当于一个访问磁盘分区的入口一样
第四步 以只读的形式挂载设备 mount -o ro /dev/sdb5 /mnt
root@ubuntu:/mnt# mount -o ro /dev/sdb5 /mnt
root@ubuntu:/mnt# df -hT | grep sdb5
/dev/sdb5 fuseblk 50G 72M 50G 1% /mnt
root@ubuntu:/mnt# ls
root@ubuntu:/mnt# mount -l | grep sdb5
/dev/sdb5 on /mnt type fuseblk (ro,relatime,user_id=0,group_id=0,allow_other,blksize=4096)
root@ubuntu:~# cd /mnt
root@ubuntu:/mnt# ls
test.txt
root@ubuntu:/mnt# touch 超哥太强了.txt
touch: cannot touch '超哥太强了.txt': Read-only file system
root@ubuntu:/mnt# tail -1 test.txt
root@ubuntu:/mnt# exit
logout
longchi@ubuntu:~$
以上验证只可以读取文件,不能写入
第五步 禁止挂载的文件,执行二进制文件
# 取消挂载
root@ubuntu:~# df -hT # 查看所有磁盘设备挂载情况
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 388M 1.9M 386M 1% /run
/dev/sda3 ext4 98G 11G 83G 12% /
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 388M 72K 387M 1% /run/user/126
tmpfs tmpfs 388M 56K 388M 1% /run/user/0
/dev/sdb5 fuseblk 50G 72M 50G 1% /mnt
root@ubuntu:~# umount /mnt # 取消挂载
root@ubuntu:~# df -hT # 再次查看所有磁盘挂载情况,发现 /dev/sdb5 已经取消挂载
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 388M 1.9M 386M 1% /run
/dev/sda3 ext4 98G 11G 83G 12% /
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 vfat 512M 6.1M 506M 2% /boot/efi
tmpfs tmpfs 388M 72K 387M 1% /run/user/126
tmpfs tmpfs 388M 56K 388M 1% /run/user/0
# 挂载
root@ubuntu:~# mount -o noexec /dev/sdb5 /mnt
root@ubuntu:~# cd /mnt
root@ubuntu:/mnt# ls
test.txt
root@ubuntu:/mnt# vim hehe.sh
root@ubuntu:/mnt# ls
hehe.sh test.txt
root@ubuntu:/mnt# ./hehe.sh
-bash: ./hehe.sh: Permission denied
root@ubuntu:/mnt# chmod 777 hehe.sh # 添加权限
root@ubuntu:/mnt# ./hehe.sh # 还是没有权限执行
-bash: ./hehe.sh: Permission denied
root@ubuntu:/mnt# ls -l
total 6069
-rwxrwxrwx 1 root root 64 Feb 13 18:28 hehe.sh
-rwxrwxrwx 1 root root 6212788 Feb 13 17:31 test.txt
取消挂载(两种方式取消挂载)
root@ubuntu:~# umount /dev/sdb5 # 取消设备挂载
root@ubuntu:~# umount /mnt # 取消挂载点挂载
# 再次挂载(用默认的读写形式挂载)
root@ubuntu:~# mount /dev/sdb5 /mnt
root@ubuntu:~# cd /mnt
root@ubuntu:/mnt# ls
hehe.sh test.txt
root@ubuntu:/mnt# ./hehe.sh
超哥说,今天天气不错,适合学习,给力
注意:挂载的时候,添加了 noexec 参数,是无法执行脚本文件的,只能在挂载的时候,是允许 exec 才可以执行脚本文件
root@ubuntu:~# ls
snap
root@ubuntu:~# stat snap
File: snap
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 5636983 Links: 4
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-02-07 16:55:09.251999715 -0800
Modify: 2024-02-07 16:55:13.499999591 -0800
Change: 2024-02-07 16:55:13.499999591 -0800
Birth: 2024-02-07 16:55:09.251999715 -0800