sodcalculator

10 Tech How-To

SSH in Linux: How to Use It

Guide 10-28825 · Updated · Step by step

SSH (Secure Shell) lets you securely connect to another computer over a network, usually to run commands on a remote Linux server. This guide walks you through the basics: connecting, running commands, transferring files, and using keys for passwordless logins. You’ll need a Linux machine with an SSH client (almost always preinstalled) and access to a remote server with SSH enabled.

What You’ll Need

  • A Linux computer (local machine) with the ssh command available (check with which ssh).
  • A remote server or another Linux machine with SSH server running (usually sshd).
  • The remote server’s IP address or hostname, a username, and either a password or an SSH key.
  • Network access to the remote machine (port 22 must be reachable).

Step-by-Step

  1. Open a terminal. On most Linux distributions, press Ctrl+Alt+T or search for “Terminal” in the applications menu.
  2. Check if SSH is installed. Run ssh -V. If you see a version number, you’re good. If not, install it: on Debian/Ubuntu use sudo apt install openssh-client, on Fedora/RHEL use sudo dnf install openssh-clients.
  3. Connect to the remote server. Use the basic syntax: ssh username@hostname. For example, ssh alice@192.168.1.100. Replace alice with your username on the remote machine and the IP with the server’s address.
  4. Accept the host key (first time only). The first time you connect, you’ll see a warning about the host’s authenticity. Type yes and press Enter. This stores the server’s key in ~/.ssh/known_hosts.
  5. Enter your password. When prompted, type your password (nothing will show as you type, that’s normal) and press Enter. You should now be logged in to the remote shell.
  6. Run commands remotely. Once connected, any command you type runs on the remote machine. For example, whoami shows the remote user, ls lists files in the remote directory.
  7. Exit the session. Type exit or press Ctrl+D to return to your local terminal.
  8. Run a single command without a full session. Use ssh username@hostname 'command'. For example, ssh alice@192.168.1.100 'uptime' runs uptime on the remote and shows the output locally, then disconnects.
  9. Copy files with SCP. To copy a file from local to remote: scp /path/to/local/file username@hostname:/path/on/remote. To copy from remote to local: scp username@hostname:/path/on/remote/file /path/to/local/.
  10. Set up SSH key authentication (recommended). Generate a key pair on your local machine: ssh-keygen -t ed25519. Press Enter to accept the default location (~/.ssh/id_ed25519) and optionally set a passphrase. Then copy the public key to the server: ssh-copy-id username@hostname. This adds your public key to the server’s ~/.ssh/authorized_keys file.
  11. Test passwordless login. After copying the key, try ssh username@hostname. It should log you in without asking for a password (if you set a passphrase, it will ask for that instead).

Common Mistakes

  • Typing your password while it’s not asked. Wait for the prompt. If you type before the prompt, characters may appear in the terminal or be ignored.
  • Using the wrong username. The username must be a valid account on the remote machine. If you’re not sure, try the default like ubuntu for cloud servers or root for some VPS.
  • Forgetting the port. If the server uses a non-default port, you must specify it with -p (lowercase) for SSH, e.g., ssh -p 2222 user@host. For SCP, use -P (uppercase).
  • Ignoring the host key warning. If you see a warning that the host key has changed, it could be a security issue (man-in-the-middle) or the server was reinstalled. Don’t blindly accept it; verify with the server admin.
  • Using a relative path with SCP. Always use absolute paths or ensure the relative path is correct from the current directory. For remote paths, a relative path is relative to the remote user’s home directory.

Tips

  • Create an SSH config file. In ~/.ssh/config, you can define hosts. For example:
    Host myserver
        HostName 192.168.1.100
        User alice
        Port 2222

    Then just type ssh myserver.

  • Use -v for verbose output. If something goes wrong, ssh -v user@host shows detailed debug info that helps diagnose issues.
  • Keep your private key safe. Never share your private key (~/.ssh/id_ed25519). Add a passphrase to protect it if your laptop is stolen.
  • Use ssh-agent to avoid typing passphrases. Add your key once with ssh-add and it will be used automatically for the session.
  • For file transfers, consider rsync over SSH. It’s more efficient for large or incremental transfers: rsync -avz /local/dir user@host:/remote/dir.

Troubleshooting

  • Connection refused. The SSH server isn’t running or the port is blocked. Check with sudo systemctl status sshd (on the remote, if you have console access) or try telnet host 22 to see if the port is open.
  • Permission denied (publickey,password). Your password or key is wrong, or the server doesn’t allow password auth. Try ssh -v to see which authentication methods are offered. If using keys, ensure the public key is in the correct ~/.ssh/authorized_keys on the remote and permissions are correct (chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys).
  • Host key verification failed. The server’s key changed. If you trust the server, remove the old entry with ssh-keygen -R hostname and reconnect.
  • Connection timed out. Network issue or firewall blocking port 22. Check your local network and any cloud security groups. Try pinging the host to see if it’s reachable.
  • Terminal freezes after login. This can happen with a broken shell profile. Try connecting with ssh user@host /bin/bash to bypass the default shell.

Frequently Asked Questions

What is the default SSH port?

Port 22. If the server uses a different port, you must specify it with -p.

Can I use SSH on Windows?

Yes, Windows 10 and 11 have OpenSSH client built-in. You can use the same commands in PowerShell or CMD.

How do I copy files from remote to local?

Use scp user@host:/path/to/remote/file /local/destination.

What is the difference between SSH and SCP?

SSH is for interactive login and running commands; SCP is for copying files over the SSH protocol.

How do I change my SSH password?

You can’t change it via SSH directly. You need to use the passwd command on the remote machine after logging in.

Is SSH safe to use over the internet?

Yes, SSH encrypts all traffic. Just use strong passwords or keys, and avoid using the default root account with a weak password.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →