# SimpleSSHD

SimpleSSHD is an SSH2 server based on dropbear that supports scp, sftp, and rsync. It only supports public-key based authentication (no password/interactive auth except for bootstrapping). It does not use root, which means it must listen on a port over 1024 (defaults to port 2222).

SimpleSSHD version 20 was released 2019/06/16 and features significant changes -- a new version of Dropbear 2019.78 and a new version of the Android SDK. If you are having troubles with the upgrade, please email me!

## Quick start

- Install SimpleSSHD.
- On any computer which will connect to your Android device, set the port to 2222. With OpenSSH, this can be done by putting these lines in `~/.ssh/config`:

      Host myphone
      Port 2222

- Launch SimpleSSHD, and in Settings enable "Start on Boot", then manually start it for the first time.
- Create `authorized_keys` in the home directory (do not use `ssh-copy-id`).
- Optionally run `restorecon -F authorized_keys` (LineageOS needs it for "SELinux context").
- Optionally make `.profile`.

If SimpleSSHD does not find an `authorized_keys` file when a client connects, then it generates a single-use password at that time and displays it in the console log. So the procedure to login the first time is to initiate the ssh connection, then look at the phone and type in the password that is on the screen in the SimpleSSHD app. It is recommended to use that shell session to install the `authorized_keys` file.

Once `authorized_keys` exists, only public key authentication is supported. If you screw up your `authorized_keys` file, use the options menu (upper right) -> Reset Keys.

The default home directory is now the app-private directory, which will generally be something like `/data/data/org.galexander.sshd/files`, but may vary depending on your device. Use options menu -> Copy App-private Path to view the path or copy it onto the clipboard. A nice feature of the app-private directory is it generally supports the full range of Unix file permissions, including execute! A disadvantage is that these files all disappear if you uninstall SimpleSSHD.

Once you get setup, you may want BusyBox. Check out SimpleBusyBox.

## Settings

Generally speaking, you must re-start the SimpleSSHD service for any of these settings to take effect.

Start on Boot
  Automatically start the SimpleSSHD service when the phone powers on.
Start on Open
  Run the SimpleSSHD service whenever the app is launched. The "STOP" button will be replaced with a "QUIT" button that stops the service and exits the app. This is useful if you want to only run the service when you really need it -- start the app to start listening for connections, and then click "QUIT" when you are done.
Foreground Service
  Run the SimpleSSHD service as an Android "foreground service." This means a notification will be displayed for the service (you might be able to block the notification by long tapping the notification). On newer versions of Android (since Marshmallow), this may also make SimpleSSHD more responsive if the phone has been in Doze mode. It does not seem to increase battery consumption much.
Port Number
  This is the port number that SimpleSSHD will listen on. It must be greater than 1024 (because SimpleSSHD does not have root).
Buffer hack for rsync+SuperSU
  A bug in SuperSU interacts with an infelicity in rsync to cause an occasional deadlock. This is a real pain because I use SuperSU+rsync for system-wide backups, which will always eventually trigger the deadlock. To work around this, there is a "Buffer hack" option that should be enabled if you use rsync with SuperSU. It adds a miniscule overhead to rsync, but isn't harmful even if you aren't using SuperSU.
Extra Commandline
  You can specify additional commandline options for dropbear server. The options which are hardcoded are:

  >     -R -F -p :portno

  You can use `\` or `"` to escape characters in this commandline.
Environment Variables
  You can specify environment variables to define before executing the shell. There is one variable per line, with a "name`=`value" format. A common use would be "`ENV=/data/data/org.galexander.sshd/files/.profile`", if your device has a build of `mksh` that does not execute a `.profile` automatically.
SSH Path
  Path for general dropbear files, which now defaults to the app-private directory (usually something like `/data/data/org.galexander.sshd/files`). Most importantly, this is where `authorized_keys` is found. It is also where host keys wind up (they are created on demand), and where temporary files go. Other than the app-private directory, it may go under `/sdcard`.
Home Directory
  The login shell's home directory, which also defaults to the app-private directory. This is where your ssh session starts out, and is where `.profile` will be found if it is present.
Login Shell
  Path to the command shell. The default Android shell is `/system/bin/sh`, but you may prefer to use the one that comes with busybox or whatever. If you set it to the su binary (i.e., `/system/xbin/su`, or maybe `/su/bin/su`), then scp, sftp, and rsync will run as root and should be able to access files outside of `/sdcard`. If the name of the shell contains "`su`" in it, instead of putting "`-`" before `argv[0]` to indicate the login shell, SimpleSSHD puts "`-`" as `argv[1]`. That is, a regular login shell would be invoked as "`-sh`", but su will be invoked as "`su -`".

## Broadcast intents

SimpleSSHD registers receivers for broadcasts that you can use to start/stop the service programmatically, using an app like Tasker, or the commandline tool `am`.

action: `org.galexander.sshd.START`
or: `org.galexander.sshd.STOP`
app: `org.galexander.sshd`
category: `android.intent.category.DEFAULT` (optional).

Note that these intents will only work on phones with Android 8 (Oreo) or later if Settings -> Foreground Service is enabled.

