100 Useful Command-Line Utilities

by Oliver; 2014

26. ssh

As discussed in An Introduction to the Command-Line (on Unix-like systems) - ssh, the Secure Shell (ssh) protocol is as fundamental as tying your shoes. Suppose you want to use a computer, but it's not the computer that's in front of you. It's a different computer in some other location—say, at your university or on the Amazon cloud. ssh is the command that allows you to log into a computer remotely over the network. Once you've sshed into a computer, you're in its shell and can run commands on it just as if it were your personal laptop. To ssh, you need to know the address of the host computer you want to log into, your user name on that computer, and the password. The basic syntax is:
ssh username@host
For example:
$ ssh username@myhost.university.edu
If you have a Macintosh, you can allow users to ssh into your personal computer by turning ssh access on. Go to:
System Preferences > Sharing > Remote Login
ssh with the flag -Y to enable X11 (X Window System) forwarding:
$ ssh -Y username@myhost.university.edu
Check that this succeeded:
$ echo $DISPAY	# this should not be empty
$ xclock	# this should pop open X11's xclock
ssh also allows you to run a command on the remote server without logging in. For instance, to list of the contents of your remote computer's home directory, you could run:
$ ssh -Y username@myhost.university.edu "ls -hl"
Cool, eh?

The file:
~/.ssh/config
determines ssh's behavior and you can create it if it doesn't exist. In the next section, you'll see how to modify this config file to use an IdentityFile, so that you're spared the annoyance of typing in your password every time you ssh (see also Nerderati: Simplify Your Life With an SSH Config File).

If you're frequently getting booted from a remote server after periods of inactivity, trying putting something like this into your config file:
ServerAliveInterval = 300
If this is your first encounter with ssh, you'd be surprised how much of the work of the world is done by ssh. It's worth reading the extensive man page, which gets into matters of computer security and cryptography.

<PREV   NEXT>