Linux SSH Command Examples

SSH (Secure Shell) is the standard protocol for securely connecting to remote servers over an encrypted connection. Beyond basic login, SSH enables tunneling, port forwarding, file transfers via SCP, and running remote commands — making it the backbone of modern DevOps and cloud workflows.

LinuxSSHRemoteSecurity
Connecting
Basic SSH connection
ssh user@hostname
The most basic SSH command. Replace user with your username and hostname with the server IP or domain. SSH connects on port 22 by default and prompts for a password if no key is configured.
Connect on a non-default port
ssh -p 2222 user@hostname
Use -p to specify a non-standard port. Many hardened servers run SSH on a custom port to reduce automated brute-force attempts against the default port 22. This is a common security practice for public-facing servers.
Authenticate with a specific private key
ssh -i ~/.ssh/mykey.pem user@hostname
The -i flag specifies a private key file for authentication. AWS EC2, DigitalOcean Droplets, and other cloud providers use key-pair authentication. The private key must have restrictive permissions: chmod 600 mykey.pem.
Run a command on the remote server
ssh user@hostname 'ls -la /var/www && df -h'
Append a quoted command string to execute it on the remote server and return the output locally. The connection closes automatically when the command finishes. This is the foundation of automated deployment and monitoring scripts.
File Transfer (SCP)
Copy a file to a remote server
scp localfile.txt user@hostname:/remote/path/
SCP (Secure Copy) uses SSH encryption to transfer files. This copies a local file to the specified remote path. Any SSH keys you've configured for the host work automatically — no separate authentication setup needed.
Copy a file from remote to local
scp user@hostname:/remote/file.txt ./
Reverse the source and destination to download a file. The ./ saves it in the current local directory. Useful for retrieving logs, database exports, or generated reports from production servers.
Copy a directory recursively
scp -r ./localdir user@hostname:/remote/path/
The -r flag copies directories recursively including all subdirectories and files. For large or frequently synced directories, rsync -avz is more efficient as it only transfers changed files.
Tunneling & Keys
Local port forwarding
ssh -L 8080:localhost:80 user@hostname
Forwards your local port 8080 to port 80 on the remote server. Open http://localhost:8080 in your browser to reach the remote service. Essential for accessing databases, admin panels, or web apps behind a firewall.
Background SSH tunnel (no terminal)
ssh -fN -L 5432:localhost:5432 user@hostname
-f sends SSH to the background after authentication. -N tells SSH not to execute a remote command (port forwarding only). This creates a persistent tunnel to a remote PostgreSQL database without occupying a terminal session.
Copy public key for passwordless login
ssh-copy-id user@hostname
Copies your local public key (~/.ssh/id_rsa.pub) to the server's authorized_keys file, enabling passwordless future logins. If ssh-copy-id isn't available: cat ~/.ssh/id_rsa.pub | ssh user@host 'cat >> ~/.ssh/authorized_keys'.

How to Use

  1. Generate a key pair if you don't have one: ssh-keygen -t ed25519.
  2. Copy your public key to the server: ssh-copy-id user@hostname.
  3. Connect with ssh user@hostname for passwordless key-based login.
  4. Add frequently used servers to ~/.ssh/config with Host aliases to avoid typing long options.

Frequently Asked Questions

What is the difference between SSH and SCP?

SSH is the protocol for remote terminal access and command execution. SCP (Secure Copy) is a utility built on SSH specifically for transferring files. Both use the same authentication and encryption, so your SSH keys work for SCP automatically with no extra setup.

How do I fix "Permission denied (publickey)" errors?

This usually means the server doesn't have your public key, or local key permissions are wrong. Verify your public key is in ~/.ssh/authorized_keys on the server. Locally, ensure the private key has permissions of 600: chmod 600 ~/.ssh/id_rsa.

How do I create SSH config shortcuts?

Add entries to ~/.ssh/config: Host myserver / HostName 1.2.3.4 / User ubuntu / IdentityFile ~/.ssh/mykey.pem. Then connect with just ssh myserver instead of specifying all options each time.

Related Tools