The ssh command
The ssh command logs you in to another machine over the network…
The ssh command logs you in to another machine over the network, so the commands you type run there: ssh user@host to go, and exit to come back.
Taught in Intermediate, lesson 31; more in Advanced and Ninja
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
ssh [USER@]HOST [COMMAND] ssh -p PORT HOST ssh -L LOCALPORT:HOST:PORT [-N] [-f] SERVER
Log in to another machine. Opens a shell on another machine: the prompt changes to show where you are, commands run there, and exit brings you back. Give a command after the host to run just that and come back. The first visit asks you to confirm the machine’s key and adds it to ~/.ssh/known_hosts. You log in with a password, or better, a key: make one with ssh-keygen, copy it with ssh-copy-id, and ssh stops asking. Short names for hosts go in ~/.ssh/config (Host, HostName, User, Port). ssh -L 8080:localhost:80 web makes a tunnel: localhost:8080 here reaches port 80 as seen from web. The machines here are web, db and router. In this practice shell the network is a simulation inside your browser: the machines web, db and router and the website example.com are pretend, and nothing leaves your device.
In the practice terminal:A simulation: web, db, router and example.com are pretend machines inside your browser, and nothing leaves your device. No password is asked for or sent; the pretend servers let you in.
How to use ssh
Log in to a server
The first time, ssh shows the server’s key fingerprint and asks whether to trust it, then asks for your password on that machine. The prompt changes to show where you are. Type exit (or press Ctrl+D) to come back.
ssh webThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: Welcome to Ubuntu 24.04 LTS (practice web server web) This is a pretend machine inside your browser. Type exit to go back to sandbox. Last login: Mon Jan 12 06:39:27 2026 from 10.0.0.5
Log in as another user: ssh user@host
Without a user name, ssh uses the one you have here.
ssh learner@webThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: Welcome to Ubuntu 24.04 LTS (practice web server web) This is a pretend machine inside your browser. Type exit to go back to sandbox. Last login: Mon Jan 12 06:39:27 2026 from 10.0.0.5
Run one command and come back
Put the command after the host: it runs there and prints here.
ssh web hostnameThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: web
ssh db uptimeThe authenticity of host 'db (10.0.0.20)' can't be established. ED25519 key fingerprint is SHA256:fLxDFExDWEnysz8O/eQFLvp7rre0Hshqu8oYMO9o94O. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'db' (ED25519) to the list of known hosts. learner@db's password: 09:01:00 up 3 days, 2:49, 1 user, load average: 0.03, 0.02, 0.02
Log in with a key instead of a password
ssh-keygen makes a key pair and ssh-copy-id puts the public half on the server. After that, ssh logs you in without a password.
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N ""Generating public/private ed25519 key pair. Created directory '/home/learner/.ssh'. Your identification has been saved in /home/learner/.ssh/id_ed25519 Your public key has been saved in /home/learner/.ssh/id_ed25519.pub The key fingerprint is: SHA256:yfaGYXt9qaHEi+RfKqo19GBy7eorc906dYpaKPTlsVh learner@sandbox The key's randomart image is: +--[ED25519 256]--+ | | | | | | | . . | … and 6 more lines
ssh-copy-id web/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/learner/.ssh/id_ed25519.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys The authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: Number of key(s) added: 1 … and 3 more lines
ssh web hostnameweb
Use another port: ssh -p
Some servers listen on a port other than 22. Nothing listens on 2222 here, so the connection times out.
ssh -p 2222 webssh: connect to host web port 2222: Connection timed out
When ssh says "Connection refused"
The machine answered, but no ssh server is running on it: the router here has none. A timeout instead usually means a firewall drops the connection.
ssh routerssh: connect to host router port 22: Connection refused
Options
| -p PORT | connect to another port than 22 |
|---|---|
| -i KEYFILE | log in with this key |
| -L L:HOST:P | forward local port L to HOST:P, as seen from the server |
| -N | no shell (just the tunnel) |
| -f | go to the background once connected |
Questions about ssh
What does "The authenticity of host can’t be established" mean?
It is your first connection to that server, so ssh can’t tell yet whether it is the real one. Check the fingerprint with whoever runs the server, then answer yes. ssh keeps the key in ~/.ssh/known_hosts and warns you if it ever changes.
How do I exit ssh?
Type exit or press Ctrl+D at the remote prompt. If the connection hangs, press Enter, then ~ and a full stop to close it.
What is the difference between ssh and scp?
ssh gives you a shell on the other machine; scp copies files to or from it over the same kind of connection.
How do I make a short name for a server?
Add a Host block to ~/.ssh/config with HostName, User, Port and IdentityFile lines. Then ssh myserver uses them all.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.