Auto-connect CyanogenMod Terminal to a SSH Server

This article explains how to setup the CyanogenMod Terminal Emulator to automatically connect to an SSH server when loaded. It is based on CyanogenMod 10.1.1 and uses key based authentication to avoid the need to type a password.

This can be done on-device using the terminal however having the SDK installed will make it a lot easier and safer as if you break the Terminal app you're stuffed. This article will assume you're using the SDK.

Setting Up Key Based Authentication

If you want to type a password on each connect, you can skip this section and go straight to Configuring the Terminal Emulator.

Creating and copying the key

The first step is to create the authentication keys. To do this, you will ADB to the device, create the keys and copy them to the server you want to automatically login to. The commands to type are highlighted in bold.

[user@host-pc ~]$ adb shell
shell@android: busybox sh
~ $ su
shell@android:/ # ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/data/.ssh/id_rsa): <ENTER>

This will create two files in /data/.ssh/: id_rsa and id_rsa.pub. The .pub file is the public key you need to copy to the server.

shell@android:/ # scp /data/.ssh/id_rsa.pub user@target-server:~/.ssh/android.pub

Adding the Key to Authorised Users

Next you must tell the server you want to allow the key for password-less logins. SSH into the target server and run the commands below.

[user@host-pc ~]$ ssh user@target-server
[user@target-server ~]$ cd .ssh
[user@target-server ~]$ cat android.pub >> authorized_keys

Fixing the Permissions

At this point, you have setup key based authentication but the Terminal Emulator application cannot access the keys to use them. We will fix that now.

Load the Terminal Emulator on the device, you will notice the prompt is something like u0_a43@android:/ $, the u0_a43 part of this tells you the user the Terminal Emulator is running under; we need to give this user permission to read the keys. Back in ADB shell, run the command below replacing the username with that shown in your Terminal Emulator.

shell@android:/ # chown u0_a43.u0_a43 /data/.ssh
shell@android:/ # chown u0_a43.u0_a43 /data/.ssh/*

You should now test that the authentication is working by trying to SSH into the server from the Terminal Emulator application. If it fails, check the permissions and re-cover the steps above to make sure you haven't missed anything.

Apply Permissions on Boot

CyanogenMod will reset these permissions on each boot, so we need to create a init script that will apply these permissions on startup. To do this, we will use the VI text editor that ships with CyanogenMod, but any text editor will suffice so long as it can write to /data/local.

Type vi /data/local/userinit.sh into the ADB terminal. Provided you haven't created this file already, you should be presented with an empty file, otherwise just append the commands to the bottom.

Press i to enter insert mode and paste in the following two lines (remembering to replace the username with the username for your device):

chown u0_a43.u0_a43 /data/.ssh
chown u0_a43.u0_a43 /data/.ssh/*

Finally type :wq to write and quit the VI editor. Reboot the device and confirm that the Terminal Emulator can still login using keys.

Configuring the Terminal Emulator

Next we want the Terminal Emulator to automatically connect to the server on load, this is fairly simple as the application provides a means to launch a command on startup.

Load the Terminal Emulator and press Menu > Preferences. Scroll down and press the 'Initial Command' option and enter the text below, replacing the user@target-server with your username and host name.

echo off; clear; ssh user@target-server;

The 'echo off; clear;' commands are optional but will put you to a nice clean login prompt. When you disconnect from the server, you will drop back to a local terminal. If you want to exit the Terminal Emulator instead, append 'exit;' to the above command. Make sure to test that it is working before adding this as it can cause the terminal to launch and exit immediately if it cannot connect to the SSH server.

If at any point you murder the Terminal Emulator application, go into Settings > Applications and use the 'Clear Data' button on the Terminal Emulator.