SSH Fundamentals: Secure Remote Access and Administration
Learn SSH for secure remote access including key-based authentication, configuration, tunneling, port forwarding, and security hardening.
Securing Remote Access with SSH
Remote server administration is a daily task for every cybersecurity professional. SSH is the tool that makes it possible — and secure. Whether you are connecting to a compromised host during an incident, managing a firewall remotely, or tunneling traffic through an encrypted channel, SSH is your gateway. This guide walks through SSH setup, key-based authentication, and practical usage patterns.
Prerequisites
Basic Linux command line knowledge. Understanding of IP addresses and ports.
How SSH Works
The SSH Handshake
Basic SSH Usage
ssh user@192.168.1.100 # Basic connection
ssh -p 2222 user@example.com # Non-default port
ssh user@host "ls -la /var/log" # Run single command
ssh -i ~/.ssh/id_rsa user@host # Use specific identity file
SSH Config File (`~/.ssh/config`)
Host webserver
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/webserver_key
Now connect with: ssh webserver
Key-Based Authentication
Generating Keys
ssh-keygen -t ed25519 -C "email@example.com" # Recommended
ssh-keygen -t rsa -b 4096 -C "email@example.com" # Fallback
Copying Public Key
ssh-copy-id user@192.168.1.100
# Manual:
cat ~/.ssh/id_ed25519.pub | ssh user@host "cat >> ~/.ssh/authorized_keys"
Key Types
| Type | Security | Speed | Recommendation |
|------|----------|-------|----------------|
| Ed25519 | Excellent | Fastest | Default choice |
| RSA 4096 | Excellent | Slower | Good fallback |
| DSA | Weak | Moderate | Avoid |
SSH File Transfer
# SCP
scp file.txt user@host:/path/ # Copy to server
scp -r dir/ user@host:/path/ # Copy directory
scp user@host:/var/log/syslog ./ # Copy from server
scp -P 2222 file.txt user@host:/tmp/ # Custom port
# SFTP (interactive)
sftp user@host
# Commands: ls, lls, get, put, rm, mkdir, exit
SSH Tunneling
Local Port Forwarding
ssh -L 8080:internal-server:80 user@gateway
# Access http://localhost:8080 to reach internal web server
Remote Port Forwarding
ssh -R 9000:localhost:3000 user@public-server
# Exposes local port 3000 on remote server's port 9000
Dynamic Port Forwarding (SOCKS Proxy)
ssh -D 1080 user@proxy-server
# Configure browser to use SOCKS5 at localhost:1080
Security Hardening
In /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
Port 2222
AllowUsers admin john
ClientAliveInterval 300
MaxAuthTries 3
LogLevel VERBOSE
sudo systemctl restart sshd
Real-World Examples
Jump Host: ssh -J bastion.example.com internal-server.local
Reverse Tunnel: Device behind NAT: ssh -R 2222:localhost:22 user@public-server then ssh -p 2222 localhost from public server.
SSH as VPN: Dynamic port forwarding encrypts browsing like a VPN.
Common Mistakes
Exposing SSH on default port (22 gets constant probes). Leaving password auth enabled. Sharing private keys. Not using ssh-agent for passphrases.
Best Practices
Use Ed25519 keys. Use an SSH agent. Implement fail2ban. Keep SSH updated. Use jump hosts for private networks. Disable SSH protocol 1.
Related Tools
Mosh — Mobile shell for high latency. autossh — Auto-restart tunnels. sshfs — Mount remote directories. fail2ban — Block brute force. ssh-audit — Test SSH config.
Related Articles
Summary
SSH provides encrypted remote access, file transfer (SCP/SFTP), and tunneling (local/remote/dynamic port forwarding). Best practices include Ed25519 keys, disabling password auth, changing the default port, and using jump hosts for private network access.