How to Connect to a Server with SSH

Open a terminal session on a remote server with ssh, handle the first-connection host key prompt, use a custom port or key file, and disconnect.

You have a server's address and a login, and you want a shell on it. The ssh command is preinstalled on macOS, Linux, and Windows 10 and later.

Connect with a username and host

ssh user@example.com

Replace user with your account on the server and example.com with its hostname or IP address, such as ssh user@203.0.113.10. If your local username matches the remote one, ssh example.com is enough.

If the server listens on a port other than 22:

ssh -p 2222 user@example.com

Accept the host key on first connection

The authenticity of host 'example.com (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:Jr7v0X3Cq9b2cM8Yk4Ff1sT6qP0wLxZ5vN1aH2dE3gU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Type yes. SSH records the fingerprint in ~/.ssh/known_hosts and will not ask again for that host. If your provider published the server's fingerprint, compare it before answering.

Authenticate

If the server accepts passwords, it prompts for one; nothing is shown as you type. If it uses key authentication, SSH tries the keys in ~/.ssh automatically (id_ed25519, id_rsa, and others). To point at a specific key file:

ssh -i ~/.ssh/my-server-key user@example.com

A key file must be readable only by you, or SSH refuses to use it:

chmod 600 ~/.ssh/my-server-key

Work on the server

Once connected, the prompt changes to the remote machine's:

user@server:~$

Every command you type now runs on the server, not on your laptop.

Disconnect

exit

Closing the terminal window also ends the session, but exit (or Ctrl+D) is cleaner and confirms you are back on your own machine.

If you connect to the same server often. Create ~/.ssh/config with an entry for it:

Host myserver
    HostName example.com
    User user
    Port 2222
    IdentityFile ~/.ssh/my-server-key

After that, ssh myserver applies all of those settings, and scp and rsync recognize the alias too.

If the connection is refused or times out. The server may not be running SSH, the port may be wrong, or a firewall may be blocking it. ssh -v user@example.com prints each step of the connection so you can see where it stops.

More Coding how-tos