Posted on

Remote SSH Into Your Mac Through iCloud

Everyone has experienced times when they wish they could access their home computer while on the road.  Maybe you forgot an important document, want a special music file, or just forgot to shut your computer down.  If you have 2 Macs both running iCloud (Mac OS X 10.7 Lion or 10.8 Mountain Lion), you can easily SSH intothe other one using the Terminal in just a few easy steps, giving you remote access from anywhere.

Before we get started, make sure Back To My Mac is enabled on your Macs.  To check, goto System Preferences and the iCloud preferences to make sure that the Back To My Mac option is enabled.  Next you must make sure that sharing is enabled on the computer you will SSH into.  Goto the Sharing system preference and make sure that the boxes for File Sharing and Remote Login are both checked to allow those sharing types.

Now that you have those items setup, it’s time to find your iCloud account number.  In the Terminal (Applications > Utilities), type the following:

dns-sd -E

The final 9 digits are your iCloud account number.

Now it’s time to SSH into your Mac through iCloud.  Type the following (or copy and paste it) with your iCloud account number inserted into the space given:

ssh -2 -6 username@computer-name.[account number]

You will need to know your computer’s name and your username before you SSH into it.  This name can be found by going to the System Preferences  Sharing preference on the destination computer.  Click on Remote Login. Your Computer name is listed at the top (if it’s multiple words use the address with the dashes). Your username is listed on the line below Remote Login right before the @ symbol.

All this information can be a lot to remember.  If you may use this frequently, you may want to add the following to your ~/.ssh/config file:

Host mac-remote
User username
HostName username@computer-name.[account number]
AddressFamily inet6
Protocol 2

Then can can simply type the following into the Terminal to SSH into your remote computer via iCloud:

ssh mac-remote

If you don’t want to hardcode your Back To My Mac address into your .ssh/config file, you can grab it dynamically each time by using this line:

echo show Setup:/Network/BackToMyMac | scutil | sed -n ‘s/.* : *\(.*\).$/\1/p’

While Terminal does have built-in support for Back To My Mac, it doesn’t always function correctly so you may not want to rely on it (although it is easier to use when it does work).

Open Terminal, goto the Shell menu and select New Remote Connection…

In the Services list, click Secure Shell (ssh)

Your Back To My Mac computers should begin to populate on the servers list along with any Bonjour servers on your local network.  Simply click the desired computer’s name, enter your username and password and click the Connect button.

SSH is a great, secure way to access your Mac remotely and with iCloud and Back To My Mac, you can do so from anywhere with ease.