Gitpod has been my go-to development environment for quite some time now. This week I was working on a Magisk module, but I was doing the development on my local machine. I used my local machine because I wanted to use ABD to pull, push and shell into my Android device from time to time. Today, A friend of mine came to me with a question. Is it possible to access a device over ADB connected to a local machine from a remote machine? I decided to try it because it would help me move my current work on the Magisk module back to Gitpod. This article serves as a reference for my future self.

How does ADB work?

ADB, short for Android Debug Bridge is a command line tool which helps debug an Android device. ADB basically has three components:

  1. CLI: This component of ADB is used to issue various ADB commands.
  2. ADB daemon(adbd): This component is a daemon process which runs on your android device.
  3. Server: The server is started on the development machine and is responsible to make the communication between the CLI and the adbd process running on your device.

What happens when we run adb command?

When we try to invoke any adb command, adb will first make sure that the server is running and is bound to TCP port 5037. If the server is not running on that port, it will start a new server instance.

Untitled

How to use adb on a remote machine?

The ADB client tried to connect to TCP port 5037 by default, so what we are going to do here is to create a remote tunnel to tunnel all requests to TCP port 5037 on my remote machine (Gitpod in this case) to the same port on my local machine.

The tunnel can be setup using ssh using the following command:

ssh -R 5037:127.0.0.1:5037 $USER@$HOST

Luckily Gitpod has started supporting ssh as a way to connect to a workspace.

Let’s get to it

adb on remote machine.png

Prerequisites

  • Make sure to have the same version of adb installed on both the local machine and remote machine. You can download the latest version from here.
  • You should be able to connect to the remote machine over SSH.

The process

  • The process itself is pretty straightforward. Kill any previously started adb server. Use the following command.
adb kill-server
  • Start an ADB server on the local machine. You can do this by simply running the following command.
adb start-server
  • Now that the server is running. Connect your android device and check if adb can detect it. Run the following command to see a list of all the attached devices. This list not only includes physical devices but also emulators.
adb devices
  • If everything is working fine up until now. Let us set up a tunnel to the remote machine. I am using Gitpod workspace as a remote machine. Gitpod gives a simple way to copy the ssh command to connect to the workspace once it has started running.

source: https://www.gitpod.io/blog/copy-paste-ssh-workspace-access

  • The same process can be done for any other remote machine that supports ssh. Now that we have the ssh command to connect to the remote machine, we just have to modify it a bit to set up a tunnel.
ssh -R 5037:127.0.0.1:5037 $USER@$HOST

replace HOST with your username and hostname of your remote machine.

  • What we have done here is basically is, we have set up a communication channel between our remote machine and our local machine saying that any requests to TCP port 5037 on my local machine should be routed back to TCP port 5037 on my local machine.
  • As discussed before adb client(the cli interface) will try to connect to the server on that very same port that we have set up a tunnel for in the previous step.
  • Once the connection is successful, head over to the remote machine and run adb commands, since port 5037 on the remote is wired to 5037 on the local machine adb will not try to start a new server instance on the remote. It is as if the adb server is running on the remote machine.
  • Now when we run adb command on the remote, we should be able to see the devices connected to the local machine and run all sorts of adb commands.

Untitled (6).png

References