SSH (Secure Shell) is a cryptographic network protocol used for secure communication, remote command-line login, remote command execution, and other secure network services between two networked computers. It was developed in 1995 as a secure replacement for Telnet, rlogin, and other insecure protocols. SSH encrypts all traffic, including passwords, to prevent eavesdropping, connection hijacking, and other attacks.
Pros: Simple to set up
Cons: Vulnerable to brute-force attacks
2. Public Key Authentication (Recommended)
Pros:
More secure (resistant to brute force)
Enables passwordless login
Supports key passphrases for extra security
Common SSH Commands
Basic Connection
Command
Description
ssh user@host
Connect to host as user
ssh -p 2222 user@host
Connect to non-standard port
ssh -i ~/.ssh/key.pem user@host
Use specific private key
File Transfer
Command
Description
scp file.txt user@host:/path
Upload file via SCP
scp user@host:/path/file.txt .
Download file via SCP
sftp user@host
Interactive SFTP session
Port Forwarding
Command
Description
ssh -L 8080:localhost:80 user@host
Local port forwarding
ssh -R 9000:localhost:3000 user@host
Remote port forwarding
ssh -D 1080 user@host
SOCKS proxy tunneling
SSH can create a dynamic encrypted SOCKS proxy tunnel, allowing you to securely route traffic from your local machine through a remote server.
SSH Server Configuration (sshd)
Configuration file: /etc/ssh/sshd_config
Security Best Practices
Restart SSH Service
SSH Hardening Guide
Step 1: Key-Based Authentication Only
Generate keys on client:
Copy public key to server:
Disable passwords in /etc/ssh/sshd_config:
Step 2: Firewall Configuration
Step 3: Fail2Ban Setup
Install and configure Fail2Ban to block brute-force attempts:
Edit /etc/fail2ban/jail.local:
SSH Troubleshooting
Common Issues
Error
Solution
Permission denied (publickey)
Verify authorized_keys permissions (600)
Host key verification failed
Remove offending key from known_hosts
Connection refused
Check firewall/SSH daemon status
Too many authentication failures
Use -o IdentitiesOnly=yes with -i
Debug Mode
SSH Security Scanning with Nmap
1. Basic SSH Detection
2. SSH Version & Algorithms
3. Vulnerability Checks
4. Full SSH Audit
Advanced SSH Features
1. SSH Config File (~/.ssh/config)
2. SSH Agent Forwarding
3. Multiplexing (Faster Connections)
SSH Best Practices
Always use key authentication - Disable password logins
Use strong algorithms - Prefer ed25519 over RSA
Restrict access - Use AllowUsers/AllowGroups
Update regularly - Patch against vulnerabilities
Monitor logs - Watch for brute force attempts
Use bastion hosts - For critical infrastructure access
A bastion host (also called a jump server or jump host) is a specially configured server that acts as a secure gateway between untrusted networks (like the internet) and trusted internal networks. It's the only server exposed to the internet in a properly secured architecture.
Disable unused features - X11, port forwarding if not needed