Disconnect/connect USB device from command line

Assume I have a Galaxy phone connected via USB to my mac

Listing USB devices is easy, and I can see my phone connected:

ioreg -p IOUSB -l -w 0

Is it possible, given I have the ioreg output, to disconnect a specific device (without PHYSICALLY disconnecting it)?


Solution 1:

It turns out that there is a way of doing this not only in macOS, but in all *NIX systems thanks to the mvp/uhubctl repository over on GitHub. The program is called uhubctl, and allows you to toggle and cycle power on individual ports for supported USB hubs.

As the description would imply, you'll need to know which USB port the device in question is connected to (or cycle the entire hub if you're confident it won't cause and issues with other peripherals), and I still recommend using diskutil eject diskX (or sudo umount /dev/sdX for Linux and umount diskX for BSD) before power cycling the port to ensure that the device unmounts cleanly.

uhubctl home page is here (you can get source code using git clone https://github.com/mvp/uhubctl).

You'll need to compile it from source one way or another, either with make or via brew. By far easiest way to install it in one swoop with brew:

brew tap mvp/uhubctl https://github.com/mvp/uhubctl  
brew install uhubctl

Tested and can confirm working with both Apple and 3rd party USB hubs on macOS 10.13-10.15, and on macOS 11 Big Sur too (including M1 Macs).