首页 > 代码库 > Linux系统下查看USB设备名及使用USB设备

Linux系统下查看USB设备名及使用USB设备

 

1、系统插入USB设备后,从控制台界面有如下提示:

从控制台信息可以看出插入的USB设备名。

技术分享

从上图可以看出,插入的USB设备为sde4。

但是,如果是CRT工具远程连接过去,可以使用下面的命令来查看USB设备。

2、使用lsblk命令查找USB设备名

lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息。块设备有硬盘,闪存盘,cd-ROM等等。

如下所示:

[root@rhel7 ~]# lsblk  --插入USB设备前的分区情况
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   20G  0 disk 
├─sda1              8:1    0  500M  0 part /boot
└─sda2              8:2    0 19.5G  0 part 
  ├─rhel-root     253:0    0 17.5G  0 lvm  /
  └─rhel-swap     253:1    0    2G  0 lvm  [SWAP]
sdb                 8:16   0    6G  0 disk 
├─sdb1              8:17   0  100M  0 part 
├─sdb2              8:18   0  110M  0 part 
└─sdb3              8:19   0  120M  0 part 
sdc                 8:32   0    5G  0 disk 
├─sdc1              8:33   0  100M  0 part 
│ └─vgdate-lvvol1 253:2    0  292M  0 lvm  /lvmFiles
├─sdc2              8:34   0  200M  0 part 
│ └─vgdate-lvvol1 253:2    0  292M  0 lvm  /lvmFiles
├─sdc3              8:35   0  200M  0 part 
└─sdc4              8:36   0  250M  0 part 
sdd                 8:48   0    2G  0 disk 
sr0                11:0    1 1024M  0 rom  
[root@rhel7 ~]# lsblk  --插入USB设备后的分区情况
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                 8:0    0   20G  0 disk 
├─sda1              8:1    0  500M  0 part /boot
└─sda2              8:2    0 19.5G  0 part 
  ├─rhel-root     253:0    0 17.5G  0 lvm  /
  └─rhel-swap     253:1    0    2G  0 lvm  [SWAP]
sdb                 8:16   0    6G  0 disk 
├─sdb1              8:17   0  100M  0 part 
├─sdb2              8:18   0  110M  0 part 
└─sdb3              8:19   0  120M  0 part 
sdc                 8:32   0    5G  0 disk 
├─sdc1              8:33   0  100M  0 part 
│ └─vgdate-lvvol1 253:2    0  292M  0 lvm  /lvmFiles
├─sdc2              8:34   0  200M  0 part 
│ └─vgdate-lvvol1 253:2    0  292M  0 lvm  /lvmFiles
├─sdc3              8:35   0  200M  0 part 
└─sdc4              8:36   0  250M  0 part 
sdd                 8:48   0    2G  0 disk 
sde                 8:64   1  7.5G  0 disk 
└─sde4              8:68   1  7.5G  0 part 
sr0                11:0    1 1024M  0 rom  
[root@rhel7 ~]# 
从上面的输出信息可以看出,设备为sde,为第一个分区sde4,大小为7.5GB
sde 8:64 1 7.5G 0 disk └─sde4 8:68 1 7.5G 0 part

3、使用fdisk工具识别USB设备名

fdisk是一个功能强大的工具,用于查看你系统中的所有分区表,包括所有的USB设备,使用root权限执行如下命令:

