Skip to main content

SSH

SSH (Secure Shell) is an encrypted network protocol for remote login and command execution. It authenticates with key pairs or passwords and protects all traffic from eavesdropping, replacing plaintext tools such as Telnet.

Understanding SSH

What Is SSH and Why Is It Used?

SSH is the secure front door to Linux servers. Telnet sent passwords and commands in plain text, so anyone sniffing the network could read them. SSH encrypts the whole session, and it checks the server's identity with host keys, which helps defend against man-in-the-middle attacks.

How an SSH Connection Is Established

The client connects to the server's SSH daemon (sshd, port 22 by default), both sides agree on a session key through a key exchange, the user authenticates, and an encrypted session starts.

sequenceDiagram participant C as Laptop participant S as sshd on server C->>S: TCP connect on port 22 C->>S: Key exchange S-->>C: Encrypted channel ready C->>S: Authenticate with key S-->>C: Shell as your user

Practical SSH Commands

Bash
ssh rahul@10.0.1.50
ssh -p 2222 rahul@10.0.1.50
ssh -i ~/.ssh/id_ed25519 rahul@10.0.1.50
ssh -v rahul@10.0.1.50 # debug, use -vvv for more
ssh rahul@10.0.1.50 'df -h' # run one command
scp app.log rahul@10.0.1.50:/tmp/
rsync -avz ./deploy/ rahul@10.0.1.50:/opt/app/
ssh -L 5433:localhost:5432 rahul@10.0.1.50 # local port forward
ssh -J bastion@52.66.1.100 rahul@10.0.1.50 # jump host

Aliases in ~/.ssh/config save typing:

TEXT
Host mumbai-prod
HostName 10.0.1.50
User rahul
IdentityFile ~/.ssh/id_ed25519

Troubleshooting SSH

Symptom Command What to Check
Connection refused ssh -v user@host sshd running, port, firewall
Permission denied ssh -v user@host Key in authorized_keys, perms
Host key changed ssh-keygen -R host Stale known_hosts entry
Slow login ssh -o ConnectTimeout=5 DNS lookup delay
Security

Disable password login on production servers with PasswordAuthentication no in /etc/ssh/sshd_config, reload sshd, and use keys. Consider restricting port 22 to known source addresses.

Common Mistake

Blindly deleting a changed host key warning. It can mean a rebuilt server, but it can also mean an interception attempt, so verify before you remove the entry.

Tip

Use ~/.ssh/config host aliases so ssh mumbai-prod picks the right user, port, and key.

Frequently Asked Questions

What made SSH replace Telnet?

Telnet sends everything, including passwords, as plaintext. SSH encrypts the session and verifies the server identity, closing both the eavesdropping and the impersonation gap.

Why disable password authentication on SSH servers?

Internet-facing SSH is constantly probed by password-guessing bots. Key-only login removes that attack path, especially when combined with fail2ban or a restricted source range.

What does the known_hosts file do?

It stores the host keys of servers you have connected to. If a server's key later changes, SSH warns you, which helps detect man-in-the-middle attacks.

Found a mistake, an outdated step or a broken link?