| name | ssh-essentials |
| description | Essential SSH commands for secure remote access, key management, tunneling, and file transfers. Use when the user needs help with SSH connections, key generation, port forwarding, file transfers (scp/sftp/rsync), or SSH configuration. NOT for: general networking questions, VPN setup, or when SSH is not involved. |
SSH Essentials
Secure Shell (SSH) for remote access and secure file transfers.
Basic Connection
Connecting
ssh user@hostname
ssh user@hostname -p 2222
ssh -v user@hostname
ssh -i ~/.ssh/id_rsa user@hostname
ssh user@hostname 'ls -la'
ssh user@hostname 'uptime && df -h'
Interactive use
ssh -A user@hostname
ssh -X user@hostname
ssh -Y user@hostname
SSH Keys
Generating keys
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-keygen -t ed25519 -f ~/.ssh/id_myserver
ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_deploy
Managing keys
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname
cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> ~/.ssh/authorized_keys'
Check key fingerprint
ssh-keygen -lf ~/.ssh/id_rsa.pub
Change key passphrase
ssh-keygen -p -f ~/.ssh/id_rsa
### SSH agent
```bash
# Start ssh-agent
eval $(ssh-agent)
# Add key to agent
ssh-add ~/.ssh/id_rsa
# List keys in agent
ssh-add -l
# Remove key from agent
ssh-add -d ~/.ssh/id_rsa
# Remove all keys
ssh-add -D
# Set key lifetime (seconds)
ssh-add -t 3600 ~/.ssh/id_rsa
Windows 用户:Claude Code 自带 bash,以上命令直接可用。如果用原生 PowerShell,需先启用 ssh-agent 服务:Get-Service ssh-agent | Set-Service -StartupType Automatic
Port Forwarding & Tunneling
Local port forwarding
ssh -L 8080:localhost:80 user@hostname
ssh -L 8080:database.example.com:5432 user@jumphost
ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@hostname
Remote port forwarding
ssh -R 8080:localhost:3000 user@hostname
ssh -R 9000:localhost:9000 user@publicserver
Dynamic port forwarding (SOCKS proxy)
ssh -D 1080 user@hostname
Background tunnels
ssh -f -N -L 8080:localhost:80 user@hostname
ssh -o ServerAliveInterval=60 -L 8080:localhost:80 user@hostname
Configuration
SSH config file (~/.ssh/config)
# Simple host alias
Host myserver
HostName 192.168.1.100
User admin
Port 2222
# With key and options
Host production
HostName prod.example.com
User deploy
IdentityFile ~/.ssh/id_prod
ForwardAgent yes
# Jump host (bastion)
Host internal
HostName 10.0.0.5
User admin
ProxyJump bastion
Host bastion
HostName bastion.example.com
User admin
# Wildcard configuration
Host *.example.com
User admin
ForwardAgent yes
# Keep connections alive
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
Using config
ssh myserver
ssh internal
File Transfers
SCP (Secure Copy)
scp file.txt user@hostname:/path/to/destination/
scp user@hostname:/path/to/file.txt ./local/
scp -r /local/dir user@hostname:/remote/dir/
scp -P 2222 file.txt user@hostname:/path/
SFTP (Secure FTP)
sftp user@hostname
Rsync over SSH
rsync -avz /local/dir/ user@hostname:/remote/dir/
rsync -avz --progress /local/dir/ user@hostname:/remote/dir/
rsync -avz --delete /local/dir/ user@hostname:/remote/dir/
rsync -avz --exclude '*.log' --exclude 'node_modules/' \
/local/dir/ user@hostname:/remote/dir/
rsync -avz --dry-run /local/dir/ user@hostname:/remote/dir/
Security Best Practices
Hardening SSH
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
Port 2222
sudo systemctl restart sshd
sudo launchctl stop com.openssh.sshd && sudo launchctl start com.openssh.sshd
Restart-Service sshd
Troubleshooting
Debugging
ssh -vvv user@hostname
ls -la ~/.ssh/
Common issues
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys
ssh-keygen -R hostname
Advanced Operations
Jump hosts (ProxyJump)
ssh -J bastion.example.com user@internal.local
ssh -J bastion1,bastion2 user@final-destination
Multiplexing
ssh -M -S ~/.ssh/control-%r@%h:%p user@hostname
ssh -S ~/.ssh/control-user@hostname:22 user@hostname
Execute commands
cat local-script.sh | ssh user@hostname 'bash -s'
ssh -t user@hostname 'sudo command'