Script to automatically set up sshd under Cyanogenmod

This is a shell script I created which will automate the process of configuring sshd for Cyanogenmod (as of this writing release 10.1).

The script is designed so that you can have the following directory:

/sdcard/backup/sshd

which holds the template config files necessary for sshd. It will then backup any existing config files before overwriting, copy the template files over, then attempt to start the ssh daemon.

I created script because every time I have to upgrade Cyanogenmod it involves wiping /data and hence the config files for sshd. Now, with my handy script, I can just execute:

sh /sdcard/backup/sshd/sshd_setup.sh

and it will setup the sshd daemon with the working config files. One thing was tricky about this: I found out that its not possible to create an executable file in the /sdcard directory for security reasons. Hence the script, which will only be set with read/write permissions by default, must be invoked with the sh command as above.

For this script to work the following files must exist:

/sdcard/backup/sshd/data_ssh/authorized_keys
/sdcard/backup/sshd/data_ssh/sshd_config
/sdcard/backup/sshd/90sshd

The script itself I put in:

/sdcard/backup/sshd/ssh_setup.sh

Here are copies of the template files, which I have described how to create in a previous post. You will still need to consult that page for information on setting up the authorized_keys file for the first time.  If you do not have an existing one to use as a template with this script then create a blank one and put it in /sdcard/backup/sshd/data_ssh

sshd_config
90sshd

And the setup script itself:
sshd_setup.sh

Here are the contents of the script:

#!/system/xbin/bash -e

BDIR="/sdcard/backup/sshd"

# First back up existing data before overwriting
# Just to be safe
SDIR="$BDIR/`busybox date +%Y.%m.%d-%H%M%S`"
mkdir $SDIR
cp -a /data/ssh $SDIR/data_ssh
if [ -e "/data/local/userinit.d/90sshd" ]; then
   cp -r /data/local/userinit.d/90sshd $SDIR/
fi

# Copy over /data/ssh directory template
cp -r $BDIR/data_ssh/* /data/ssh
chown root.root /data/ssh/authorized_keys
chmod 644 /data/ssh/authorized_keys
chown root.root /data/ssh/sshd_config
chmod 644 /data/ssh/sshd_config

# Copy over /data/local/userinit/90sshd
if [ ! -d "/data/local/userinit.d" ]; then
   mkdir /data/local/userinit.d
fi
cp $BDIR/90sshd /data/local/userinit.d/
chown root.shell /data/local/userinit.d/90sshd
chmod 755 /data/local/userinit.d/90sshd

echo "Finished setting up sshd"
sleep 1
echo "Now attempting to start sshd"
/data/local/userinit.d/90sshd

 


Comments

Leave a Reply