Running SSH Server on Android phone while not rooted?

Viewed 118

I am looking into making a tool for my phone. One of the things I need is the ability to SSH into the phone. I was looking into different methods and one method was to use Socat however that requires root access. Another method I was looking into is using JSch library however it only includes the client code.

After looking at a few other SSH sources for Android it says that it requires root. Is this the only way to run SSH Server is by root? As far as I know, getting root requires rooting your phone correct?

Also, I am trying to do this all programmatically. Even if it is using ProcessBuilder on other binaries to get this accomplished. I just want to built it all into my own apk.

1 Answers

You're correct that running ssh requires root, but only if you want to use the standard port 22 (see here for background & discussion on the mutual exclusivity of DalvikVM processes & privileged ports <1024)

If you don't need port 22 specifically, try Apache Mina? In just a few lines of code you can set it to listen to any port >=1024 and embed it in your app, e.g.

var sshServer = SshServer.setUpDefaultServer();
sshServer.setPort(1025);
sshd.start();

There's a bit more to it, but that's the basics.


Also worth noting that if you're running LineageOS, it comes with a standard ssh server (/system/bin/sshd, see this blog) that you can enable by escalating to root, then either patch the exploit or uninstall your rooting tool to revert to an unrooted device. sshd will still work.

Related