Connecting with ssh

SSH · Essentials · 8 min

Reaching servers, cloud VMs and containers' hosts over SSH is a basic skill for any developer or operator.

SSH is a protocol for secure remote access. A client (ssh) connects to a server (sshd), normally on TCP port 22. Everything is encrypted, and both sides authenticate: the server proves its identity with a host key, and you prove yours with a password or, better, a key pair.

The basic command:

  • ssh alice@server.example.com logs in as alice. Without user@, your local username is used.
  • -p 2222 connects to a non-standard port.
  • -i ~/.ssh/work_key picks a specific private key.
  • -v (up to -vvv) prints debugging output when a connection fails.

Once connected you get a normal shell on the remote machine. exit or Ctrl-d closes it. If the connection hangs (say, after a network change), the escape sequence Enter ~ . (Enter, then tilde, then dot) kills the session from the client side; ~? lists other escapes.

On the very first connection to a host, ssh shows the server's key fingerprint and asks whether to trust it. That question matters; the next lesson explains why.

OpenSSH ships with Linux, macOS and Windows 10/11, so ssh is usually already there. To accept connections, a machine needs the server package (openssh-server on Debian/Ubuntu) running.

Example

ssh alice@203.0.113.10             # log in as alice
ssh -p 2222 deploy@build.example.com
ssh -i ~/.ssh/work_ed25519 alice@server
ssh -v alice@server                # debug a failing connection
# inside the session
whoami && hostname                 # alice / server
exit                               # or Ctrl-d
# frozen session? press:  Enter  ~  .

Remember

  • ssh user@host, -p for port, -i for a key, -v to debug
  • exit or Ctrl-d closes; Enter ~ . kills a frozen session
  • Both server (host key) and user are authenticated

Watch out for

  • Pressing Ctrl-C on a frozen session; it is sent to the remote side, use Enter ~ . instead.

Check yourself

  1. A session froze after your laptop changed networks. How do you terminate it from the client?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: man.openbsd.org/ssh