Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Linux ssh command

Reference for the Linux ssh command: log in to remote hosts, run remote commands, use keys, config files, jump hosts, and port forwarding, with common mistakes.

ssh opens an encrypted connection to another machine and gives you a shell there, or runs a single command and returns its output. The same connection can also carry forwarded ports, file transfers and agent keys. It replaced telnet and rsh because nothing travels in clear text and the server proves its identity with a host key.

Terminal
ssh user@host
ssh -p 2222 user@host 'uptime'
ssh -J jump.example.com user@internal

Try it

The sandbox has no network and no user database entry for the running account, so ssh itself fails here and the real commands are read-only. The runnable example works on the thing you edit most: ~/.ssh/config. It lists the host aliases in a sample file.

Bash Read host aliases from an ssh config

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Read-only: running one command remotely and checking the exit status.

CONSOLE
$ ssh web 'systemctl is-active nginx'
active
$ echo $?
0

Syntax

Terminal
ssh [OPTIONS] [USER@]HOST [COMMAND]
Option Description
-p PORT Server port (default 22).
-i FILE Private key to use.
-l USER Login name (same as USER@).
-J HOST Connect through a jump host.
-L LPORT:HOST:PORT Forward a local port to a remote destination.
-R RPORT:HOST:PORT Forward a remote port back to your side.
-D PORT Local SOCKS proxy.
-N No remote command; useful with forwarding.
-f Go to the background after authentication.
-t Force a terminal, needed for interactive programs run remotely.
-A Forward your agent (use only on trusted hosts).
-o KEY=VALUE Set any config option from the command line.
-v, -vv, -vvv Debug output, increasingly detailed.
-q Quiet.
-F FILE Use another config file.
-G Print the effective configuration for a host and exit.

Returns: the exit status of the remote command, or 255 if ssh itself failed (connection refused, authentication failure, host key problem).

Usage notes

  • Config file first. Put hosts in ~/.ssh/config (Host, HostName, User, Port, IdentityFile, ProxyJump). Then ssh web replaces a long command line. Keep the file at mode 600.
  • Host keys. The first connection asks you to accept the server's fingerprint and stores it in ~/.ssh/known_hosts. A later "REMOTE HOST IDENTIFICATION HAS CHANGED" warning means the key changed, whether after a reinstall or an attack: verify before removing the old entry with ssh-keygen -R host.
  • Keys beat passwords. Create a key with ssh-keygen, install the public half in the server's ~/.ssh/authorized_keys, and disable password login there.
  • Permissions matter. ~/.ssh should be 700, private keys 600. The client ignores keys that others can read, and the server ignores authorized_keys in a group-writable directory.
  • Quoting remote commands. The local shell expands first. ssh host "echo $HOME" prints your local home; use single quotes to expand on the remote side.
  • Commands inside loops swallow stdin. In while read loops, add -n (or < /dev/null) so ssh does not eat the rest of the input.
  • Long sessions: ServerAliveInterval 30 keeps idle connections open through NAT devices; ControlMaster auto with ControlPersist 10m reuses one connection for many commands.
  • Debugging: ssh -v host shows which keys are offered and where authentication fails; ssh -G host shows what the config resolves to.