Secure Shell (SSH) protocol for secure remote login and command execution.
Command
Description
Example
ssh user@host
Connect to host
ssh john@192.168.1.1
ssh -p port user@host
Connect using specific port
ssh -p 2222 john@server.com
ssh -i keyfile user@host
Connect using private key
ssh -i ~/.ssh/id_rsa john@server.com
ssh user@host command
Execute remote command
ssh john@server.com ls -la
ssh -v user@host
Verbose mode (debugging)
ssh -v john@server.com
ssh -C user@host
Enable compression
ssh -C john@server.com
ssh -X user@host
Enable X11 forwarding
ssh -X john@server.com
Command
Description
Example
ssh-keygen
Generate SSH key pair
ssh-keygen -t rsa -b 4096
ssh-keygen -t ed25519
Generate Ed25519 key (recommended)
ssh-keygen -t ed25519 -C "email@example.com"
ssh-copy-id user@host
Copy public key to server
ssh-copy-id john@server.com
ssh-add keyfile
Add key to SSH agent
ssh-add ~/.ssh/id_rsa
ssh-add -l
List keys in SSH agent
ssh-add -l
ssh-add -D
Remove all keys from agent
ssh-add -D
ssh-keygen -p -f keyfile
Change key passphrase
ssh-keygen -p -f ~/.ssh/id_rsa
ssh-keygen -R hostname
Remove host from known_hosts
ssh-keygen -R server.com
ssh -L local_port:remote_host:remote_port user@ssh_server
Forward local port to remote host through SSH server
Example: ssh -L 8080:localhost:80 john@server.com
ssh -R remote_port:local_host:local_port user@ssh_server
Forward remote port to local host
Example: ssh -R 9090:localhost:3000 john@server.com
Dynamic Port Forwarding (SOCKS Proxy)
ssh -D local_port user@ssh_server
Create SOCKS proxy
Example: ssh -D 1080 john@server.com
Command
Description
Example
scp file user@host:path
Copy file to remote
scp file.txt john@server.com:/home/john/
scp user@host:file .
Copy file from remote
scp john@server.com:/home/john/file.txt .
scp -r dir user@host:path
Copy directory recursively
scp -r mydir john@server.com:/home/john/
scp -P port file user@host:
SCP with custom port
scp -P 2222 file.txt john@server.com:
rsync -avz file user@host:path
Sync files (better for large transfers)
rsync -avz mydir/ john@server.com:/backup/
sftp user@host
Interactive SFTP session
sftp john@server.com
Create ~/.ssh/config for connection shortcuts:
Host myserver
HostName server.example.com
User john
Port 2222
IdentityFile ~/.ssh/id_rsa_server
Host tunnel
HostName tunnel.example.com
User admin
LocalForward 8080 localhost:80
DynamicForward 1080
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
ssh myserver - Connect using config alias
scp file myserver: - SCP using config alias
Command
Description
Example
ssh -J jumphost user@target
Connect via jump host
ssh -J bastion@jump.com john@internal.server
ssh -N -f user@host
Background connection (no shell)
ssh -N -f -L 8080:localhost:80 john@server.com
ssh -o option user@host
Set SSH option
ssh -o "StrictHostKeyChecking=no" john@server.com
ssh -A user@host
Enable agent forwarding
ssh -A john@server.com
ssh -t user@host command
Force TTY allocation
ssh -t john@server.com vim file.txt
ssh -q user@host
Quiet mode
ssh -q john@server.com
Common SSH Options for Debugging
-v - Verbose mode (use -vv or -vvv for more detail)
-o "LogLevel=DEBUG3" - Maximum debug output
-o "ConnectTimeout=10" - Set connection timeout
-o "StrictHostKeyChecking=no" - Skip host key verification (insecure)
-o "UserKnownHostsFile=/dev/null" - Don't save host key
# Check if SSH is running
systemctl status sshd # SystemD
service ssh status # SysV
# Restart SSH service
sudo systemctl restart sshd
sudo service ssh restart
Common Issues and Solutions
Issue
Solution
Permission denied (publickey)
Check key permissions: chmod 600 ~/.ssh/id_rsa
Host key verification failed
Remove old key: ssh-keygen -R hostname
Connection refused
Check if SSH service is running on server
Connection timeout
Check firewall rules and network connectivity
Too many authentication failures
Specify key: ssh -o "IdentitiesOnly=yes" -i keyfile user@host
# Start SSH agent
eval " $( ssh-agent -s) "
# Check agent is running
echo $SSH_AUTH_SOCK
# Add key to agent
ssh-add ~ /.ssh/id_rsa
Use key-based authentication instead of passwords
Disable root login in /etc/ssh/sshd_config
Use Ed25519 or RSA keys with at least 2048 bits
Keep private keys secure with proper permissions (600)
Use SSH agent forwarding carefully - can be security risk
Enable two-factor authentication when possible
Regularly update SSH software and keys
Use fail2ban or similar to prevent brute force attacks
Essential SSH commands for secure remote access and file transfer.