SSH Command Reference Sheet

Basic SSH Commands

Command Description
ssh user@host Connect to a remote host as a specific user.
ssh -p port user@host Connect using a non-default port.
ssh host Connect to a remote host using default username.
ssh -i /path/to/key user@host Connect using a specific private key file.
ssh-copy-id user@host Install your public key on a remote host for passwordless login.
ssh -v user@host Connect with verbose output for debugging.
ssh -X user@host Enable X11 forwarding for GUI applications.

SSH Key Management

SSH Configuration File (~/.ssh/config)

Create a configuration file to simplify SSH commands:

Host shortname
    HostName example.com
    User username
    Port 2222
    IdentityFile ~/.ssh/id_ed25519
    ForwardAgent yes

Usage:

Now you can connect using ssh shortname instead of the full command.

SSH Tunneling and Port Forwarding

Local Port Forwarding

Forward a port from the local machine to the remote server.

ssh -L local_port:destination_host:destination_port user@remote_host

Example: Access a database on a remote server's network:

ssh -L 3306:db.internal.local:3306 user@remote_host

Now, you can connect to localhost:3306 to access the remote database.

Remote Port Forwarding

Forward a port from the remote server to the local machine.

ssh -R remote_port:destination_host:destination_port user@remote_host

Example: Allow the remote host to access a service running on your local machine:

ssh -R 8080:localhost:3000 user@remote_host

The remote host can now access your local service via localhost:8080.

Dynamic Port Forwarding (SOCKS Proxy)

Create a SOCKS proxy on the local machine that routes traffic through the SSH server.

ssh -D local_port user@remote_host

Example: Set up a SOCKS proxy on port 1080:

ssh -D 1080 user@remote_host

Configure your applications to use localhost:1080 as a SOCKS proxy.

SSH Multiplexing

Reuse SSH connections to improve performance.

Host *
    ControlMaster auto
    ControlPath ~/.ssh/ssh_mux_%h_%p_%r
    ControlPersist 10m

With this configuration in ~/.ssh/config, SSH connections to the same host will be reused for 10 minutes.

SSH Jump Hosts (ProxyJump)

Connect to a remote server via an intermediate SSH server.

ssh -J user@jump_host user@destination_host

Example: Connect to a server behind a firewall using a jump host:

ssh -J [email protected] [email protected]

You can also configure this in ~/.ssh/config:

Host internal
    HostName internal.example.com
    User user
    ProxyJump [email protected]

Now, connect using ssh internal.

SSH File Transfers

SCP (Secure Copy)

SFTP (SSH File Transfer Protocol)

SSH Control and Management

Advanced SSH Examples

Mount Remote Directory with SSHFS

Mount a remote directory over SSH:

sshfs user@remote_host:/remote/path /local/mountpoint

Unmount:

fusermount -u /local/mountpoint

SSH Socks Proxy for Browser

Set up a SOCKS proxy and configure your web browser to use it for secure browsing:

ssh -D 8080 -C user@remote_host

Configure your browser's proxy settings to use localhost on port 8080 as a SOCKS v5 proxy.

Run Remote Command Non-Interactively

Execute a command on a remote server without logging in:

ssh user@remote_host 'ls -la /var/www'

SSH Port Knocking

If your server uses port knocking for security, use the following sequence:

for x in port1 port2 port3; do nmap -Pn --host_timeout 100 --max-retries 0 -p $x your.server.com; done
ssh [email protected]

Security Best Practices

Troubleshooting

Return to Home