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

目录
相关文章
|
3月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
318 3
Linux系统禁用swap
|
3月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
529 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
426 0
Linux系统初始化脚本
|
3月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
361 1
|
3月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1075 1
|
Linux
百度搜索:蓝易云 ,Centos7系统wget 的安装与使用详细教程。
以上是在CentOS 7系统上安装和使用wget的详细教程。确保按照步骤操作,并根据您的需求进行必要的修改和调整。
455 0
|
4月前
|
Ubuntu Linux 索引
Centos 7、Debian及Ubuntu系统中安装和验证tree命令的指南。
通过上述步骤,我们可以在CentOS 7、Debian和Ubuntu系统中安装并验证 `tree`命令。在命令行界面中执行安装命令,然后通过版本检查确认安装成功。这保证了在多个平台上 `tree`命令的一致性和可用性,使得用户无论在哪种Linux发行版上都能使用此工具浏览目录结构。
436 78
|
6月前
|
机器人 Linux
CentOS 7系统中安装特定版本CMake 3.21.2的方法。
到这里,过程已经全部完成。如果你跟随上面的步骤来,那么你现在已经拥有了一个全新的CMake版本在你的CentOS 7系统上了。这个过程就像是你通过一系列仪式,唤醒了一个沉睡已久的古老机器人,它现在完全按照你的意愿来帮你构建和编译软件了。
551 18
|
8月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
2157 26
|
关系型数据库 MySQL Linux
百度搜索:蓝易云【Centos7系统如何安装MySQL5.7数据库?】
安装 MySQL 5.7 数据库需要进行以下步骤:
245 0