SSH Public Key Login From a Rooted Android Phone
I wanted to be able to used my phone to log into my MacBook. The catch? I do not want passwords enabled. This post covers what I did to achieve this goal.
Android does not store users in /etc/passwd so it is exceedingly difficult to add users for command line work. Home directories for the users that are available seem to default to / or /data. /data and the SD card survive OS updates, but only root can properly write to /data. In light of this, this solution is written such that root calls ssh to log into another box. The SD card is used to store a script that contains a convenience function so connections can be canned.
The solution in this post assumes a rooted phone with busybox. My phone is a Nexus 5 running Cyanogenmod 13. As written, this solution may need minor modifications to work other phones.
Software Versions
Disable Password Based SSH
SSH can be enabled on OSX by going to Preferences → Sharing and selecting Remote Login. Optionally allow access for all users.
Password login can be disabled by adding the following lines to /etc/ssh/sshd_config as root. Note that the file is sshd_config, not ssh_config.
/etc/ssh/sshd_config partial listing
Restart sshd as root after making any configuration changes.
Adding an SSH Key to a Rooted Android Device
Open a shell on your phone and switch to root.
Define some variables. Change these if you want to customize your installation. Note that the /data partition survives OS updates.
Create the .ssh directory.
Create a symlink so root ssh will work. The root home directory is /. This step will need to be repeated after an OS update.
Generate the SSH key.
Add .ssh/known_hosts.
Copy the Android public key from the phone to to .ssh/authorized_keys on any machines you want to log into.
As root on the Android phone, ssh into a machine to add it to .ssh/known_hosts.
Convenience SSH Shell Script
Doing command line work with a tiny touch screen keyboard is painful. Convenience shell functions make this task bearable.
I could not get .profile to work out of the box on Android. The Android shell always runs /system/etc/mkshrc. The problem is that this script is replaced when the operating system is updated so the goal is to keep changes minimal. Remount /system in read/write mode so this file can be modified.
Add a line to load a script from the SD card to the bottom of mkshrc.
/system/etc/mkshrc partial listing
Now the workon.sh script will be loaded from the SD card when the shell starts. Files on the SD card can not be executed, so loading the script makes more sense than creating a utility. If the script were placed in /data it could be executed, but only by root. This solution allows a non-root shell to use the script.
Add the following convenience function to /sdcard/workon.sh to log into different machines. Replace the alpha and beta entries with your own machines. This file should survive operating system updates because it is on the SD card.
Redefine SSH_ID if you want to store .ssh in another location. Note that ssh will refuse to use the keys if they are user readable, so putting them on the SD card is not an option.
/sdcard/workon.sh
Now the Android phone can log into machine Alpha like this.
As a bonus, the following can be used to get ready to do admin work on the Android phone.
If you want to add the same function to a normal UNIX box, the workon function looks like this. Put it in something like ${HOME}/.profile.
${HOME}/.profile partial listing
Upgrading
After upgrading the OS, a couple of the steps need to be repeated.
Recreate the .ssh symlink.
Remount /system.
Add the line to load the SD card script to the bottom of mkshrc.
/system/etc/mkshrc partial listing
Alternatively, use the following script to perform the above tasks after upgrading Android.
enable-ssh-workon.sh
References:
- Android, Is there a .bashrc equivalent for android?
- Android, A terminal command for a rooted Android to remount /System as read/write
- Android, Android Partitions Explained: boot, system, recovery, data, cache & misc
- Android, Can I update the adb shell’s environment variables?
- Android, man mksh
- SSH, How do I force SSH to only allow uses with a key to log in?
- SSH, Make a passwordless SSH Connection between OSX 10.10 Yosemite and Linux Server
- SSH, How do I set up SSH public-key authentication to connect to a remote system?
- SSH, RSA vs. DSA for SSH authentication keys
- SSH, How To Enable SSH on Your Mac
- SSH, How to start/stop/restart launchd services from the command line?
- SSH, Disable password authentication on SSH server on OS X Server 10.8
- SSH, man ssh
- SSH, man sshd_config
- UNIX, Using Shell Functions to Jump Into Terminal Projects
- UNIX, In Unix, what is a symbolic link, and how do I create one?
- UNIX, Linux Delete Symbolic Link ( Softlink )
- UNIX, Bash: Strip trailing linebreak from output
- UNIX, Find and Replace Inside a Text File from a ash Command