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

目录
相关文章
|
11天前
|
Linux
CentOS系统中rpm包与源码包的主要区别
总的来说,RPM包和源码包各有优缺点,选择哪种包主要取决于你的需求和技术水平。希望这个答案能帮助你更好地理解RPM包和源码包的区别。
66 27
|
11天前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
172 26
|
11天前
|
Linux 开发工具 C语言
在CentOS系统中编译安装TinyXml2的方法
以上就是在CentOS系统中编译安装TinyXml2的方法。这个过程可能会有些复杂,但只要你按照上面的步骤一步一步来,你应该能够成功地完成这个任务。如果你在这个过程中遇到任何问题,你可以在网上搜索相关的资料,或者在相关的论坛上寻求帮助。希望这个指南能够帮助你成功地在你的CentOS系统中编译安装TinyXml2。
54 22
|
2月前
|
Linux 应用服务中间件 nginx
CentOS系统下的软件安装与卸载
CentOS系统中安装及卸载软件,常用yum命令安装及卸载软件包。yum是基于RPM的软件包管理器,可用于在CentOS中安装、更新、查询和移除软件包。yum命令可以从指定服务器下载rpm包并安装,能自动解决依赖问题。
171 78
|
28天前
|
存储 安全 Linux
CentOS 7.9系统备份:每日定期发送最新备份文件到另一台服务器。
注意,这个解决方案忽略了很多细节,例如错误处理和通知、备份版本控制、循环处理旧的备份文件等等。此外,你也应该尽量保持源服务器和目标服务器之间快速,稳定且安全的网络连接,并且目标服务器应该有足够的空间用于存放每天的备份文件。如果你需要更高级的备份解决方案,可能需要考虑一下使用专门的备份工具或者服务。
67 18
|
1月前
|
Linux 虚拟化 数据安全/隐私保护
系统崩溃不用慌!VMware这个逆天功能竟能让CentOS一键回档?后悔药真实存在!
备份的重要性在于它能在系统出现异常或错误时,帮助我们快速恢复到正常状态,避免重新安装系统和配置环境。VMware 提供了两种备份方式:快照和克隆。 **快照**是保存虚拟机某一时刻的完整状态(包括内存、CPU、磁盘数据),便于快速回滚,适合临时保存状态。操作简单,可在系统运行时创建。 **克隆**则是复制整个虚拟机系统,侧重长期备份,需在系统关闭时进行。分为完整克隆和链接克隆,前者独立于源系统,占用更多空间,但更安全可靠。
76 17
系统崩溃不用慌!VMware这个逆天功能竟能让CentOS一键回档?后悔药真实存在!
|
1月前
|
存储 安全 Linux
Centos 8系统ext4文件系统类型进行扩容缩容 (LVM)
通过使用这些技术和工具,可以有效管理CentOS 8系统上的存储资源,确保系统在高负载和高并发情况下的稳定运行。
107 9
|
4月前
|
运维 监控 安全
CentOS 7系统 OpenSSH和OpenSSL版本升级指南
本文详细介绍如何在CentOS 7系统上升级OpenSSH和OpenSSL至最新稳定版本(OpenSSH 9.6p1和OpenSSL 1.1.1w),解决多个已知安全漏洞(如CVE-2023-51767等)。涵盖环境说明、现存漏洞、升级准备、具体步骤及故障排查,确保服务器安全。建议先在测试环境验证,再应用于生产环境。
678 7
|
5月前
|
Linux 开发工具 Windows
CentOS8 64位系统 搭建内网穿透frp
【10月更文挑战第23天】本文介绍了如何在Linux系统上搭建frp内网穿透服务,并配置Windows客户端进行访问。首先,通过系统信息检查和软件下载,完成frp服务端的安装与配置。接着,在Windows客户端下载并配置frpc,实现通过域名访问内网地址。最后,通过创建systemd服务,实现frp服务的开机自动启动。
343 20
|
6月前
|
Web App开发 搜索推荐 Unix
Linux系统之MobaXterm远程连接centos的GNOME桌面环境
【10月更文挑战第21天】Linux系统之MobaXterm远程连接centos的GNOME桌面环境
1212 5
Linux系统之MobaXterm远程连接centos的GNOME桌面环境
下一篇
oss创建bucket