Connect

Introduction

OpenSSH's ssh opens encrypted remote shells and tunnels. This sheet covers logins, keys, config, forwarding, and troubleshooting.

Log in

ssh user@host
ssh -p 2222 user@host
ssh ssh://user@host:2222
ssh host  # uses a config alias

-l sets the login name and -i selects a key.

See: ssh(1)

Run a command

ssh host uptime
ssh host 'ls -l /var/log'
ssh -t host htop
ssh host -- -l

Arguments after the destination form one command sent to the remote shell. -t forces a TTY; -- ends option parsing for commands starting with -.

See: ssh(1)

Keys

Create a key

ssh-keygen -t ed25519 -C "me@example.com"

This writes ~/.ssh/id_ed25519 and its .pub file. Set a passphrase when prompted.

See: ssh-keygen(1)

Install a key

ssh-copy-id -i ~/.ssh/id_ed25519.pub \
  user@host

The public key is appended to the remote authorized_keys.

See: ssh-copy-id(1)

Inspect a key

ssh-keygen -lf ~/.ssh/id_ed25519.pub

Prints the bit length, fingerprint, comment, and algorithm.

See: ssh-keygen(1)

Use an agent

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l

The agent holds decrypted keys so passphrases are entered once.

See: ssh-agent(1)

Config

Host aliases

Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

ssh github now uses this user, host, and key.

See: ssh_config(5)

Wildcards

Host prod.internal
    HostName 10.0.0.5

Host *.internal
    User deploy
    ProxyJump bastion

For each option, the first value wins, so list specific hosts before wildcards.

See: ssh_config(5)

Effective configuration

$ ssh -G -F ~/.ssh/config github | grep -E '^(user|hostname|port) '
user git
hostname github.com
port 22

-G resolves options and exits; -F checks the given file.

See: ssh(1)

Forwarding

Local and remote ports

Local

ssh -N -L 8080:localhost:80 user@host

Remote

ssh -N -R 9000:localhost:5432 user@host

-L listens locally and reaches port 80 from host; -R listens on host and reaches the client. -N holds the connection open without a shell.

See: ssh(1)

SOCKS proxy

ssh -N -D 1080 user@host

The client listens on 1080 and forwards through host.

See: ssh(1)

Jump host

ssh -J bastion internal

-J connects through bastion; ProxyJump does the same in config.

See: ssh(1)

Troubleshoot

Verbose output

ssh -vvv user@host

Three -v flags give the most detail about config and authentication.

See: ssh(1)

Changed host key

ssh-keygen -R host

This removes stale keys from known_hosts. Verify the new fingerprint before reconnecting.

See: ssh-keygen(1)

Also see

0 Comments for this cheatsheet. Write yours!