
SSH commands: SSH provides encrypted remote access, key-based authentication, file copying and port forwarding. Keep private keys private and verify remote host identities. This guide explains what each command does, its syntax, practical examples, useful options, and common mistakes. Commands are shown for typical installations; flags can vary across operating-system and tool versions.
Before you start
Open a terminal appropriate for the operating system shown in the examples. Check that the required tool is installed and that you are running it with the permissions needed for your task. Replace sample names, paths, hostnames and IDs with values from your own environment.
Syntax at a glance
Square brackets usually indicate optional arguments, while uppercase words such as FILE, HOST and PACKAGE are placeholders that you replace. Keep quotes around paths or text that contains spaces. Commands should be tested on a safe sample before being used in production.
Quick command reference
| Command | What it does | Example | Useful options |
|---|---|---|---|
ssh USER@HOST | Open a secure shell on a remote host | ssh deploy@example.com | -p PORT specifies a non-default port; -v enables diagnostics |
ssh-keygen | Create a local SSH key pair | ssh-keygen -t ed25519 -C "deploy key" | -f chooses a file path; protect private keys with a passphrase |
ssh-copy-id | Install a public key on a remote account | ssh-copy-id deploy@example.com | -i selects a public-key file |
scp | Copy files over SSH | scp backup.tar.gz deploy@example.com:/tmp/ | -P PORT chooses the SSH port; -r copies directories recursively |
ssh -L | Forward a local port through an SSH connection | ssh -L 8080:127.0.0.1:80 user@server.example.com | -N opens no remote command; -f runs in background where supported |
Command syntax and examples explained
1. ssh USER@HOST
Purpose: Open a secure shell on a remote host.
Syntax
ssh USER@HOSTExample
ssh deploy@example.comOptions and notes: -p PORT specifies a non-default port; -v enables diagnostics.
Verify the host fingerprint, especially on first connection.
2. ssh-keygen
Purpose: Create a local SSH key pair.
Syntax
ssh-keygen -t ed25519 -C COMMENTExample
ssh-keygen -t ed25519 -C "deploy key"Options and notes: -f chooses a file path; protect private keys with a passphrase.
Share only the public .pub key; never share the private key.
3. ssh-copy-id
Purpose: Install a public key on a remote account.
Syntax
ssh-copy-id USER@HOSTExample
ssh-copy-id deploy@example.comOptions and notes: -i selects a public-key file.
Use only for accounts you are authorized to access.
4. scp
Purpose: Copy files over SSH.
Syntax
scp SOURCE USER@HOST:DESTINATIONExample
scp backup.tar.gz deploy@example.com:/tmp/Options and notes: -P PORT chooses the SSH port; -r copies directories recursively.
Check the destination before copying potentially sensitive files.
5. ssh -L
Purpose: Forward a local port through an SSH connection.
Syntax
ssh -L LOCAL_PORT:HOST:REMOTE_PORT USER@SSH_HOSTExample
ssh -L 8080:127.0.0.1:80 user@server.example.comOptions and notes: -N opens no remote command; -f runs in background where supported.
Port forwarding grants access through the remote host; limit it to trusted machines.
Common mistakes and troubleshooting
- Command not found: confirm the tool is installed and available on your PATH; open a new terminal after changing PATH.
- Permission denied: verify the current account, file permissions and target path. Use administrator/root privileges only when genuinely required.
- Unexpected output: check the command version, quoting, current directory and exact arguments. Consult the local help or manual for options supported by your build.
- Destructive operations: preview the target and keep a backup before deleting data, rewriting history, flashing a partition or changing system configuration.
Safety tips
Review every command before running it, especially commands that delete files, change permissions, flash firmware, alter a database or expose a network service. For network scanning and remote administration, work only on devices and systems you own or are authorized to test. Keep credentials, private keys, access tokens and log files private.
Frequently asked questions
Do these commands work on every system?
Not always. Some commands are specific to Windows, Linux, Android, a particular package manager or an installed tool version. Use the syntax shown for your platform and check local help when a flag is unavailable.
Can I copy and run every example exactly?
Use the examples as templates. Replace placeholders such as filenames, package IDs, hostnames and paths with your own values, and verify the effect before executing a command that changes data or system state.
Conclusion
Use this reference as a starting point, then confirm flags in the tool’s official help or manual for your installed version. A clear understanding of syntax, options and side effects makes command-line work more reliable.