打开/关闭USB端口
是否可以在ubuntu中使用终端打开/关闭特定的USB端口?
lsusb
显示以下结果
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 003: ID 2232:1020 Bus 002 Device 009: ID 0bc2:a013 Seagate RSS LLC Bus 002 Device 003: ID 0a5c:219c Broadcom Corp.
希捷是我的外置硬盘。 我可以在终端关机吗? 我试过https://stackoverflow.com/questions/4702216/controlling-a-usb-power-supply-on-off-with-linux 。 但是混淆应该替换的是usbX
在遇到同样的问题之后,我发现命令应该以不同的方式输入,以便“sudo”适当地应用权限。
使用“tee”命令。
echo 0 | sudo tee /sys/bus/usb/devices/usb2/power/autosuspend_delay_ms
将root权限应用于“tee”命令,该命令将0写入指定文件,替换当前存在的任何内容。 要追加,请使用带有选项-a的tee命令。
有关信息,请参阅tee
的手册页
所有以前的答案都谈到USB挂起机制,即“逻辑断电”,它们永远不会从USB端口物理切断VBUS + 5V。
如本文所述,只有少数集线器可以实际切断VBUS。
hubpower工具可以做到(如果集线器支持它)。
实际上usbX
只不过是USB
端口号,其中X
表示像1
或2
这样的数字等等。例如,端口1和2的 usb1
和usb2
。 通常,笔记本电脑可能有3个或4个带USB 2.0
和USB 3.0
端口的USB 3.0
端口。
在ubnty usb1
,usb2 … usbX
是/sys/devices/pci000:00/*
。 要理解它,请运行以下命令:
ls -l /sys/bus/usb/devices/
因此,当您要启用/禁用USB Port Number 1
(或参考StackExchange
)时,启用/禁用USB端口usbX
将被替换为usb1
:
编辑
感谢Stefan Denchev告诉使用sudo将某些文本回显到文件的正确方法。 (也检查他的评论。)你现在不应该获得permission denied
消息。
sudo sh -c "echo '0' > /sys/bus/usb/devices/usb1/power/autosuspend_delay_ms" sudo sh -c "echo 'auto' > /sys/bus/usb/devices/usb1/power/control"
看到你的lsusb
结果后,看起来你的Seagate
设备连接到Port No. 2
所以你需要禁用usb2
,然后命令是:
sudo sh -c "echo '0' > /sys/bus/usb/devices/usb2/power/autosuspend_delay_ms" sudo sh -c "echo 'auto' > /sys/bus/usb/devices/usb2/power/control"
希望它有效,你现在可以理解。
udisksctl
有power-off
标志,我建议你与unmount
一起使用
从man udisksctl
:
power-off Arranges for the drive to be safely removed and powered off. On the OS side this includes ensuring that no process is using the drive, then requesting that in-flight buffers and caches are committed to stable storage. The exact steps for powering off the drive depends on the drive itself and the interconnect used. For drives connected through USB, the effect is that the USB device will be deconfigured followed by disabling the upstream hub port it is connected to
演示
这是我卸下我的USB跳线然后关闭它的电源
testdir:$ lsusb Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 009: ID 154b:007a PNY Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub testdir:$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 111.8G 0 disk └─sda1 8:1 0 111.8G 0 part / sdb 8:16 1 30G 0 disk └─sdb1 8:17 1 30G 0 part /media/xieerqi/6A32C4555E1C5B4D sr0 11:0 1 1024M 0 rom testdir:$ udisksctl unmount -b /dev/sdb1 && udisksctl power-off -b /dev/sdb1 Unmounted /dev/sdb1. testdir:$ lsusb Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub testdir:$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 111.8G 0 disk └─sda1 8:1 0 111.8G 0 part / sr0 11:0 1 1024M 0 rom testdir:$