Linux系统CentOS8 检测USB、创建挂载点、挂载、存取USB数据、取消挂载

简介: usb 设备挂载与取消挂载; 永久挂载点实现

Linux系统CentOS8 检测USB、创建挂载点、挂载、存取USB数据、取消挂载

当主机接入 usb 设备时, linux 系统会在 /dev 路径下增加一个新的 block设备。在和 usb 设备进行数据传输之前,需要将 usb 文件系统 进行挂载

1. 检测:
#查看磁盘信息,根据磁盘文件系统类型和大小查找 usb 驱动器
[arthur@localhost ~]$ sudo fdisk -l
Disk /dev/sdb: 119.2 GiB, 128035676160 bytes, 250069680 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x522664d6

Device     Boot   Start       End   Sectors   Size Id Type
/dev/sdb1  *       2048   2099199   2097152     1G 83 Linux
/dev/sdb2       2099200 250068991 247969792 118.2G 8e Linux LVM
...
Disk /dev/sdc: 29.3 GiB, 31457280000 bytes, 61440000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x50886156

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdc1  *       63 61439999 61439937 29.3G  c W95 FAT32 (LBA)
2. 创建挂载点: 挂载点可以是主机文件系统内已经存在的或新创建的路径
[arthur@localhost ~]$ sudo mkdir /media/usbC
[arthur@localhost ~]$ ll  /media/usbC
total 0
3. 挂载:
[arthur@localhost ~]$ sudo mount /dev/sdc1 /media/usbC/
#查看挂载后 usb disk 内的目录文件信息
[arthur@localhost ~]$ ll /media/usbC/
total 926512
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00  hg19.fa.gz
drwxr-xr-x. 2 arthur arthur     16384 Aug  3 07:15 'System Volume Information'
#查看挂载信息
[arthur@localhost ~]$ mount |  grep sdc1
/dev/sdc1 on /run/media/arthur/disk type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
/dev/sdc1 on /media/usbC type vfat (rw,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro)
4. 存取数据
4.1 写入数据:
[arthur@localhost usbC]$ echo Hello,Aliyun! >test.txt
[arthur@localhost usbC]$ ll
total 926528
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00  hg19.fa.gz
drwxr-xr-x. 2 arthur arthur     16384 Aug  3 07:15 'System Volume Information'
-rw-r--r--. 1 arthur arthur        14 Sep  2 04:40  test.txt
[arthur@localhost usbC]$ cat test.txt 
Hello,Aliyun!
4.2 拷贝数据至主机:
[arthur@localhost usbC]$ cp test.txt  /home/arthur/Downloads/ 
[arthur@localhost usbC]$ ll /home/arthur/Downloads/ 
total 926500
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00 hg19.fa.gz
-rw-r--r--. 1 arthur arthur        14 Sep  2 04:40 test.txt
4.3 查看磁盘使用情况
[arthur@localhost usbC]$ df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             4.6G     0  4.6G   0% /dev
...
/dev/sdc1             30G  905M   29G   4% /media/usbC
5. 取消挂载
#确保没有进程正在使用或者占用挂载点路径,否则会出现设备占用的警告
umount:  target is busy
#命令`umount`取消挂载
[arthur@localhost ~]$ sudo umount /media/usbC 
[sudo] password for arthur: 
#查看当前系统挂载文件系统
##手动创建的挂载点已无设备,此处出现的挂载信息为系统检测到的 usb disk,当手动弹出 usb disk 之后,该条信息消失
[arthur@localhost ~]$ mount | grep sdc1
/dev/sdc1 on /run/media/arthur/disk type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2) 
[arthur@localhost ~]$ ll /media/usbC/
total 0
[arthur@localhost ~]$ sudo umount /media/usbC
umount: /media/usbC: not mounted.
#未手动弹出 usb disk 
[arthur@localhost ~]$ ll  /run/media/arthur/disk/
total 926528
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00  hg19.fa.gz
drwxr-xr-x. 2 arthur arthur     16384 Aug  3 07:15 'System Volume Information'
-rw-r--r--. 1 arthur arthur        14 Sep  2 04:40  test.txt
#弹出 usb disk 之后
[arthur@localhost ~]$ ll /run/media/arthur/
total 0
[arthur@localhost ~]$ mount | grep sdc1
[arthur@localhost ~]$ 
6. 在 Linux 中永久性添加挂载点

