Home·Terminal commands

ssh

Networking

Usage

ssh [-p <port>] <user>@<host> [command]

Opens an encrypted shell on another machine; ssh flatly refuses a private key that others can read, so `chmod 600` it, and the port is `-p` here but `-P` in scp and sftp.

Common flags

FlagMeaning
-p 2222Port. Note that scp and sftp spell the same thing -P.
-i ~/.ssh/id_ed25519Use a specific private key.
-L 8080:localhost:80Local forward: your port 8080 reaches the remote side.
-R 9000:localhost:3000Remote forward: their port 9000 reaches your machine.
-D 1080A local SOCKS proxy tunnelled through the host.
-J <bastion>Jump through a bastion host in one command.
-N -fNo remote command, go to the background. The pair used for tunnels.
-v / -vvvShow the handshake, which is how you find out why a key was refused.
-o <Option>=<value>Any ssh_config setting for this one connection.

Examples

ssh -p 2222 deploy@example.com

Log in on a non-standard port.

ssh -N -L 5432:localhost:5432 deploy@db.example.com

Reach the remote database as if it were local.

ssh -vvv git@github.com

See exactly which keys were offered and rejected.

When a connection fails, these narrow down how far it got — name not resolving, no route, or a blocked port.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does ssh do?

Opens an encrypted shell on another machine; ssh flatly refuses a private key that others can read, so `chmod 600` it, and the port is `-p` here but `-P` in scp and sftp.

Q. How do I type it?

ssh [-p <port>] <user>@<host> [command] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

9 are listed here; the full set is in man ssh. This command sits under Networking.

Related commands

man page: man ssh