Sunday, November 2, 2014

ssh to Windows from an iPad

My main computer is Windows, and sometimes I'd like to connect to it via ssh with an iPad and iSSH.  The reason I do this is to get access to an R console on iPad.  The App store has an R console app, but it's limited to packages that the app author included and it can't be extended.  The best way to get a full R installation on an iPad is to ssh to a computer with a full R installation.

A computer needs an active sshd (ssh daemon) to accept ssh connections. Windows doesn't provide an sshd and requires special software.  Many options are available, but most of them are limited or not free.  The best free solution that provides full functionality is OpenSSH, and the best way to get OpenSSH is through Cygwin.

I won't repeat all the steps here, but instead I will supply what I found to be the most helpful resources, and raise some potential obstacles.  NOTE that any instructions completed within a Cygwin terminal will usually require you to launch the terminal 'as administrator,' which you can do by right clicking the executable.

First go get Cygwin:

Before installing Cygwin read through the instructions for setting up an sshd with OpenSSH.  Cygwin is big and you don't need all of it; you can install just the portions you need.  The best walkthrough I've found for setting up OpenSSH's sshd is here:

If you install Cygwin, get the right packages, and start the above walkthrough, you're likely to encounter an error at the portion of the walkthrough where you invoke ssh-user-config.  You might get something to the effect of a home directory that doesn't exist.  This is because Cygwin chooses the wrong default home directory, unless you've set the correct home directory in a Windows global environment variable named HOME.  Thing is, you probably don't want to set a global environment variable for your home folder, but would rather use a user environment variable, but this isn't good enough for Cygwin.  The solution is to regenerate a special file that Cygwin uses to find things like your home directory.  This file resides in C:\\cygwin64\etc\passwd--Cygwin uses some special POSIX to Windows file system conversions, however, and will refer to this file as /etc/passwd.  You could go changing this file by hand with a text editor, but the simplest solution is to invoke mkpasswd and regenerate the /etc/passwd file to point to the correct home directory.  Launch a Cygwin terminal and enter the following:

mkpasswd -l -p "$(cygpath -H)" > /etc/passwd

This tells Cygwin to regenerate /etc/passwd.  The l switch tells mkpasswd to print the local user account--that's you--and the p switch tells it to set the home directory to the specified file path instead of using Cygwin's default method of finding the home.  The next bit, between the quotes, is the argument to p--the specified home path.  We enclose this in quotes because the home path may contain spaces, which must be enclosed in quotes.  The $( is a special syntax that starts a subshell, a shell script within a shell, and returns the results.  Here we make a subshell to invoke cygpath to get the value for the argument to the p switch of mkpasswd.  Are we confused yet?  And why can't we just type out the home path ourselves?  Cygwin is bastard child of scandalous trysts between Windows and POSIX, and while it operates in Windows it still needs POSIX style paths.  The cygpath utility helps convert between Windows and POSIX paths.  The H switch to cygpath is a convenience switch that returns the homeroot of the current user.  Lastly, the > is telling mkpasswd to write the results to the /etc/passwd file--it's a standard *nix thing.  Diligent readers may note that /etc/passwd has already been modified by the ssh-host-config command, but don't worry mkpasswd will keep all that intact and rewrite only the entry for the user's home.

If none of that worked, then there's some more resources on this problem here:

Now you can get back to the walkthrough, and you should be able to finish everything normally.

The next step is to try connecting.  If you could connect to the localhost, as described in the above walkthrough, then you are ready to go.  Launch iSSH and create a new configuration.  You will need to know your computer's IP address on the local network.  To find this launch Window's usual command line terminal--not Cygwin--and type ipconfig /all.  That will list all the IP stuff, there will be a lot of it, but there should be only one IPv4 entry, and that's what you need.  It should be four integers separated by periods, the first integer should be 192, but not always.  In iSSH enter the IPv4 value in the Host field.  Enter 22 in the Port field, and enter your Window's user name in the Login field.  You should also go ahead and enter your Window's login password in the password field.  It should be optional, but for me if I didn't do this the connection would often 'drop unexpectedly' after I entered my password at the prompt; I never had this problem after setting my password here in the configuration setup.

You will probably now encounter another problem: connection timeout.  Windows firewall likely has port 22 blocked.  To open port 22 follow these instructions:

Now try connecting again through iSSH.  If everything works you should find yourself at a Cygwin prompt.  Just type R and you're cookin' with gas!

If you royally fucked up any of the steps and feel like you need to nuke your sshd stuff and start over, then here's an entry at Superuser that can help you do that:

Configuration finished.  Have fun!