9

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)?

  • To clarify, by "disconnect", do you mean you want to unmount it from the file system, or make it invisible to your computer? Also, if the former, would you like to be able to reconnect it without needing to unplug it and plug it back in? – Timothy Mueller-Harder Feb 1 '17 at 19:27
  • @timothymh regardless what he is asking, I like to know how to remove a device from diskutil and add it back. Unmount command doesn't remove a device. Right click the device icon and select eject is different, it CAN remove a device from the diskutil, and the only way to add it back is to physically replug it. – aeroxy Aug 1 '17 at 5:55
3

UPDATE: 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. Whichever you choose, you're going to need libusb version 1.0.22 installed. If you have brew installed, you can do it in one swoop by:

brew install libusb
brew tap mvp/uhubctl https://github.com/mvp/uhubctl  
brew install --HEAD uhubctl

Tested and can confirm working with both Apple and 3rd party USB hubs on macOS 10.13.1.

P.S. @aeroxy I concede that all along you were correct, and I was wrong.

If you want to unmount it and have it's UDI (Unix Disk Identifier, i.e. disk1s3 for the 3rd partition on your second drive) you can run diskutil unmount diskXsX from the terminal, and remount it by using the same command, replacing unmount with mount.

If you are using Android Debug Bridge to connect it you can open a terminal, go to whatever folder its installed to, and run ./adb stop to shut down the local Android USB server, then ./adb start to reconnect when you need to.

You could also use Disk Utility to unmount the device.

Finally, if you want to eject it to simulate unplugging it completely without physically unplugging it run diskutil eject diskXsX from the command line, or right-click the phone in Disk Utility and click the "Eject" button.

EDIT: If you are not using ADB or a similar program like Android File Transfer (AFT) then you will not be able to disconnect the device, because it never connected in the first place. In order for an android device to connect to a Unix system (and by extension macOS) you need to fulfill two conditions: 1.) The device must have developer mode enabled, and USB debugging turned on, or your device will auto-deny any requests from another computer to connect through USB. 2.) You must have a program like ADB or AFT installed, these programs run a local Android MTP server and are required to send the connection request to and read the filesystem of the Android device.

Developer mode can be enabled by going to Settings>About Device> and tapping on Build Number 7 times, USB Debugging is in the developer options menu that it now at the bottom of your settings page.

The official AFT app for macOS can be found here: [link] (https://www.android.com/filetransfer/) and the command line version of ADB can be found here: [link] (https://developer.android.com/studio/releases/platform-tools.html#download)

Once both prerequisites have been met you should be able to disconnect the device without unplugging it by stopping the ADB server or by unmounting/ejecting it as one would with any other external filesystem.

  • I can't see the phone in diskutil nor can I use adb (for that matter)... – Shai Feb 2 '17 at 7:52
  • I have updated the answer with info that takes your situation in to account, if ADB will not work in general AFT can be used instead. Why, may I ask, is it that you cannot use ADB? – Alison E.E. Feb 4 '17 at 22:22
  • +1, Thanks for the effort you made in answering my question :) I can't use ADB as I need a solution not only for Android devices, but also for blackberry, iOS, etc etc :) – Shai Feb 5 '17 at 7:55
  • Well, as far as Blackberry OS goes I believe it has been based on the Android Open Source Project for quite some time, so the solution should be the same for those two. With an iDevice it should natively mount like a USB drive and be ejectable via Disk Utility/diskutil if it runs an iOS compatible with your iTunes version. You might have to programmatically check whether the device is Android/BB or iOS/generic USB and use one of the two methods accordingly, as I don't know of a way to universally unmount like that. You might be able to use the unmount command as root if it's a loop device. – Alison E.E. Feb 7 '17 at 21:58
  • 1
    This is not a correct answer. The OP asked for method to Disconnect/connect USB device from command line, your answer is to Umount/mount in diskutil. They are not the same. I can for example, right click a disk to eject it, and then it will be removed from diskutil and cannot be remount again. Nor do we know what command has the same effect to the right click eject. – aeroxy Aug 1 '17 at 5:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .