SSH Tips

Introduction

The ssh application is essential to just about everyone. Are you using it as efficiently as possible? Check out these tips and config settings that will make your life easier, including aliases, specifying identify files, forwarding ports, and using jump hosts.

We will assume you already have OpenSSH installed. It comes installed by default with most Linux distributions. If you are using Windows, try Windows Subsystem for Linux.

SSH options

SSH has a lot of potential options:

ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
    [-D [bind_address:]port] [-E log_file] [-e escape_char]
    [-F configfile] [-I pkcs11] [-i identity_file]
    [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
    [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
    [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
    [user@]hostname [command]

Some simple usage example uses may include:

ssh devdungeon.com
ssh nanodano@devdungeon.com
ssh -p 2222 -i ~/.ssh/custom_id_rsa devdungeon.com

It can be tedious to configure all of the options using command-line flags, so the SSH config file allows you to set settings for different hosts and access them using a convenient alias.

In the SSH config file you can configure aliases to use. For example, if we set up an alias for host dd for devdungeon.com, we could simply connect using:

ssh dd

We will look at an example configuration in the next section.

SSH config file template

# Example ~/.ssh/config

# Connect using ``ssh sandbox`` or ``scp sandbox:* .``
Host sandbox 
    HostName sandbox.local
    User myusername
    IdentityFile ~/.ssh/id_rsa
    Port 22
    ServerAliveInternal 30
    ProxyJump jumphost.local

    # If using local forward, do ssh -f -N host
    # -f puts ssh in background
    # -N makes it not execute a remote command
    LocalForward 9906 127.0.0.1:3306

    # Another local forward example.
    # This one will take the service that sits on the remote host
    # listening locally on port 3006 and makes it available
    # via a local listener that listens on localhost:9999.
    # E.g. Make a remote database that only listens locally available
    #      on your localhost port 9999
    LocalForward 127.0.0.1:9999 127.0.0.1:3306

    # Remote forward will make something available on the local 
    # machine/network available to the remote server.
    # E.g. Making an internal git server available to the host you are
    #   connecting to that would not normally be able to reach the git
    #   server. This eample makes git available on localhost:9999 while on
    #   the remote SSH server
    RemoteForward 127.0.0.1:9999 internal-git-server.local:22

    # Set up a SOCKS proxy on 127.0.0.1:9999 that lets you pivot through the remote host(HostName) 
    # Then configure the local tool/browser to use a SOCKS proxy of localhost:9999
    DynamicForward 127.0.0.1:9999

Host myotherhostalias
    HostName example.com

Connecting through jump hosts

SSH through jump host with the -J option:

ssh -J myjumpserver destinationserver

SCP through jump host with the -o ProxyJump option:

scp -o ProxyJump=myjumphost <source> <dest>
scp -o ProxyJump=myjumphost devdungeon.com:file.txt .

You can reference aliases in the ProxyJump settings that are defined in the SSH config file, or specify it literally.

Conclusion

You should now have a solid grasp on how to configure your SSH client for extra convenience.