Home > Uncategorized > SSH Tricks You Must Know

SSH Tricks You Must Know

Here are some general SSH tips and a short guide for setting up an SSH connection from Mac to iOS devices.

Setting up USB connection to your iDevice

To forward a port from iDevice connected to Mac by USB, you need usbmuxd tcp relay. You can get it from here.
To connect to an iOS device, you can use tcprelay.py from that package this way:

tcprelay.py -t 22:2222

This will create port forwaring from 22 port on the device into 2222 port on your Mac.

For convenience, I have created a script ~/Startup where I am putting various commands to be executed after I log in into my Mac OS. I set this script to be executed in System Preferences -> Users and Groups -> <My Name> -> Login Items. It gets executed by Mac OS terminal so I have killall Terminal line at the end of the Startup script to terminate it after this script is executed. Normally I am using iTerm2, which I recommend to you too.

In this script, I am executing tcprelay.py only once and it is running all the time until I reboot the Mac.

nohup /path/to/tcprelay.py -t 22:2222 &>/dev/null &

It redirects stdout to /dev/null and hohup keeps it running after killing Terminal.

Using SSH key authentication

Because it’s very annoying to enter the password (make sure to change password for both mobile and root users or disable password authentication completely!) each time you connects to a ssh server, you should use SSH key authentication. All you need to do is generate a key and set it. I don’t think it’s worth giving you steps here when you can find them at many other blogs. After you are using ssh key authentication, you can set easily connect to the device without specifying a password (on Mac you just enter key passphrase once per login session).

Using custom hostnames for SSH clients

Because entering “ssh -p2222 mobile@127.0.0.1” each time is annoying as well, folks from OpenSSH introduced config file located at ~/.ssh/config. Create that file if not created yet and create a new block for your local usb ssh like this:

Host iusb
User mobile
Port 2222
HostName localhost

Basically you are here saying that your host is named iusb, that username is mobile, port 2222 and you are connecting to localhost. Save the file and now you can connect to the USB SSH using just “ssh iusb”. Isn’t it sweet?

Preventing host key conflicts when switching USD devices

If you are using SSH over USB with several devices, you are probably seeing key mismatch messages every time you connect a different device and you need to manually remove the old key and confirm the new one. Very annoying! But this can be fixed by a trick I found on some other blog one day. Create ~/.ssh/config file if it doesn’t exist yet and add block like this one

Host iusb
User mobile
Port 2222
HostName localhost
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null

Now, when you enter ssh iusb command, you will be connected without asking or storing host key file.
While you may need to delete the old key first, it won’t be created again.

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.