Skip to content
IT related

SSH cheatsheet

Updated 1 min read

Sure! Here's a comprehensive SSH (Secure Shell) cheat sheet, covering basic to advanced features, along with tips and examples:

Basic SSH Usage

  1. Connect to a Host

    ssh user@hostname
    

    Connect to hostname as user.

  2. Connect with a Specific Port

    ssh -p 2222 user@hostname
    

    Connect to hostname on port 2222.

  3. SSH Key Generation

    ssh-keygen
    

    Generate a new SSH key pair.

  4. Copy SSH Key to a Host

    ssh-copy-id user@hostname
    

    Copy your local public key to the remote host for key-based authentication.

File Transfer

  1. SCP: Copy Files Over SSH

    scp localfile.txt user@hostname:/remote/path
    scp user@hostname:/remote/path/remotefile.txt localfile.txt
    

    Copy localfile.txt to a remote host, or copy a file from a remote host to local.

  2. RSync Over SSH

    rsync -avz -e ssh localfile.txt user@hostname:/remote/path
    

    Securely sync files over SSH.

Advanced SSH Features

  1. SSH Tunneling (Port Forwarding)

    • Local Port Forwarding

      ssh -L localPort:remoteAddress:remotePort user@hostname
      

      Forward a local port to a remote address and port.

    • Remote Port Forwarding

      ssh -R remotePort:localAddress:localPort user@hostname
      

      Forward a remote port to a local address and port.

    • Dynamic Port Forwarding (SOCKS Proxy)

      ssh -D localPort user@hostname
      

      Create a SOCKS proxy on a local port.

  2. SSH Config File

    • Create a ~/.ssh/config file for easier connection management. Example:
      Host myserver
        HostName server.example.com
        User myuser
        Port 2222
        IdentityFile ~/.ssh/mykey
      
    • Connect using the alias: ssh myserver
  3. SSH Agent Forwarding

    ssh -A user@hostname
    

    Forward your SSH key agent to the host (useful for hopping between hosts).

  4. X11 Forwarding

    ssh -X user@hostname
    

    Forward X11 (GUI) applications from the remote host.

SSH Tips

  1. Keep SSH Sessions Alive

    • Add the following to ~/.ssh/config:
      Host *
        ServerAliveInterval 60
      
    • This sends a keepalive signal every 60 seconds to keep the connection active.
  2. Use SSH Multiplexing

    • Reuse an existing SSH connection for multiple sessions to the same host.
    • Add to ~/.ssh/config:
      Host *
        ControlMaster auto
        ControlPath ~/.ssh/sockets/%r@%h-%p
        ControlPersist 600
      
  3. Mount Remote Filesystem with SSHFS

    sshfs user@hostname:/remote/path /local/mount/point
    

    Mount a remote filesystem locally via SSHFS.

Security Tips

  1. Disable Root Login

    • In the remote /etc/ssh/sshd_config file, ensure:
      PermitRootLogin no
      
  2. Use Key-Based Authentication

    • Disable password authentication in /etc/ssh/sshd_config:
      PasswordAuthentication no
      
  3. Change Default SSH Port

    • In /etc/ssh/sshd_config:
      Port 2222
      

Examples

  1. Connect to a Server

    ssh [email protected]
    
  2. Securely Transfer a File

    scp report.txt [email protected]:/home/myuser/
    
  3. SSH Tunnel to Access a Remote Database Locally

    ssh -L 3306:localhost:3306 myuser@dbserver
    

    Access a remote MySQL server on local port 3306.

IT related RSS feed Published with Oblish

Keyboard shortcuts

/
Search
[
Previous page
]
Next page
?
Show this list
Esc
Close