Cyrus Stoller home about consulting

Useful SSH tricks

Here are some things that I wish someone had shared with me when I first started using SSH.

Basics

If your username is the same on your local machine as it is on your remote, you can leave it out. For example, if your local username and remote username are both cyrus then you can just type ssh example.com instead of ssh cyrus@example.com.

You can also run commands directly. For example, you can type ssh cyrus@example.com pwd and it will execute pwd on your remote host.


SSH Config

By adjusting your SSH config, you can setup aliases for servers that you connect to often, significantly reducing boilerplate typing.

# ~/.ssh/config
Host revtilt
  Hostname alpha.revtilt.com
  User deployer
$ ssh revtilt # is equivalent to the following
$ ssh deployer@alpha.revtilt.com

That alone was a game changer for me since it makes logging into remote servers so much easier. But, if I’m working on a remote host, it’s likely that I’ll scp or rsync files or open another session. You can speed up your remote commands by sharing the same connection. Without the following configuration you negotiate a separate SSH handshake for each session.

Host *
  Controlmaster auto
  Controlpath ~/.ssh/ssh-%r@%h:%p.sock

This creates a socket at the Controlpath that is shared by ssh, scp, rsync, etc… The %r is the User, %h is the Hostname, and %p is the Port.

For a full listing of the options you can configure, go to:

$ man ssh_config

SSH Tunneling

Create a tunnel from a remote machine’s port 80 to your local port 2001. This is useful for getting around firewalls.

$ ssh -N -L2001:localhost:80 remotemachine

Proxy

You can also create a lightweight SOCKS5 Proxy. This is useful when you want to watch Netflix outside of the US or live coverage of the Olympics on the BBC.

$ ssh -D localport host

Helpful resources

Category Tutorial