SSH (Secure Shell) – Secure Remote Access
1. What Is SSH?
SSH (Secure Shell) is a cryptographic network protocol that provides secure remote login, command execution, file transfer, and port forwarding over an unsecured network. Defined in RFC 4251–4254, SSH replaced insecure legacy protocols — primarily Telnet (port 23) — and operates at OSI Layer 7 (Application Layer) over TCP port 22.
SSH delivers three fundamental security properties for every session:
- Confidentiality: All data (including passwords and commands) is encrypted — an attacker capturing packets sees only ciphertext
- Integrity: Message Authentication Codes (MACs) detect any tampering with data in transit
- Authentication: Both the server and client are verified before any data is exchanged
SSH Client SSH Server
(PuTTY, OpenSSH, SecureCRT) (Cisco IOS, Linux sshd)
| |
|-- TCP SYN -> port 22 ------------------>| Step 1: TCP connection
|<-- TCP SYN-ACK -------------------------|
| |
|<--- SSH Version & Algorithm Exchange ---->| Step 2: Protocol negotiation
| |
|<--- Key Exchange (Diffie-Hellman) ------->| Step 3: Session key established
| |
|-- Authentication (password/key) ------->| Step 4: Client authenticates
| |
|====== Encrypted Session Begins =========| Step 5: All traffic encrypted
Related pages: Telnet | FTP (File Transfer Protocol) | Firewalls | ACL Overview | Standard & Extended ACLs | Common Network Ports | OSI Model | SSH & Telnet Security | Step-by-Step: SSH Configuration
2. SSH Versions
| Version | Status | Security | Action |
|---|---|---|---|
| SSH-1 | Obsolete — do not use | Multiple known vulnerabilities including man-in-the-middle attacks and session hijacking | Disable explicitly: ip ssh version 2 on Cisco; Protocol 2 in sshd_config on Linux |
| SSH-2 | Current standard — always use | Modern, secure architecture with separate transport, authentication, and connection layers; supports AES, ECDSA, Ed25519 | Enable and enforce in all production environments |
3. SSH Components
| Component | Description | Examples |
|---|---|---|
| SSH Client | Initiates the SSH connection to a remote host | ssh (OpenSSH), PuTTY, SecureCRT, MobaXterm, Termius |
| SSH Server | Listens on TCP port 22 and accepts incoming SSH connections | OpenSSH sshd (Linux/Unix), Cisco IOS SSH server, Windows OpenSSH |
| Host Keys | The server's public/private key pair used to prove the server's identity to clients | RSA, ECDSA, or Ed25519 key pairs generated on the server |
| User Keys | Client's public/private key pair used for passwordless authentication | ~/.ssh/id_rsa, ~/.ssh/id_ed25519 (generated with ssh-keygen) |
4. SSH Authentication Methods
| Method | How It Works | Security | When to Use |
|---|---|---|---|
| Password | User enters username and password; server validates against its user database | Medium — password is encrypted in transit but vulnerable to brute-force if weak | Simple setups; always combine with rate limiting and brute-force protection |
| Public Key | Client proves possession of the private key corresponding to the public key installed on the server in ~/.ssh/authorized_keys |
High — private key never leaves the client; immune to password brute-force | Preferred method for all production servers and automation |
| Certificate | SSH CA signs user/host certificates; server trusts the CA rather than individual keys | Very High — scales to thousands of servers without deploying individual public keys | Large enterprise deployments with centralised SSH management — see AAA Overview |
5. SSH Key Management
Step 1 — Generate a Key Pair (Linux/macOS)
ssh-keygen -t ed25519 -C "[email protected]" # Modern, recommended
ssh-keygen -t rsa -b 4096 # RSA 4096-bit (also acceptable)
This creates two files:
~/.ssh/id_ed25519— Private key — never share or copy to other systems~/.ssh/id_ed25519.pub— Public key — install this on remote servers
Step 2 — Deploy Public Key to a Remote Server
ssh-copy-id [email protected]
This appends your public key to ~/.ssh/authorized_keys on the remote server. After this, you can log in without a password.
Step 3 — Protect the Private Key with a Passphrase
Always set a passphrase when generating keys. The passphrase encrypts the private key file on disk — even if an attacker steals the file, they cannot use it without the passphrase.
Using SSH Agent for Convenience
eval $(ssh-agent) # Start the agent
ssh-add ~/.ssh/id_ed25519 # Load your key (enter passphrase once)
The agent holds the decrypted key in memory for the session, so you only enter the passphrase once per login session.
6. Configuring SSH on Cisco IOS
SSH is the required secure management protocol for all Cisco devices. Telnet must be disabled after SSH is configured. See also Console & VTY Line Configuration.
Full SSH Configuration Example
! Step 1: Set a hostname (required for RSA key generation)
Router(config)# hostname Router1
! Step 2: Set a domain name (required for RSA key generation)
Router1(config)# ip domain-name example.com
! Step 3: Generate RSA key pair (minimum 1024 bits; 2048 recommended)
Router1(config)# crypto key generate rsa modulus 2048
! Step 4: Enforce SSH version 2 only (disables insecure SSH-1)
Router1(config)# ip ssh version 2
! Step 5: Set SSH timeout and authentication retry limits
Router1(config)# ip ssh time-out 60
Router1(config)# ip ssh authentication-retries 3
! Step 6: Create a local user account
Router1(config)# username admin privilege 15 secret StrongP@ssword2025
! Step 7: Configure VTY lines to use SSH only
Router1(config)# line vty 0 4
Router1(config-line)# login local
Router1(config-line)# transport input ssh
! Step 8: Verify
Router1# show ip ssh
Router1# show ssh
Restricting SSH Access with an ACL
Router1(config)# access-list 10 permit 192.168.1.0 0.0.0.255
Router1(config)# line vty 0 4
Router1(config-line)# access-class 10 in
Only hosts in 192.168.1.0/24 can reach the VTY lines via SSH. See Applying ACLs and Wildcard Masks for more detail.
7. SSH Client Usage
Basic Connection
ssh [email protected] # Connect as user "admin"
ssh -p 2222 [email protected] # Connect to non-default port 2222
ssh -v [email protected] # Verbose mode for troubleshooting
Using an SSH Config File for Shortcuts
Create ~/.ssh/config to define host aliases:
Host router1
HostName 192.168.1.1
User admin
Port 22
IdentityFile ~/.ssh/id_ed25519
Then connect simply with: ssh router1
8. SSH Tunneling and Port Forwarding
SSH can forward TCP ports through an encrypted tunnel — useful for securing unencrypted protocols or bypassing firewall restrictions. For full network-layer VPN tunneling, see IPsec VPN and GRE Tunnels.
| Type | Command Example | Use Case |
|---|---|---|
| Local Forwarding | ssh -L 8080:localhost:80 admin@server |
Access a web server on the remote network via localhost:8080 on your machine |
| Remote Forwarding | ssh -R 8080:localhost:80 admin@server |
Allow remote users to access a service on your local machine |
| Dynamic Forwarding | ssh -D 1080 admin@server |
Create a SOCKS5 proxy — route all browser traffic through the SSH tunnel |
9. Security Best Practices
| Practice | Implementation | Benefit |
|---|---|---|
| Always use SSH-2 | ip ssh version 2 (Cisco) | Protocol 2 (sshd_config) | Eliminates SSH-1 vulnerabilities |
| Disable password auth, use keys | PasswordAuthentication no in sshd_config | Eliminates brute-force password attacks entirely |
| Disable root login | PermitRootLogin no in sshd_config | Prevents direct root compromise |
| Restrict access with ACL/firewall | Allow TCP 22 only from management networks — see ACL Overview and Firewalls | Reduces attack surface dramatically |
| Change default port | Port 2222 in sshd_config | Reduces automated scanning noise (security through obscurity — not a primary control) |
| Set session timeouts | ip ssh time-out 60 | ClientAliveInterval 300 | Terminates abandoned sessions |
| Use strong ciphers | Configure AES-256-GCM, ChaCha20; disable 3DES, RC4 | Ensures only modern encryption algorithms are negotiated |
10. SSH vs Telnet — Summary Comparison
| Feature | SSH (Secure Shell) | Telnet |
|---|---|---|
| Encryption | Yes — all session data is encrypted | No — all data including passwords in plaintext |
| Port | TCP 22 (default) | TCP 23 |
| Authentication | Password, public key, certificate | Password only (in plaintext) |
| Integrity | Yes — MACs detect tampering | No |
| Use for Management | Yes — required for all production devices | No — testing and legacy troubleshooting only |
| File Transfer | Yes — SCP and SFTP over SSH | No |
| Port Forwarding | Yes — local, remote, dynamic | No |
| RFC / Standard | RFC 4251–4254 (SSH-2) | RFC 854 |
11. Troubleshooting SSH Connections
| Symptom | Likely Cause | Fix |
|---|---|---|
| Connection refused (port 22) | SSH server not running, firewall blocking TCP 22, wrong IP/hostname | Verify show ip ssh (Cisco) or systemctl status sshd (Linux); check firewall rules and ACLs |
| Authentication failed (password) | Wrong password, account locked, SSH server rejecting passwords | Verify credentials, check PasswordAuthentication yes in sshd_config |
| Permission denied (publickey) | Public key not in authorized_keys, wrong file permissions, wrong key | Run ssh-copy-id; verify ~/.ssh/authorized_keys permissions (600) and ~/.ssh (700) |
| Host key verification failed | Server's host key changed (or MITM attack) | If legitimate: remove old entry from ~/.ssh/known_hosts; if suspicious: investigate before proceeding |
| SSH hangs after connection | Routing problem, firewall allowing SYN but dropping established | Check return path routing; use ssh -v for detailed negotiation trace |
Debugging SSH with Verbose Mode
ssh -v [email protected] # Basic verbose — shows handshake steps
ssh -vv [email protected] # More detail
ssh -vvv [email protected] # Maximum verbosity — shows every packet
12. Advanced SSH Features
- Multiplexing: Reuse a single SSH connection for multiple sessions —
ControlMaster autoin ssh config significantly speeds up repeated connections - Agent forwarding: Use your local SSH keys on a remote server to connect to further hosts —
ssh -A admin@jump-host - Jump hosts (ProxyJump): Reach hosts behind a bastion server —
ssh -J [email protected] admin@internal-host - SSHFS: Mount a remote filesystem securely over SSH —
sshfs admin@server:/var/www /mnt/remote