Connecting to a Remote Host
Chapter 2 — Connecting to a Remote Host
Chapter 1 explained the theory — encryption, key pairs, host verification. This
chapter is where you actually type things. The ssh command is
surprisingly flexible: it can open an interactive shell, run a single command and
exit, forward a port, or proxy through an intermediate host. This chapter covers
the core syntax, the most useful flags, running remote commands without opening a
full shell, and diagnosing the most common connection errors.
The Basic Command
The minimum you need is a username and a hostname or IP address:
| ssh | The SSH client command |
| -p 2222 | Optional — connect on port 2222 instead of the default port 22 |
| philip | The username to log in as on the remote machine |
| @ | Separator between user and host |
| 192.168.1.100 | The hostname or IP address of the remote machine |
user@ part entirely and just type
ssh hostname. SSH uses your current username by default.
Your First Connection — Step by Step
Notice the prompt changed — philip@debian became
philip@server. Every command you type now runs on the remote
machine. To return to your local machine, type exit or press
Ctrl+D.
Connecting by Hostname vs IP Address
You can use either an IP address or a hostname. On a local network, your router usually provides hostname resolution so you can use the machine's name directly:
raspberrypi.local) use
mDNS (multicast DNS), which works on local networks without a DNS server.
On Linux this requires avahi-daemon to be running. On Windows,
Bonjour (installed with iTunes or Apple devices) provides the same feature.
Essential SSH Flags
| Flag | What it does | Example |
|---|---|---|
| -p PORT | Connect on a non-default port (default is 22) | ssh -p 2222 philip@server |
| -i FILE | Use a specific private key file instead of the default | ssh -i ~/.ssh/id_work philip@server |
| -l USER | Specify the login username (alternative to user@host) | ssh -l philip server |
| -v | Verbose — show the connection and auth process. Use -vv or -vvv for more detail. Invaluable for debugging. | ssh -v philip@server |
| -A | Forward your SSH agent — lets you use your local keys on the remote machine to connect onwards to a third machine | ssh -A philip@jumphost |
| -X | Enable X11 forwarding — run graphical apps on the remote machine, display locally | ssh -X philip@server |
| -N | Do not execute a remote command — used with port forwarding (Chapter 5) when you only want the tunnel | ssh -N -L 8080:localhost:80 philip@server |
| -f | Go to background after connecting — used with -N for background tunnels | ssh -fN -L 8080:localhost:80 philip@server |
| -q | Quiet mode — suppress warnings and diagnostic messages. Useful in scripts. | ssh -q philip@server uptime |
| -o OPTION=VALUE | Set any config option inline without editing the config file | ssh -o StrictHostKeyChecking=no philip@server |
Running a Single Remote Command
You don't have to open an interactive shell every time. Append a command after the host and SSH will run it, print the output, and exit — no prompt, no interaction required. This is very useful in scripts.
ssh server "df -h | grep sda" sends the whole string to the remote
shell. Without quotes, the pipe runs locally and SSH never sees it.
Running a Local Script on a Remote Machine
You can pipe a local script into SSH and have the remote machine execute it — without copying the file first:
Connecting on a Non-Standard Port
Many administrators move SSH away from port 22 to reduce automated scanning
noise. If the server listens on a different port, use -p:
Connecting from Windows
Windows 10 and 11 include OpenSSH as an optional feature — the same
ssh command works in PowerShell and Command Prompt. If it's not
installed, enable it from Settings → Optional Features.
GUI SSH clients are also available if you prefer a point-and-click interface:
- PuTTY — the classic Windows SSH client. Free, lightweight, saves session profiles. Still widely used in enterprise environments.
- Kitty — a PuTTY fork with extra features (tabs, automatic reconnect, URL highlighting). Philip uses this.
- MobaXterm — SSH client + X11 server + file browser in one. Popular for Linux admin from Windows.
- Windows Terminal — Microsoft's modern terminal. Use the built-in
sshcommand inside it for a clean experience.
Debugging with Verbose Mode
When a connection fails silently or behaves unexpectedly, -v
reveals exactly what SSH is doing at each step. Add more vs for
progressively more detail:
The key lines to look for: which key file SSH is trying, whether the server accepts it, and which authentication method ultimately succeeded.
Troubleshooting Common Errors
sudo systemctl status sshd. Check the port in /etc/ssh/sshd_config. Check UFW: sudo ufw status. If using a custom port, add -p PORT to your ssh command.ping 192.168.1.100. Confirm the server is on. Confirm you're on the right network. Check DNS: nslookup hostname.ssh -v to see which keys are being offered. If using keys, verify the public key is in ~/.ssh/authorized_keys on the server with permissions 600.ssh-keygen -R hostname. Reconnect and accept the new fingerprint.hosts.deny, AllowUsers / DenyUsers in sshd_config, or fail2ban banning your IP./etc/hosts.deny and /etc/hosts.allow. Check sshd_config for AllowUsers. Check fail2ban: sudo fail2ban-client status sshd — your IP may be banned.ssh -v — if it hangs on "Connecting to..." the problem is network/firewall before the SSH daemon. Check UFW rules on the server. Check that port 22 is open: nc -zv server 22 from your machine.Keeping a Connection Alive
SSH connections that sit idle too long get dropped — by NAT routers, firewalls,
or the server's own timeout settings. Fix this by sending keepalive packets from
the client side. Either add this to your ~/.ssh/config (Chapter 4
covers this in full) or pass it inline:
Quick Reference
| Task | Command |
|---|---|
| Open a shell session | ssh philip@server |
| Connect on a custom port | ssh -p 2222 philip@server |
| Use a specific key file | ssh -i ~/.ssh/id_work philip@server |
| Run one command and exit | ssh philip@server df -h |
| Run a piped command | ssh philip@server "df -h | grep sda" |
| Debug a failed connection | ssh -v philip@server |
| Even more debug detail | ssh -vvv philip@server |
| Remove stale host key | ssh-keygen -R hostname |
| Test TCP connectivity to port 22 | nc -zv server 22 |
| End the session | exit or Ctrl+D |
| Escape sequence (if stuck) | Enter, then ~. |
~. — if your SSH session freezes
(network dropped, server hung) and Ctrl+C does nothing, press
Enter then type ~. (tilde followed by a full stop).
This tells the SSH client to drop the connection immediately, returning you to
your local prompt.