zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-02-26 12:26:49 时间

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

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

1. 检测:
#查看磁盘信息,根据磁盘文件系统类型和大小查找 usb 驱动器 [[email protected] ~]$ 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. 创建挂载点: 挂载点可以是主机文件系统内已经存在的或新创建的路径
[[email protected] ~]$ sudo mkdir /media/usbC [[email protected] ~]$ ll  /media/usbC total 0 
3. 挂载:
[[email protected] ~]$ sudo mount /dev/sdc1 /media/usbC/ #查看挂载后 usb disk 内的目录文件信息 [[email protected] ~]$ 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' #查看挂载信息 [[email protected] ~]$ 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 写入数据:
[[email protected] usbC]$ echo Hello,Aliyun! >test.txt [[email protected] 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 [[email protected] usbC]$ cat test.txt  Hello,Aliyun!
4.2 拷贝数据至主机:
[[email protected] usbC]$ cp test.txt  /home/arthur/Downloads/  [[email protected] 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 查看磁盘使用情况
[[email protected] 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`取消挂载 [[email protected] ~]$ sudo umount /media/usbC  [sudo] password for arthur:  #查看当前系统挂载文件系统 ##手动创建的挂载点已无设备,此处出现的挂载信息为系统检测到的 usb disk,当手动弹出 usb disk 之后,该条信息消失 [[email protected] ~]$ 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)  [[email protected] ~]$ ll /media/usbC/ total 0 [[email protected] ~]$ sudo umount /media/usbC umount: /media/usbC: not mounted. #未手动弹出 usb disk  [[email protected] ~]$ 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 之后 [[email protected] ~]$ ll /run/media/arthur/ total 0 [[email protected] ~]$ mount | grep sdc1 [[email protected] ~]$  
6. 在 Linux 中永久性添加挂载点

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

(福利推荐:阿里云、腾讯云、华为云服务器最新限时优惠活动,云服务器1核2G仅88元/年、2核4G仅698元/3年,点击这里立即抢购>>>

[[email protected] ~]$ 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

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


本站部分内容转载自网络,版权属于原作者所有,如有异议请联系QQ153890879修改或删除,谢谢!
转载请注明原文链接:Linux系统CentOS8 检测USB、创建挂载点、挂载、存取USB数据、取消挂载

你还在原价购买阿里云、腾讯云、华为云、天翼云产品?那就亏大啦!现在申请成为四大品牌云厂商VIP用户,可以3折优惠价购买云服务器等云产品,并且可享四大云服务商产品终身VIP优惠价,还等什么?赶紧点击下面对应链接免费申请VIP客户吧:

1、点击这里立即申请成为腾讯云VIP客户

2、点击这里立即注册成为天翼云VIP客户

3、点击这里立即申请成为华为云VIP客户

4、点击这里立享阿里云产品终身VIP优惠价

喜欢 (0)
[[email protected]]
分享 (0)