SSH

Secure remote server access - encrypted connections, key-based auth, tunneling for safe data transfer

TL;DR

What: Secure Shell - encrypted protocol for remote server access and file transfer.

Why: Securely connect to remote servers, transfer files, and tunnel connections.

Quick Start

Connect to a server:

ssh user@hostname
ssh [email protected]
ssh -p 2222 user@hostname    # Custom port

Generate SSH key (recommended):

ssh-keygen -t ed25519 -C "[email protected]"
# Or RSA (wider compatibility)
ssh-keygen -t rsa -b 4096 -C "[email protected]"

Copy key to server (passwordless login):

ssh-copy-id user@hostname
# Or manually
cat ~/.ssh/id_ed25519.pub | ssh user@hostname "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

SSH config for shortcuts:

# ~/.ssh/config
Host myserver
    HostName 192.168.1.100
    User admin
    Port 22
    IdentityFile ~/.ssh/id_ed25519

# Now just use: ssh myserver

Cheatsheet

CommandDescription
ssh user@hostConnect to server
ssh -i key.pem user@hostUse specific key
scp file user@host:/pathCopy file to server
scp user@host:/path fileCopy file from server
scp -r dir user@host:/pathCopy directory
ssh -L 8080:localhost:80 user@hostLocal port forward
ssh -R 8080:localhost:80 user@hostRemote port forward
ssh -D 1080 user@hostSOCKS proxy
ssh-add ~/.ssh/keyAdd key to agent

Gotchas

Permission denied (publickey)

# Check key permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

# Check server authorized_keys
chmod 600 ~/.ssh/authorized_keys

Connection refused

# Check if SSH service is running
sudo systemctl status sshd

# Start SSH service
sudo systemctl start sshd

# Check firewall
sudo ufw allow 22

Host key verification failed

# Remove old key (after verifying it's safe)
ssh-keygen -R hostname

# Or skip check (not recommended for production)
ssh -o StrictHostKeyChecking=no user@host

Keep connection alive

# ~/.ssh/config
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

Next Steps