Skip to content

Instantly share code, notes, and snippets.

@KreAch3R
Last active December 22, 2023 17:14
Show Gist options
  • Save KreAch3R/21bc193bf313ee2b198eaf763eb4c5bb to your computer and use it in GitHub Desktop.
Save KreAch3R/21bc193bf313ee2b198eaf763eb4c5bb to your computer and use it in GitHub Desktop.
SSH: connect to a remote ADB server

Case:

A laptop running Android Studio and a linux server somewhere (office, basement, other side of the world) with connected Android devices. (No Android device connected on local laptop). Normally, you would have to unplug the device from the server, plug it into the laptop, continue development.

Now you can skip this.

(From now on, local is the laptop. The machine which doesn't have anything connected. remote is the machine which has the adb devices but doesn't have anything useful to do with them)

It can work both for Windows WSL and Linux local.

Step 1: Install LINUX SDK Tools on local

Install it as you would normally do in Linux, aka inside ~/bin for example, so you end up with a adb PATH like this:

$ which adb
/home/kreach3r/bin/android/sdk/platform-tools/adb

Step 1.1: Make sure you don't have android-tools-adb

or other equivalent programs installed.

Step 1.2: Place adb into PATH

With whatever method you can find, I choose to place it inside ~/.bashrc.

Step 2: Install LINUX SDK Tools on remote

Same procedure as before. End up with same path. Place into PATH.

Step 3: Use your favourite terminal emulator on local

I only tested this with ConEmu. Open a {Bash::bash} terminal window. In Linux, choose whatever you want.

Step 4: Set up your SSH config file on local

The path should be ~/.ssh/config. Make sure you have a working SSH connection with the host SERVER before proceeding.

Step 5: Add the forwarding port argument in the SSH config file on local

Example:

Host SERVER
    Hostname www.server.com
    User kreach3r
    IdentityFile ~/.ssh/id_private
    Port 22
    LocalForward 5037 localhost:5037

Step 6: Run adb devices on remote in ~/.bashrc

This is optional, but it saves you having to do it manually each time.

# Always start adb server after login
adb start-server &> /dev/null

ADB server should start.

Step 7: Run adb devices on local

Same devices should appear! Profit!

Step 8: Install autossh and keep the forward port alive

Again, this is optional, but saves you having to open another terminal window to ssh SERVER to keep to start/keep the connection alive.

sudo apt install autossh

Place this into your local ~/.bashrc:

# Run autossh daemon for local forward porting without needing to login manually to ssh
autossh -M 20000 -f -N SERVER

References:

http://www.tinmith.net/wayne/blog/2015/11/android-remote-adb.htm
https://stackoverflow.com/a/27879297
https://stackoverflow.com/a/9532938
https://stackoverflow.com/a/9146502
https://unix.stackexchange.com/a/134545
https://serverfault.com/a/598276/404661

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment