SSH Cheat Sheet

Here’s a cheat sheet for working with SSH (Secure Shell), a protocol for secure remote access to servers:

Connecting to a Remote Server

Syntax:

ssh [username]@[hostname or IP address] -p [port]

Example: ssh [email protected] -p 22

Key-based Authentication

Generate SSH Key Pair:

ssh-keygen -t rsa -b 2048

Copy Public Key to Server:

ssh-copy-id [username]@[hostname or IP address] -p [port]

Example: ssh-copy-id [email protected] -p 22

SSH Config File

Location:

  • ~/.ssh/config

Sample Config:

Host myserver
    HostName 192.168.1.100
    Port 22
    User user

Connect Using Config:

ssh myserver

File Transfer with SCP

Copy Local to Remote:

scp [local_file] [username]@[hostname or IP address]:[remote_directory]

Example: scp local.txt [email protected]:/home/user/

Copy Remote to Local:

scp [username]@[hostname or IP address]:[remote_file] [local_directory]

Example: scp [email protected]:/home/user/remote.txt .

Tunneling (Port Forwarding)

Local Port Forwarding:

ssh -L [local_port]:[destination_host]:[destination_port] [username]@[ssh_server]

Example: ssh -L 8080:localhost:80 user@ssh-server

Dynamic Port Forwarding (SOCKS Proxy):

ssh -D [local_port] [username]@[ssh_server]

Example: ssh -D 1080 user@ssh-server

SSH Agent

Start SSH Agent:

eval $(ssh-agent)

Add SSH Key to Agent:

ssh-add ~/.ssh/id_rsa

Miscellaneous

Terminate Remote Process (Ctrl+C equivalent):

  • Press ~ and then . (dot)

SSH Version:

ssh -V

Check Server Fingerprint:

ssh-keyscan -p [port] [hostname or IP address]

This cheat sheet covers some common SSH commands and configurations. For more details and options, refer to the OpenSSH documentation.