For example, to start sshd service under shell, you could run:

    am broadcast -a org.galexander.sshd.START org.galexander.sshd

You may need to specify `--user 0` depending on the context:

    am broadcast --user 0 -a org.galexander.sshd.START org.galexander.sshd

If you are getting an error from `am` like:

    cmd: Failure calling service activity: Failed transaction (2147483646)

it can probably be solved by disassociating `am` from the ssh terminal (I don't know why). This can be accomplished by redirecting stdin, stdout, and stderr. For example, I can actually stop sshd from within an ssh session using this command:

    am broadcast --user 0 -a org.galexander.sshd.STOP org.galexander.sshd < /dev/null > /dev/null 2> /dev/null

## Frequently Asked Questions

Here are some common complaints:

- Writing to an SD card is not generally possible -- your mileage may vary. Modern Android (since about Kit Kat) only allows write access to an SD card through the Android Java API, but shell commands usually use Unix APIs (POSIX, C). I don't know a work-around. Most phones let you write to `/sdcard`, but it's often in the phone's internal memory.
- If you installed an `authorized_keys` file, but SimpleSSHD acts like it doesn't exist, try running "`restorecon -F authorized_keys`". Some Android configurations, such as LineageOS, run apps in an "SELinux Context" (don't ask me) where that kind of thing is necessary.
- `rsync` will do too much work for an update, because there is usually no way to update the ctime, mtime, or atime on files in `/sdcard`. Use `rynsc --size-only`, and it will compare the file sizes instead of the modification time.
- For the same reason, `rsync -a` will generally fail. Use `rsync -r` instead.
- "`Not backgrounding`" is just the string that dropbear outputs when it isn't asked to `fork()` when it starts up -- it's harmless.
- If you really must specify which IP to listen on, consider setting Port Number to 22 (which will fail and be ignored), and then add `-p 192.168.1.123:2222` to Extra Commandline.
- `ed25519` keys are not supported by Dropbear (yet). Use a regular `rsa` key for greatest compatibility.
- Do not use `ssh-copy-id`, it will try to put the `authorized_keys` file in the wrong place.

## Change Log

- 2020/03/XXX Version 25: Fix compatibility with WinSCP.
- 2020/01/01 Version 24: Fix crash with "Start on Open" on Android 9. New TV icon. Improve performance.
- 2019/09/21 Version 23: Support Android TV.
- 2019/09/01 Version 22: Add broadcast intents for start and stop.
- 2019/08/11 Version 21: Fix restarting after a crash. Fix SIGSEGV on some Android 9 devices.
- 2019/06/16 Version 20: Update SDK. Support x86_64, arm64. Fix finding support libraries. Fix colors again. Fix start-on-boot on new Android. Update to dropbear 2019.78.
- 2018/05/19 Version 19: Use the app-private directory by default on new installs for the SSH/home dir. Organize settings. Fix black-on-black text on some devices.
- 2018/05/16 Version 18: Use correct idiom for `su` as a login shell (so busybox `su` can work). Add environment variables to settings.
- 2018/03/24 Version 17: Add native x86 support.
- 2017/10/28 Version 16: Fix rsync on Android 8.0 (Oreo).
- 2016/10/17 Version 15: Fix crash on pre-Nougat versions of Android.
- 2016/10/16 Version 14: Support single-use passwords for bootstrapping.
- 2016/10/16 Version 13: Support foreground service (fewer stalls with newer Android). Add "Start on Open" mode.
- 2016/08/08 Version 1.11: Fix stall initializing random number generator.
- 2016/07/22 Version 1.10: Enable TCP port forwarding.
- 2016/07/04 Version 1.9: Restart sshd if the system kills it.
- 2016/06/21 Version 1.8: sftp, rsync, and scp now work on files bigger than 2GB.
- 2016/06/19 Version 1.7: Don't clear environment variables (so you can use `am` or `pm` more easily). Add "Authorized Keys" menu option to help maintain the `authorized_keys` file.
- 2016/06/12 Version 1.6: Fix typo that broke scp/sftp/rsync in version 1.5.
- 2016/06/11 Version 1.5: SuperSU+rsync hack now limits the buffer size to 1MB.
- 2015/11/23 Version 1.4: Don't abort if IP address lookup fails, and make proper "About" menu.
- 2015/06/21 Version 1.3: Hack to make rsync work with SuperSU.
- 2015/01/18 Version 1.2: Displays the IP address it's listening on.
- 2014/12/29 Version 1.1: Works with Lollipop now.
- 2014/12/22 Version 1.0: Initial public release.

## About

SimpleSSHD is built using parts from open source projects: Dropbear SSH, openssh (for scp and sftp-server), and rsync. Those projects in turn are built on other projects. A lot of people's work is represented here, and very little of my own.

But if you find bugs/infelicities, it's probably my fault, so email me:

To get the source:

    git clone http://galexander.org/git/simplesshd.git

Direct APK download:

- SimpleSSHD-19.apk - last version to target SDK 11, Android 3.0 Honeycomb.
- SimpleSSHD-23.apk
- SimpleSSHD-24.apk