Skip to content

rom1504/sshd_android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

sshd_android

How to access your android phone from anywhere using ssh

Motivation

Android is linux. So theoretically you can just run everything you can on a linux box. It's not exactly convenient to type code or commands on a phone, so being able to write it on a computer is quite useful. It's also useful to be able to transfer files easily between a computer and the phone.

Requirements

So I decided to try and install a ssh server on my phone. It had to :

  • run all the time
  • be always available regardless of the network
  • be performant
  • run as root

Existing apps

I tried the various available ssh application and they all have some problems : slow, full of ads, old encryption methods, ...

Solution

performant ssh server that can be run as root : openssh with termux

I then found https://github.com/termux/termux-app which is really useful to run gpu tools, including sshd.

Initially sshd runs as a basic user. In order to make it run as root, https://github.com/st42/termux-sudo is useful. Using sshd -d can also provide useful debugging informations, as well as ssh -V.

That provides a good sshd server that supports ssh keys.

run all the time : init.d

The next step was to make it run all the time. To do that, I tried various methods to setup init.d but it turned out it's different on every phone and it didn't work on mine (nexus 6). So to bypass this issue, I used the init.d scripts support app which simulates the init.d functionnalities by running some scripts (which can be run as root) some time after boot (which is convenient : at that time the phone probably connected to the network).

So I added a script to run sshd at boot time:

SYSBIN=/system/bin
SYSXBIN=/system/xbin
BB=$SYSXBIN/busybox
PRE=/data/data/com.termux/files
export LD_LIBRARY_PATH=$PRE/usr/lib
export PATH=$PATH:$SYSXBIN:$SYSBIN

sshd

The environment variables are similar to the one present in termux-sudo.

be available regardless of the network

Phones usually change of network often, which can have firewalls that block the ssh ports, and it also means the public ip of the phone changes often. That is not convenient to access the phone.

To bypass that problem, I decided to connect my phone to my server using a reverse ssh tunnel.

To do that I created a simple script tun:

autossh -M 0 -f -R 19995:localhost:8022 -N  -o "ExitOnForwardFailure yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 1" user@myserver.com

Important parts :

  • -R 19995:localhost:8022 : that means the ssh server of the phone will be available on port 19995 of your server
  • -o "ExitOnForwardFailure yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 1" : options to improve the connection robustness
  • autossh -M 0 -f : autossh will keep the ssh connection open by reconnecting whenever needed

That script can then be added in your init folder.

I advise to also set ClientAliveInterval 30 and ClientAliveCountMax 1 in your server /etc/ssh/sshd_config file to close lingering connections as soon as possible.

The result is you can then connect to your server from anywhere, and then just run ssh -p 19995 localhost to connect to your phone.

Related information

  • run crond on your phone by following this tutorial

About

How to access your android phone from anywhere using ssh

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published