[root@rhel7 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0x0006c7ae

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 6442 MB, 6442450944 bytes, 12582912 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
Disk label type: dos
Disk identifier: 0xaa06e007

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   8e  Linux LVM
/dev/sdb2          206848      432127      112640   8e  Linux LVM
/dev/sdb3          432128      677887      122880   8e  Linux LVM

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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
Disk label type: dos
Disk identifier: 0x0c16d904

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048      206847      102400   8e  Linux LVM
/dev/sdc2          206848      616447      204800   8e  Linux LVM
/dev/sdc3          616448     1026047      204800   8e  Linux LVM
/dev/sdc4         1026048     1538047      256000   8e  Linux LVM

Disk /dev/sdd: 2147 MB, 2147483648 bytes, 4194304 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
Disk label type: dos
Disk identifier: 0x3990990c

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/mapper/rhel-root: 18.8 GB, 18756927488 bytes, 36634624 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


Disk /dev/mapper/rhel-swap: 2147 MB, 2147483648 bytes, 4194304 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


Disk /dev/mapper/vgdate-lvvol1: 306 MB, 306184192 bytes, 598016 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


Disk /dev/sde: 8022 MB, 8022654976 bytes, 15669248 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
Disk label type: dos
Disk identifier: 0xcad4ebea

   Device Boot      Start         End      Blocks   Id  System
/dev/sde4   *          63    15669184     7834561    7  HPFS/NTFS/exFAT
[root@rhel7 ~]# 

从上图可以看出USB设备的信息如下:

Disk /dev/sde: 8022 MB, 8022654976 bytes, 15669248 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
Disk label type: dos
Disk identifier: 0xcad4ebea

   Device Boot      Start         End      Blocks   Id  System
/dev/sde4   *          63    15669184     7834561    7  HPFS/NTFS/exFAT

使用dmesg命令来识别出USB设备名

dmesg是一个重要的用于打印或者控制内核环形缓冲区的命令。环形缓冲区是一种数据结构,它存放着内核操作数据的信息。

运行如下的命令来查看内核操作信息,它同时也会打印出USB设备的信息:

……
[ 1055.720971]  sde: sde4
[ 1055.816921] sd 6:0:0:0: [sde] Attached SCSI removable disk
[ 1168.185175] usb 1-1: USB disconnect, device number 6
[ 1250.915177] usb 1-1: new full-speed USB device number 7 using ohci-pci
[ 1251.361378] usb 1-1: New USB device found, idVendor=8644, idProduct=800b
[ 1251.364410] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1251.367812] usb 1-1: Product: USB Flash Disk                
[ 1251.370072] usb 1-1: Manufacturer: General                       
[ 1251.372505] usb 1-1: SerialNumber: 000000000000C4FC
[ 1251.378864] usb-storage 1-1:1.0: USB Mass Storage device detected
[ 1251.383180] scsi host7: usb-storage 1-1:1.0
[ 1252.415350] scsi 7:0:0:0: Direct-Access     General  USB Flash Disk   1.00 PQ: 0 ANSI: 2
[ 1252.424925] sd 7:0:0:0: Attached scsi generic sg5 type 0
[ 1252.451723] sd 7:0:0:0: [sde] 15669248 512-byte logical blocks: (8.02 GB/7.47 GiB)
[ 1252.481780] sd 7:0:0:0: [sde] Write Protect is off
[ 1252.484715] sd 7:0:0:0: [sde] Mode Sense: 03 00 00 00
[ 1252.507694] sd 7:0:0:0: [sde] No Caching mode page found
[ 1252.510073] sd 7:0:0:0: [sde] Assuming drive cache: write through
[ 1252.624206]  sde: sde4
[ 1252.722176] sd 7:0:0:0: [sde] Attached SCSI removable disk
[root@rhel7 ~]# 

其实上面的信息就是刚插入USB设备时服务器控制台上输出的信息。

查看USB设备内的文件:

从上面的信息可以看出,虽然USB设备已插入到Linux系统,但是挂载点mounted on是为空的,也就是还没有完成挂载,必须先挂载后才能使用。

[root@rhel7 ~]# mkdir -p /mnt/MyUSB

[root@rhel7 ~]# mount /dev/sde4 /mnt/MyUSB/
mount: unknown filesystem type ‘ntfs‘

[root@rhel7 ~]# df -hT
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root     xfs        18G  4.6G   13G  26% /
devtmpfs                  devtmpfs  911M     0  911M   0% /dev
tmpfs                     tmpfs     921M     0  921M   0% /dev/shm
tmpfs                     tmpfs     921M  8.4M  912M   1% /run
tmpfs                     tmpfs     921M     0  921M   0% /sys/fs/cgroup
/dev/sda1                 xfs       497M  124M  374M  25% /boot
/dev/mapper/vgdate-lvvol1 xfs       289M   16M  274M   6% /lvmFiles
tmpfs                     tmpfs     185M     0  185M   0% /run/user/0

重新格式化为ext4格式:

[root@rhel7 ~]# mkfs.ext4 /dev/sde4
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
490560 inodes, 1958640 blocks
97932 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2006974464
60 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:      
done

重新挂载后就可以使用了:

[root@rhel7 ~]# mount /dev/sde4 /mnt/MyUSB/
[root@rhel7 ~]# cd /mnt/MyUSB/
[root@rhel7 MyUSB]# touch testMyUsb
[root@rhel7 MyUSB]# ls
lost+found  testMyUsb
[root@rhel7 MyUSB]# 

使用df -hT命令查看挂载的USB设备:

[root@rhel7 MyUSB]# df -hT
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root     xfs        18G  4.6G   13G  26% /
devtmpfs                  devtmpfs  911M     0  911M   0% /dev
tmpfs                     tmpfs     921M     0  921M   0% /dev/shm
tmpfs                     tmpfs     921M  8.4M  912M   1% /run
tmpfs                     tmpfs     921M     0  921M   0% /sys/fs/cgroup
/dev/sda1                 xfs       497M  124M  374M  25% /boot
/dev/mapper/vgdate-lvvol1 xfs       289M   16M  274M   6% /lvmFiles
tmpfs                     tmpfs     185M     0  185M   0% /run/user/0
/dev/sde4                 ext4      7.3G   34M  6.9G   1% /mnt/MyUSB   //已挂载成功了,大小为7.3GB
[root@rhel7 MyUSB]# 

  

Linux系统下查看USB设备名及使用USB设备