This is still the header! Main site

Talking to your Android phone from anywhere

2022/07/17

... how to "ssh" into your Android phone from a server far away?

(tl;dr: combine adb tcpip with Tailscale!)

Why?

Ever wished your Android phone was an Actual Computer? (... well, it is, it's just hiding it well.) Like... if only you could, say, write a cron job that checks where it is? Or just remote-desktop into it? While your processes aren't randomly killed because Android doesn't like them?

... ADB?

I'll assume you know what ADB is. If not... you can look it up: it's the standard way of debugging Android phones for developers. You can run commands in a shell, pull files, start apps, send keyboard events... many things that Android works very hard to prevent arbitrary apps from doing. You don't even need root for most of it: you can just enable developer mode.

Except... there is a catch. It needs a USB cable between your computer & the phone. Which is fine if you just want to do Actual Development, but... it's slightly inconvenient if you just want to rsync a few photos off it. It's, at least inconvenient enough to make you stop doing whatever you want to do regularly. It's also impossible to automate.

Alternatives

There are apps that launch an SSH server on the phone. The problem is that... they're apps. Thus, they're a lot less powerful than ADB. (... also, I'm not entirely sure that they'd be allowed to keep running with an open port all the time).

No USB!

You can get around this though. Actually, Android supports remote debugging, via a TCP port, which has the same capabilities as doing it over USB!

The catch is... you need to first enable this with, well, a USB cable. It's not hard though; once your phone is connected, you can just say

~ $ adb tcpip [portnumber]
          

... and then you can connect your computer to it with

~ $ adb connect [ip address]:[the port you picked above]
          

... which will give you the same access as if you had an actual USB cable connected. Then, once you're done, you can disconnect with

~ $ adb disconnect
          

You then only have to repeat this after a phone restart.

As expected though, you generally have to be on the same wifi network though; phone carriers are doing a good job at firewalling away open ports (in our case, it's probably for the better). So you can do stuff with your phone while you're at home, but...

... enter Tailscale!

As you might have noticed, I kinda like Tailscale. It's a VPN thing that feels like you had actual LAN cabling connecting all your devices, no matter where they are.

Which... is... pretty much we'd need for this.

And, as it turns out, the ADB TCP server on the phone does happen to listen on the Tailscale IP, too. Which... you can talk to any time. Even if you're sitting in a cafe with a laptop and your phone is still on LTE. Even if you're in a cafe and it's a server back home that's doing the connecting with a cron job.

Practicalities

Admittedly, I haven't tested this solution a lot yet... but regardless, here are some preliminary findings.

It always works. You're now in the same class as big tech companies' notification services: even if the phone is asleep, you can wake it up!

It also looks kinda good on power. Having adb tcpip on doesn't prevent the phone from sleeping. Apparently, connecting, even leaving an actual shell open, doesn't prevent the phone from sleeping?

My experiment for this was just to run a for loop in the shell with a sleep 0.3 in it; it becomes a lot more choppy once the screen is not on (indicating that it's asleep most of the time). On the other hand... this might prevent it from going into deeper sleep levels; the jury is still out on how bad this is for, say, an entire day.

... remote desktop?

Well, yes, I mentioned that in the beginning. So, if you're interested: scrcpy is a thing. It's basically remote desktop (normally) over USB, with compressed video streaming of frames... but then it's using ADB for all of it, so you can do this with the above setup, too!

(... it's also open source, and looks reasonably non-overcomplicated.)

... comments welcome, either in email or on the (eventual) Mastodon post on Fosstodon.