Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created August 24, 2024 06:47
Show Gist options
  • Select an option

  • Save copyleftdev/0b72b91addf7e1fa5a7de617b66f99b3 to your computer and use it in GitHub Desktop.

Select an option

Save copyleftdev/0b72b91addf7e1fa5a7de617b66f99b3 to your computer and use it in GitHub Desktop.
SSH Mastery: The Ultimate Guide

πŸ” SSH Mastery: The Ultimate Guide

🌟 Basic SSH Commands

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

πŸ”‘ Key Management

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

πŸ”’ SSH Config File

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

πŸ”„ Port Forwarding

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)

πŸ” SSH Debugging

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

πŸ† Pro Tips

  1. πŸ”‘ Use different SSH keys for different purposes (e.g., work, personal, GitHub)
  2. πŸ”’ Set up SSH key with a passphrase, then use ssh-agent to avoid typing it repeatedly
  3. πŸ”„ Use ControlMaster in SSH config to reuse connections
  4. πŸ“ Create aliases in your shell for frequent SSH connections
  5. πŸ” Use ssh-audit tool to check the security of your SSH server
  6. πŸ” Implement two-factor authentication for SSH when possible
  7. πŸ“Š Use htop over SSH to monitor remote system resources
  8. πŸ“‚ Use sshfs to mount remote filesystems over SSH
  9. πŸ”„ Set up automatic key rotation for enhanced security
  10. πŸ“š Familiarize yourself with ~/.ssh/authorized_keys file format and options

Remember, SSH is a powerful tool. Use it responsibly and keep your systems secure! πŸ›‘οΈπŸ”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment