What it does
ssh (Secure Shell client) connects to the SSH server (sshd) of another system. Everything is encrypted: passwords, commands, output and files. You log in with a password or — better — with a key.
The first time you connect, ssh shows the server's host key fingerprint. When you accept it, it is stored in ~/.ssh/known_hosts; next time ssh checks that the server still has the same key, to stop man-in-the-middle attacks.
Your own settings per host (user, port, key, jump host) go in ~/.ssh/config; system-wide client settings in /etc/ssh/ssh_config and /etc/ssh/ssh_config.d/.
How to write it
ssh [OPTIONS] [USER@]HOST [COMMAND [ARGUMENTS]]
ssh [OPTIONS] ssh://[USER@]HOST[:PORT]Options 43
Most used
Connection
Commands and terminal
Forwarding and tunnels
Connection sharing and others
Nothing matches this filter.
Examples 14
$ run as a normal user# run as root (or with sudo)
1Log in to a server
ssh student@server2Type
exit(or Ctrl+D) to leave.2Run one command on a remote server
ssh server2 'df -h / && uptime'SSH runs it and ends. Quote the command so your local shell does not change it.
3Use another port
ssh -p 2222 admin@bastion.example.comMany teams move SSH away from port 22 to reduce noise in the logs.
4Use a specific key
ssh -i ~/.ssh/deploy_ed25519 deploy@app01Good when you have one key per project or environment.
5Jump through a bastion host
ssh -J admin@bastion.example.com admin@10.10.0.15Reach a server in a private network in one step. The bastion only forwards the connection.
6Debug a login problem
ssh -v student@server2Shows which keys are tried and why the server refuses them.
7Open a local tunnel to a database
ssh -N -L 5433:db.internal:5432 admin@bastion.example.comThen connect your tool to localhost:5433. The database port is never open to the internet.
8Make a SOCKS proxy for your browser
ssh -N -D 1080 admin@bastion.example.comSet the browser to SOCKS5 localhost:1080 to reach internal web tools.
9Run an interactive command remotely
ssh -t server2 'sudo journalctl -f'-tgives a terminal, so sudo can ask for a password and you can stop with Ctrl+C.10Accept a new host key automatically, but never a changed one
ssh -o StrictHostKeyChecking=accept-new ansible@web03 trueGood for first contact in automation. A changed key still stops the connection.
11Start a graphical program from the server
ssh -X student@server1 gnome-system-monitorThe window opens on your desktop.
12Copy a file through a pipe
cat id_ed25519.pub | ssh server2 'cat >> ~/.ssh/authorized_keys'What ssh-copy-id does for you (ssh-copy-id also fixes permissions).
13Check the final settings for a host
ssh -G server2 | grep -Ei '^(user|port|identityfile|proxyjump) 'Shows what ssh will really use after reading all config files.
14Close a frozen session
ssh student@server2 (press Enter, then type ~.)The escape sequence ends the session when the network is gone.
Nothing matches this filter.
Watch out
- A "host key changed" warning can be an attack. Verify with the server owner before
ssh-keygen -R host. - Agent forwarding (
-A) lets the remote root use your keys while you are logged in. Use-Jinstead where you can. - Keep private keys private: permissions 600, a passphrase on keys for people.
Files
~/.ssh/config- Your settings per host (Host, HostName, User, Port, IdentityFile, ProxyJump).
~/.ssh/known_hosts- The host keys of servers you trust.
~/.ssh/id_ed25519, ~/.ssh/id_rsa- Your private keys (default names).
/etc/ssh/ssh_config, /etc/ssh/ssh_config.d/- Client settings for all users.
Tips to remember
ssh user@host 'command'= remote command.-pport,-ikey,-Jjump,-Ltunnel,-vdebug.- Put repeated options in
~/.ssh/configand type onlyssh name.