May 142017
 

Android Robot

The Android Debug Bridge, aka adb, has been an indispensable tool while unlocking and rooting my new Moto G5 Plus. I’ve used it dozens of times this week to push and pull files, run shells, and reboot my phone. Today, as is my habit, I ran the command adb devices after plugging my phone into my laptop, but the response I received made it obvious that I had some troubleshooting ahead of me.

tpodlaski@Alabama:~$ adb devices
List of devices attached
* daemon not running. starting it now at tcp:5037 *
* daemon started successfully *
ZY2243PNMF      unauthorized

The output of adb devices lists the devices currently connected to your computer and recognized by the adb server. In addition to a unique identifier, the state of the device is also reported. Since I’d been using adb without issue for days, I fully expected to see the G5’s state listed as device, meaning it is connected and ready to go. Instead it was unauthorized, a condition I would have to rectify before I could get any work done.

If you aren’t familiar with the ins and outs of connecting to your phone using adb, the unauthorized status might lead you to believe that your phone wasn’t authorized to connect to your computer. It turns out the opposite is true. adb believes that your computer is not authorized to connect to your phone, a situation I knew not to be the case since I had successfully connected the two just earlier today.

Using adb is not as simple as just plugging in your phone, launching a terminal session, and having at it. Well, not the first time anyway. In order for the adb server to even recognize your device at all, you will have had to have enabled the hidden Developer Options settings and then made sure that USB Debugging was set to on. (For more on enabling Developer Options, see my post “How to Enable Developer Options in Android 7 Nougat.”

The first time you connect your phone to your computer after activating USB Debugging, you’ll get a pop up on your phone asking if you would like to authorize your computer to connect to it If you answer yes, the public key of your computer is stored on your phone. Unless you deauthorize your computer, any future connections between your phone and the computer will not require reauthorization.

This is how it should work, but I had not deauthorized my phone since the last time I used adb. I was not prompted to authorize the computer when I connected, and could not think of any reason to explain the unauthorized status.  I hit Google to find an answer, and while I was eventually able to fix the status, I am still clueless as to what caused the issue in the first place.

It turns out this is not an entirely uncommon problem, although the most common solutions were of no help to me. That said, if you are in a similar situation, it’s probably worthwhile to try to the easy fixes first as they did seem to help the bulk of those looking for help.

Possible solutions:

  1. Reboot the phone and the computer. 
    This one almost goes without saying. It’s the first play in every troubleshooter’s playbook. I didn’t see that this method actually solved it for anyone, but it’s still worth a shot.
  2. Stop and restart the adb server.
    Disconnect the device.
    adb kill-server
    adb start-server
    Reconnect the device.
    adb devices
  3. Toggle USB Debugging off and back on.
    Find the USB Debugging setting in the Developer Options and switch it off and back on again.
     Moto G5 Plus USB Debugging
  4. Revoke USB Debugging Authorizations
    In the Developer Options settings find  “Revoke USB Debugging Authorizations”.
    Click it to remove the keys of all authorized computers.
    Reconnect the phone and you should be prompted to authorize the computer.
     
    This solution was particularly troublesome for me because I do not have the Revoke USB Debugging Authorizations option anywhere in my settings. I do not have an explanation for why I don’t, and even now after I have corrected my unauthorized issue, I still do not have it. As such, I can’t speak to whether or not this particular solution will help you.

Here’s what worked for me:

Since none of the above options fixed the problem, I knew I had to dig a little deeper. The RSA keys for authorized computers are stored on the phone in /data/misc/adb/adb_keys. Interestingly, the path existed, but I had no adb_keys file. I don’t have an explanation for why not. I also can’t explain why this didn’t cause the phone to prompt for authorization when connecting to the computer. To fix my problem, I had to copy the public key of my computer to my phone’s adb_keys file.

  1. Turn off your device and disconnect it from the computer.
  2. Boot the device into Recovery Mode.
    For me, using a Moto G5 Plus with TWRP recovery installed:
    1. Press and hold the power and volume down buttons until the phone enters bootloader mode.
    2. Use the volume down key to step through the options until you hit Recovery.
    3. Press the power button to enter Recovery mode.
  3. Open a terminal window on your PC.
  4. Find the directory on your PC where the Android keys are stored.
    On Linux this directory is ~/.android. The public key file is named adbkey.pub.
    tpodlaski@Alabama:~$ ls -la ~/.android
    total 44
    drwxr-x---  5 tpodlaski tpodlaski 4096 May  9 10:31 .
    drwxr-xr-x 46 tpodlaski tpodlaski 4096 May 14 11:58 ..
    -rw-------  1 tpodlaski tpodlaski 1704 May  9 10:31 adbkey
    -rw-r--r--  1 tpodlaski tpodlaski  719 May  9 10:31 adbkey.pub
    -rw-r--r--  1 tpodlaski tpodlaski  185 Dec 19 21:21 analytics.settings
    drwxr-xr-x  2 tpodlaski tpodlaski 4096 Dec 19 21:24 avd
    drwxr-xr-x  2 tpodlaski tpodlaski 4096 Dec 19 19:31 cache
    -rw-r--r--  1 tpodlaski tpodlaski  140 Dec 19 22:21 ddms.cfg
    -rw-r--r--  1 tpodlaski tpodlaski 1258 Dec 19 21:20 debug.keystore
    drwxr-xr-x  3 tpodlaski tpodlaski 4096 Dec 19 21:31 monitor-workspace
    -rw-r--r--  1 tpodlaski tpodlaski   36 Dec 19 19:31 uid.txt
    tpodlaski@Alabama:~$
  5. Connect the phone to the PC with a USB cable.
  6. Verify the adb server sees the phone by running adb devices.
    tpodlaski@Alabama:~$ adb devices
    List of devices attached
    * daemon not running. starting it now at tcp:5037 *
    * daemon started successfully *
    ZY2243PNMF      recovery
  7. If, unlike me, you do have an adb_keys file on your phone, copy it to your PC so that you do not overwrite it. adb pull /data/misc/adb/adb_keys ~/adb_keys
    tpodlaski@Alabama:~$ adb pull /data/misc/adb/adb_keys
    /data/misc/adb/adb_keys: 1 file pulled. 0.0 MB/s (719 bytes in 0.043s)

    Or if you do not have the adb_keys file on your phone, create a blank one on your PC. touch ~/adb_keys

    tpodlaski@Alabama:~$ touch ~/adb_keys
  8. Append the contents of adbkey.pub to adb_keys .  cat ~/.android/adbkey.pub >> ~/adb_keys
    tpodlaski@Alabama:~$ cat ~/.android/adbkey.pub >> ~/adb_keys
  9. Copy adb_keys  back to your phone. adb push ~/adb_keys /data/misc/adb/adb_keys
    tpodlaski@Alabama:~$ adb push ~/adb_keys /data/misc/adb/adb_keys
    adb_keys: 1 file pushed. 0.3 MB/s (719 bytes in 0.003s)
  10. Reboot your phone. adb reboot
  11. When the phone has rebooted, rerun adb devices to verify the PC is now authorized.
    tpodlaski@Alabama:~$ adb devices 
    List of devices attached
    ZY2243PNMF      device

And success! Success for me anyway. Your mileage, as always, may vary. While I am pleased to be authorized again and able to continue on my way, I am still wary of the fact that I don’t know what caused this issue in the first place and why I don’t have a Revoke USB Debugging Authorizations option in my settings. If you have any insight, I kindly ask that you clue me in by leaving a comment below. Thanks!

  2 Responses to “How to Fix ADB Unauthorized Error”

  1. This will overwrite adb_keys file, which might contain multiple keys. An idea to save existing keys might be:

    adb pull /data/misc/adb_keys ~/adb_keys
    cat ~/.android/adbkey.pub >> ~/adb_keys
    adb push ~/adb_keys /data/misc/adb_keys

    • Thank you very much for pointing this out. However I borked my phone, I had completely lost the adb_keys file, so I hadn’t even considered overwriting it. I’ve rewritten the instructions using your suggestion for people who may have an adb_keys files they need to save.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)