The Command
ssh user@your_server_ip
Replace user with your account on the server and your_server_ip with its IP address or host name. Run the command in a terminal on your own computer, not on the server. Most current desktop systems ship the OpenSSH client, so ssh is usually already there; type ssh -V to check its version. If you prefer a graphical program, see how to use PuTTY to SSH into a server.
Connect on Another Port
SSH listens on port 22 unless someone changed it. For another port, add -p (lowercase, unlike scp):
ssh -p 2222 user@your_server_ip
How to move the server to a new port is covered in how to change the SSH port.
The First Connection
On the first connection ssh says that the authenticity of the host cannot be established, prints the server's key fingerprint, and asks whether you want to continue. This is ssh asking you to confirm that you are talking to the right machine.
The careful way: open the server console in your provider's control panel and run
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
If the prompt names another key type, such as ECDSA, use that type's file instead. If the fingerprint matches the one in your terminal, type yes. ssh stores the key in ~/.ssh/known_hosts and checks it on every later connection. If it ever changes, ssh stops with a warning; see host key verification failed.
Log In with a Password or a Key
With password login, ssh asks for the password. Nothing appears on the screen while you type; that is normal. Press Enter when done.
With an SSH key, ssh tries your default key files, such as ~/.ssh/id_ed25519, and any keys loaded in an agent. To use a particular key, name it with -i:
ssh -i ~/.ssh/id_ed25519 user@your_server_ip
Keys are safer than passwords and save typing. Create one with ssh-keygen, then put it on the server with ssh-copy-id.
Run One Command and Return
Put the command after the address. ssh runs it, prints the output, and disconnects:
ssh user@your_server_ip 'df -h /'
Commands that need a terminal, such as top or a sudo password prompt, need -t:
ssh -t user@your_server_ip 'sudo systemctl status nginx'
Useful Options
| Option | What it does |
|---|---|
-p 2222 | Connects to another port. |
-i key | Uses a specific private key. |
-v | Prints debug output; -vv prints more. |
-t | Forces a terminal for one command. |
-L | Forwards a local port. |
Log Out
Type exit or press Ctrl+D. If a session hangs because the network dropped, press Enter, then type ~. (tilde, then a dot) to close it from your side. Work that must keep running after you disconnect belongs in a terminal multiplexer; see the tmux cheat sheet.
When It Does Not Connect
- Connection refused: nothing answers on that port. See SSH connection refused.
- Connection timed out: packets get no answer at all. See SSH connection timed out.
- Permission denied (publickey): the server rejected your key or user. See the publickey error guide.
To stop typing the user, address, and port each time, give the server a short name in the SSH config file.