SSH Commands Cheatsheet — Essential Reference
SSH (Secure Shell) is a cryptographic network protocol for operating network services securely over an unsecured network. It is primarily used to log into remote computers and execute commands, but it also supports tunneling, forwarding TCP ports, and X11 connections.
Secure Your Connection with NordVPN
Protect your SSH sessions and network traffic with the gold standard in VPN security. Get up to 60% off.
Commands
| Command | Description | Example |
|---|---|---|
ssh [user@]hostname |
Connects to a remote host. If 'user@' is omitted, your local username is used. | ssh user@example.com |
ssh -p [port] [user@]hostname |
Connects to a remote host on a specified port. | ssh -p 2222 user@example.com |
ssh -i [path/to/key] [user@]hostname |
Connects to a remote host using a specified private key for authentication. | ssh -i ~/.ssh/my_private_key user@example.com |
ssh -L [local_port]:[remote_host]:[remote_port] [user@]hostname |
Establishes local port forwarding (tunneling). Traffic to local_port on your machine is forwarded to remote_host:remote_port via the SSH server. | ssh -L 8080:localhost:80 user@example.com |
ssh -R [remote_port]:[local_host]:[local_port] [user@]hostname |
Establishes remote port forwarding. Traffic to remote_port on the SSH server is forwarded to local_host:local_port on your machine. | ssh -R 8080:localhost:80 user@example.com |
ssh -X [user@]hostname |
Enables X11 forwarding, allowing graphical applications from the remote host to display on your local machine. | ssh -X user@example.com |
ssh -N [user@]hostname |
Connects to a remote host without executing a remote command. Useful for port forwarding only. | ssh -N -L 8080:localhost:80 user@example.com |
ssh-keygen |
Generates a new SSH key pair (public and private keys). | ssh-keygen -t rsa -b 4096 |
ssh-copy-id [user@]hostname |
Copies your public SSH key to the remote host's authorized_keys file, enabling passwordless login. | ssh-copy-id user@example.com |
Pro Tips
- Use `~/.ssh/config` to define aliases and custom settings for frequently accessed hosts, e.g., `Host my_server Hostname example.com User myuser Port 2222 IdentityFile ~/.ssh/my_key`
- Always use SSH keys for authentication instead of passwords for better security. Protect your private key with a strong passphrase.
- For long-running SSH sessions that might be interrupted, consider using `tmux` or `screen` on the remote server.
- Use `ssh-agent` to manage your SSH keys and avoid re-entering passphrases repeatedly.
- The `-o` option allows specifying any configuration option directly on the command line, e.g., `ssh -o StrictHostKeyChecking=no user@example.com`.
Frequently Asked Questions
What is ssh used for?
SSH is primarily used for secure remote access to servers and other computers. It allows you to execute commands, transfer files (with scp or sftp), and create secure tunnels for other network services, all over an encrypted connection.
How do I install ssh?
On most Linux distributions and macOS, the SSH client (`ssh`) is pre-installed. For Windows, you can use the built-in OpenSSH client (available in Windows 10 and Server 2019+), or install a client like PuTTY or Git Bash which includes SSH.