keepcalculator

06 Numbers and Powers

Ssh Command Usage: A Practical Guide

Card No. 06·68070 · Updated · Standard formula, shown below

The ssh command securely connects you to a remote computer over a network, letting you run commands and manage files as if you were sitting at that machine. You use it by typing ssh username@hostname, entering your password or key passphrase, and then executing commands in the remote shell. This guide covers the basics, common options, key-based authentication, and troubleshooting so you can use SSH confidently.

What Is SSH and Why Use It?

SSH (Secure Shell) is a cryptographic network protocol that provides a secure channel over an unsecured network. Developed in 1995 by Tatu Ylönen as a replacement for insecure protocols like Telnet and rlogin, SSH encrypts all traffic between your local machine and the remote server, protecting your credentials and data from eavesdropping. It is the standard method for remote administration of Linux and Unix servers, and it also supports secure file transfers via SFTP and SCP. Without SSH, anyone on the network could intercept your password and commands, making remote administration dangerous.

Basic SSH Syntax and Common Options

The simplest form is ssh [options] user@host. The user is optional if your local username matches the remote one. Common options include:

  • -p port – specify a non-default port (default is 22).
  • -i identity_file – use a specific private key file.
  • -v – verbose mode, useful for debugging.
  • -X – enable X11 forwarding for graphical applications.
  • -N – do not execute remote commands; useful for port forwarding.
  • -L local_port:remote_host:remote_port – local port forwarding.
  • -R remote_port:local_host:local_port – remote port forwarding.
  • -J jump_host – connect via a jump host (proxy).

For example, to connect to a server on port 2222 with a specific key: ssh -p 2222 -i ~/.ssh/id_ed25519 user@example.com.

How SSH Authentication Works

SSH uses two main authentication methods: passwords and public-key cryptography. With password auth, you type your password, which is encrypted and sent to the server. With key-based auth, you generate a key pair: a private key (kept secret on your machine) and a public key (placed on the server in ~/.ssh/authorized_keys). When you connect, the server sends a challenge encrypted with your public key; only your private key can decrypt it, proving your identity. Key-based auth is more secure because the private key is never transmitted and can be protected by a passphrase. To generate a key, use ssh-keygen -t ed25519 (or -t rsa -b 4096 for older systems). Then copy the public key with ssh-copy-id user@host.

Common Use Cases and Examples

SSH is versatile. Here are typical commands:

  • Run a single remote command: ssh user@host 'uptime' – shows the remote system’s uptime.
  • Copy a file securely: scp file.txt user@host:/path/ – uses SSH to transfer.
  • Mount a remote directory: sshfs user@host:/remote/path /local/mount – requires fuse and sshfs installed.
  • Create an SSH tunnel to access a web service: ssh -L 8080:localhost:80 user@host – then open http://localhost:8080 locally.
  • Use a jump host to reach a private server: ssh -J user@bastion user@internal.

For file transfers, sftp user@host gives an interactive FTP-like interface, while scp is simpler for one-off copies.

Configuring SSH for Convenience

You can create a ~/.ssh/config file to store connection parameters, making commands shorter and more readable. For example:

Host myserver
    HostName 192.168.1.100
    User john
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

Then you can simply type ssh myserver. You can also set default options like ServerAliveInterval 60 to keep connections alive, or ForwardX11 yes to enable X11 forwarding automatically. This file is read by the OpenSSH client on Linux, macOS, and Windows (via PowerShell or WSL).

Common Problems and How to Fix Them

Several issues arise frequently:

  • Connection refused – The SSH service is not running on the remote host, or a firewall is blocking port 22. Check with systemctl status sshd (Linux) or ensure the port is open.
  • Permission denied (publickey) – Your key is not in the server’s authorized_keys, or the file permissions are too open. Fix with chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys on the server.
  • Host key verification failed – The server’s key changed (e.g., after reinstall). Remove the old key with ssh-keygen -R hostname and reconnect.
  • Connection timed out – Network issues or firewall dropping packets. Try ping and telnet host 22 to test connectivity.
  • Too many authentication failures – The client tries multiple keys before the server gives up. Use ssh -o IdentitiesOnly=yes -i specific_key user@host to limit.

Always use ssh -v to see detailed debug output; it often reveals the exact cause.

Security Best Practices

SSH is secure, but misconfiguration can weaken it. Follow these guidelines:

  • Disable password authentication on servers if possible, using PasswordAuthentication no in sshd_config.
  • Use key-based auth with a strong passphrase, and consider using an SSH agent to avoid typing it repeatedly.
  • Change the default port (22) to a non-standard one to reduce automated attacks, though this is not a security measure by itself.
  • Use fail2ban to block IPs after repeated failed attempts.
  • Keep your SSH client and server updated to patch vulnerabilities.
  • Never share your private key; it is the equivalent of a password.

Alternatives to SSH

While SSH is the de facto standard, alternatives exist for specific needs. Telnet is insecure and should be avoided. RDP (Remote Desktop Protocol) is for graphical Windows desktops. VNC provides remote graphical access but is less secure without a VPN. Mosh (Mobile Shell) is a remote shell that handles intermittent connectivity better than SSH, but it requires SSH to establish the initial connection. For file transfers, rsync over SSH is efficient for syncing directories, and SFTP is a secure alternative to FTP.

Frequently Asked Questions

How do I copy files over SSH?

Use scp for simple copies: scp file.txt user@host:/remote/path/. For directories, add -r. For more control (resume, sync), use rsync -avz -e ssh source/ user@host:/dest/.

How do I keep an SSH session alive?

Add ServerAliveInterval 60 to your ~/.ssh/config or use ssh -o ServerAliveInterval=60 user@host. This sends a keepalive packet every 60 seconds to prevent the connection from timing out.

What is port forwarding and how do I use it?

Port forwarding (tunneling) creates a secure encrypted channel between your local machine and a remote server. For example, ssh -L 8080:localhost:80 user@host forwards your local port 8080 to the remote server’s port 80, letting you access a web service securely.

Why do I get ‘Host key verification failed’?

This happens when the remote host’s key fingerprint does not match the one stored in your ~/.ssh/known_hosts. It could be a man-in-the-middle attack or the server was reinstalled. Remove the old entry with ssh-keygen -R hostname and reconnect, but verify the new fingerprint out-of-band.

Can I use SSH on Windows?

Yes. Windows 10 and 11 include OpenSSH client by default in PowerShell. You can also use Windows Subsystem for Linux (WSL) for a full Linux environment, or third-party clients like PuTTY for older versions.

How do I generate an SSH key pair?

Run ssh-keygen -t ed25519 (or -t rsa -b 4096). It will ask for a file location (default ~/.ssh/id_ed25519) and a passphrase. Then copy the public key to the server with ssh-copy-id user@host.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →