Friday, 20 March 2026

Disconnect and power off USB peripheral from the Linux command line

I have a headless Proxmox PVE node that I sometimes need to mount USB storage to. When I'm done with it I can unmount the filesystem on it easily enough with the umount command but then I still have the drive and partitions still connected to the system. (In my case, that looks something like this below with my nvme drives and partitions showing up afterwards.)

root@pm:~# lsblk
NAME                         MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                            8:0    0   1.9T  0 disk 
└─sda1                         8:1    0   1.9T  0 part
 
In a modern desktop environment on Linux you can find buttons or menu options to 'eject' storage like this but it wasn't obvious to me how to do it from the command line for a USB HDD enclosure. [edit: so the `eject` manual page suggests that running `eject /dev/sda` might have also worked. I'll try it next time!]

So I did a Kagi search and found a solution on Reddit that worked.

From https://www.reddit.com/r/linuxquestions/comments/1dq4ha3/comment/lao9dnq/

Echoing the usb port number to /sys/bus/usb/drivers/usb/unbind works for regular usb devices, like external drives or hubs. It removes them from from the bus and stops them consuming power, just as if they were physically unplugged. You need both the bus number and port, which you can get via lsusb -t. For example if the device is on bus 01 port 3, you would use

echo 1-3 | sudo tee /sys/bus/usb/drivers/usb/unbind
Echoing the same to /sys/bus/usb/drivers/usb/bind plugs it back in.

 

No comments:

Post a Comment

Disconnect and power off USB peripheral from the Linux command line

I have a headless Proxmox PVE node that I sometimes need to mount USB storage to. When I'm done with it I can unmount the filesystem on ...