编辑 fstab 文件, 可以使系统重启后仍然保留此文件挂载点

[arthur@localhost ~]$ tail /etc/fstab 
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=bf580455-80bf-4ca2-98fc-1a663e9a73e0 /boot                   ext4    defaults        1 2
/dev/mapper/cl-home     /home                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0

添加如下:

/dev/sdc1    /media/usbC    vfat    default    0    0
拓展:

etc/fstab获取的 block device 名字来指代 usb 设备可能并不是一个好的长久的方案。随着 linux 系统中 USB drives 数量的变化, block device 名称可能会发生改变。尽管使用 UUID raw block device name 同样是临时性方案,但 使用 UUID 会相对更佳。

reference:
[1]How to mount usb drive in linux.linuxconfig

目录
相关文章
|
30天前
|
安全 关系型数据库 MySQL
Linux(CentOS6)安装MySQL5.6
Linux(CentOS 6)系统上安装MySQL 5.6版本的详细步骤,包括准备数据存放目录、创建用户、下载安装包、初始化数据库、配置服务脚本、设置环境变量等操作。
99 1
|
25天前
|
Linux PHP
Linux CentOS 宝塔 Suhosin禁用php5.6版本eval函数详细图文教程
【8月更文挑战第27天】本文介绍两种禁用PHP执行的方法:使用`PHP_diseval_extension`禁用和通过`suhosin`禁用。由于`suhosin`不支持PHP8,仅适用于PHP7及以下版本,若服务器安装了PHP5.6,则需对应安装`suhosin-0.9.38`版本。文章提供了详细的安装步骤,并强调了宝塔环境下与普通环境下的PHP路径差异。安装完成后,在`php.ini`中添加`suhosin.so`扩展并设置`executor.disable_eval = on`以禁用执行功能。最后通过测试代码验证是否成功禁用,并重启`php-fpm`服务生效。
18 2
|
24天前
|
Ubuntu Linux
内核实验(四):Qemu调试Linux内核,实现NFS挂载
本文介绍了在Qemu虚拟机中配置NFS挂载的过程,包括服务端的NFS服务器安装、配置和启动,客户端的DHCP脚本添加和开机脚本修改,以及在Qemu中挂载NFS、测试连通性和解决挂载失败的方法。
30 0
内核实验(四):Qemu调试Linux内核,实现NFS挂载
|
30天前
|
关系型数据库 MySQL Linux
Linux(CentOS7)搭建LAMP服务环境
本文介绍了在Linux (CentOS 7) 上搭建LAMP服务环境的详细步骤,包括安装Apache HTTPd、解决编译时依赖问题、配置Apache服务、安装PHP以及处理PHP与Apache集成时遇到的问题。同时,还涉及了防火墙设置和SELinux权限调整,确保Web服务能够正常运行。
47 2
|
19天前
|
Linux
centos linux内核下载
centos linux内核下载
|
25天前
|
算法 Linux 索引
Linux0.11 根文件系统挂载(四)
Linux0.11 根文件系统挂载(四)
14 0
|
27天前
|
存储 Linux Shell
【应用服务 App Service】App Service For Linux 中如何挂载一个共享文件夹呢? Mount Azure Storage Account File Share
【应用服务 App Service】App Service For Linux 中如何挂载一个共享文件夹呢? Mount Azure Storage Account File Share
|
27天前
|
存储 Linux 网络安全
【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
|
29天前
|
Linux
在Linux中,如何挂载一个ISO文件?
在Linux中,如何挂载一个ISO文件?
|
12月前
|
分布式计算 资源调度 Hadoop