The Joy of ssh: remote control and quick copying

One well-established method of building fault-tolerant systems is to provide several alternative ways of doing things. When your Mac gets into trouble, the fact that it has multiple options for restarting or shutting down is important.

You will normally reach for the Restart… command in the Apple menu. If your Bluetooth mouse/trackpad isn’t playing, you can connect it using its charging lead and see if it works over USB. If that doesn’t help, and your Mac appears to have frozen, most users head for the Power button next. Although that’s normally reliable, it’s quite a blunt instrument and doesn’t give you any options as to what happens before your Mac restarts or shuts down.

It’s much smarter to connect to the sick Mac from another Mac, or indeed another computer, and take control remotely. You can then try restarting key systems, copying files off in case they get blown away during the restart, and so on. For client systems this is valuable; for servers it is often essential. Because this command-based control doesn’t rely on the great majority of macOS running properly – it has no GUI, for instance – you can often do this successfully even when that Mac appears to be frozen and unresponsive.

The way to do this is using the secure shell, ssh, in Terminal. It may seem daunting at first, but this is one of the best practical uses of the command line for regular users who see no good reason to go near it otherwise. Prior to High Sierra, you could use plain old telnet, but ssh is far better, and 10.13 has done away with both telnet and the insecure ftp tool for transferring files.

ssh01

There’s one crucial step you must take before you can make ssh or anything based on it happen: you have to enable it on the target Mac, the one that you’re going to connect to. One of the first things that I normally do when I get a Mac is to switch this on and configure it, in the Sharing pane. I also enable Remote Management, and restrict these services to myself, as the admin user.

Whilst you’re about those, if you have any doubts about the resilience of your firewall to block all incoming connections, or suspect that your password could usefully be stronger, now is the time to do something about them, before you enable sharing services which could otherwise be the jackpot for an intruder.

ssh02

Next, I check that ssh is working correctly by typing in the basic command to connect to that Mac from another system. This is where it’s handy to have configured its Network settings using a fixed IP address, as you will already know what to use. Then in Terminal type
ssh username@192.168.63.3
where you give your username on the Mac which you’re connecting to, and its IP address. The first time that you do this, ssh will obtain and save the remote system’s fingerprint, so you’ll need to type yes to its request to continue the connection. You’re then prompted for that user’s password on the remote system, and you’re in and running as that user at the top level of the Home folder. Try a couple of commands like ls to confirm that everything is good, and then type
exit
to log out.

ssh03

If you’re going to do this much, it’s worth setting up an easy connection using the New Remote Connection… command in Terminal’s Shell menu. That is extremely simple to do, and will be available when you next want to connect.

Once you’re connected by ssh to a sick Mac, the choices continue too: if you can, restart it using
shutdown -r now
which should be more graceful than the direct
reboot

The other service which is worth being familiar with is scp, which replaces ftp for transferring files. With one Terminal window open and running ssh, you can list the folders containing the files you want to transfer. Then in the other, type a command like
scp file username@192.168.63.3:Documents/
to transfer file into the remote ~/Documents folder, or
scp username@192.168.63.3:Documents/file Documents/
to transfer file from the remote ~/Documents folder into your local ~/Documents, for example.

ssh04

scp uses ssh, so once you have configured and tested the ssh connection, scp should be a breeze to use.

Using tools such as ssh and scp may appear old-fashioned and clumsy. But once you have enabled them, they are often quicker than their GUI equivalents, and normally work when the GUI has become a lot of pain and grief. And they’re excellent fallbacks which can get you out of trouble when the only option seems to be that Power button.