The Basic Form
ssh user@your_server_ip
ssh -p 2222 user@your_server_ip
ssh -i ~/.ssh/id_ed25519 user@your_server_ip
The first line connects on port 22. -p sets another port (lowercase here, unlike scp), and -i picks a private key. -l user followed by the address is the same as writing user@ in front of it. On the first connection ssh shows the server's key fingerprint and asks you to confirm it. exit or Ctrl+D ends the session. A step-by-step first login is in how to SSH into a server, and creating a key in how to generate SSH keys.
Run One Remote Command
ssh user@your_server_ip 'df -h /'
ssh -t user@your_server_ip 'sudo systemctl restart nginx'
ssh -t user@your_server_ip htop
Quote the command so that your local shell does not expand it. Without a terminal, sudo cannot ask for a password and full-screen programs do not draw; -t requests a terminal for them. Output comes back to your screen, so you can pipe it: ssh user@your_server_ip 'cat /etc/os-release' | grep VERSION.
Debug a Connection with -v
ssh -v user@your_server_ip
ssh -vvv user@your_server_ip
The verbose output shows which configuration files are read, which keys are offered, which login methods the server accepts, and the step where the connection stops. Read it together with SSH connection refused and Permission denied (publickey).
Through a Jump Host
ssh -J user@your_jump_host user@your_server_ip
ssh connects to the jump host first and continues from there to the target. The session is encrypted end to end, and your private key stays on your computer.
Port Forwarding
ssh -L 8080:localhost:80 user@your_server_ip
ssh -R 9000:localhost:3000 user@your_server_ip
ssh -D 1080 user@your_server_ip
ssh -f -N -L 5433:localhost:5432 user@your_server_ip
-L: port 8080 on your computer reaches port 80 on the server.-R: port 9000 on the server reaches port 3000 on your computer.-D: a SOCKS proxy on local port 1080 sends traffic out through the server.-Nand-f: open the tunnel without a remote command and move ssh to the background.
The details are in SSH port forwarding.
Options at a Glance
| Option | Does |
|---|---|
-p 2222 | Sets the port |
-i key | Uses a key file |
-l user | Sets the login name |
-v | Verbose output |
-J | Jump host |
-L, -R | Forward a port |
-D | SOCKS proxy |
-N | No remote command |
-f | Go to background |
-t | Force a terminal |
-A | Forward the agent |
-o | Set one option |
Set Options with -o
ssh -o ServerAliveInterval=30 user@your_server_ip
ssh -o StrictHostKeyChecking=accept-new user@your_server_ip
The first sends a keepalive every 30 seconds, which helps against sessions dropped by idle routers; see SSH broken pipe. The second accepts the key of a host you have not seen before without asking, but still refuses a key that has changed.
Agent Forwarding and Its Risk
-A lets the server use the keys in your local agent to log in further, for example to pull from Git. While you are connected, anyone with root on that server can use your agent in the same way. Prefer -J for reaching other servers, and forward the agent only to servers you fully trust. See ssh-agent.
Close a Hung Session
When a session stops responding, press Enter, then ~, then . (tilde, dot). ssh recognizes the escape only at the start of a line, which is why Enter comes first. Enter followed by ~? lists the other escapes.
Use a Config File Instead of Long Commands
Host web
HostName your_server_ip
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
With this block in ~/.ssh/config, ssh web is enough. See SSH config file.