π SSH Mastery: The Ultimate Guide
Command
Description
ssh user@hostname
π Connect to a remote host
ssh -p 2222 user@hostname
π’ Connect using a specific port
ssh -i ~/.ssh/id_rsa user@hostname
π Connect using a specific identity file
exit or logout
π Terminate the SSH session
Command
Description
ssh-keygen -t rsa -b 4096
π οΈ Generate a new RSA key pair
ssh-keygen -t ed25519
π¬ Generate a new Ed25519 key pair (more secure)
ssh-copy-id user@hostname
π€ Copy your public key to a remote host
ssh-add ~/.ssh/id_rsa
π Add your private key to the SSH agent
ssh-add -l
π List keys added to the SSH agent
ssh-add -D
ποΈ Remove all keys from the SSH agent
Syntax
Description
Host nickname
π·οΈ Start a new host section
HostName example.com
π₯οΈ Specify the actual hostname
User username
π€ Set the username for this host
Port 2222
π’ Set a custom port
IdentityFile ~/.ssh/id_rsa
π Specify the identity file to use
π File Transfer with SCP
Command
Description
scp file.txt user@hostname:/path/to/destination/
π€ Copy a file to a remote host
scp user@hostname:/path/to/file.txt ./
π₯ Copy a file from a remote host
scp -r directory/ user@hostname:/path/to/destination/
π Copy a directory to a remote host
Command
Description
ssh -L 8080:localhost:80 user@hostname
π Local port forwarding
ssh -R 8080:localhost:80 user@hostname
π Remote port forwarding
ssh -D 9090 user@hostname
𧦠Dynamic port forwarding (SOCKS proxy)
Command
Description
ssh -v user@hostname
π Verbose mode (for debugging)
ssh -vv user@hostname
π Very verbose mode
ssh -vvv user@hostname
π΅οΈ Extremely verbose mode
π SSH Security Best Practices
Practice
Description
Use key-based authentication
π Disable password authentication
Keep your private key safe
π Never share your private key
Use strong passphrases
π Protect your keys with strong passphrases
Regularly update SSH
π Keep your SSH client and server up to date
Use SSH config file
βοΈ Manage multiple connections efficiently
Limit SSH access
π« Use AllowUsers or AllowGroups in sshd_config
Change default port
π’ Use a non-standard port to reduce automated attacks
Use fail2ban
π‘οΈ Automatically block IPs with too many failed attempts
π οΈ Advanced SSH Techniques
Technique
Description
~C
π Enter the SSH command line
ssh -t user@hostname 'command'
π₯οΈ Run a command on a remote host and exit
ssh-keygen -y -f ~/.ssh/id_rsa
π Extract public key from private key
ssh-keygen -R hostname
ποΈ Remove a host from known_hosts file
ssh -J user@jump_host user@target_host
π¦ Use a jump host (SSH proxy)
π SSH Config File Example
Host github
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa
Host dev-server
HostName 192.168.1.100
User developer
Port 2222
IdentityFile ~/.ssh/dev_rsa
Host *
AddKeysToAgent yes
UseKeychain yes
IdentitiesOnly yes
π Use different SSH keys for different purposes (e.g., work, personal, GitHub)
π’ Set up SSH key with a passphrase, then use ssh-agent to avoid typing it repeatedly
π Use ControlMaster in SSH config to reuse connections
π Create aliases in your shell for frequent SSH connections
π Use ssh-audit tool to check the security of your SSH server
π Implement two-factor authentication for SSH when possible
π Use htop over SSH to monitor remote system resources
π Use sshfs to mount remote filesystems over SSH
π Set up automatic key rotation for enhanced security
π Familiarize yourself with ~/.ssh/authorized_keys file format and options
Remember, SSH is a powerful tool. Use it responsibly and keep your systems secure! π‘οΈπ