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-copy-id: quick steps

  1. Ensure you have a key: ls ~/.ssh/id_ed25519.pub (or create one with ssh-keygen).
  2. Copy it to the host (default key/port):
    ssh-copy-id user@host
  3. Custom port or key file:
    ssh-copy-id -i ~/.ssh/id_ed25519.pub \"-p 2222 user@host\"
  4. Test login (no password prompt):
    ssh user@host

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