sodcalculator

10 Tech How-To

SSH Connect Remote Server Linux: Step-by-Step

Guide 10-56590 · Updated · Step by step

SSH (Secure Shell) lets you securely log into a remote Linux server from your own machine, giving you a command-line terminal as if you were sitting at that server. This guide walks you through the entire process—from generating a key pair to connecting for the first time—so you can manage files, run commands, and administer your server remotely.

What You’ll Need

  • A remote Linux server (e.g., a VPS, cloud instance, or a machine on your network) with SSH enabled (usually the default).
  • The server’s IP address or hostname, and the username you’ll log in as (often root or a user with sudo privileges).
  • A local machine with an SSH client. Linux and macOS have ssh built in; Windows 10/11 includes OpenSSH, or you can use PowerShell or the Windows Terminal.
  • Optional but recommended: a password or an SSH key pair for authentication.

Step-by-Step

  1. Open a terminal on your local machine. On Linux, press Ctrl+Alt+T; on macOS, open Terminal from Applications; on Windows, open PowerShell or Windows Terminal.
  2. Check if SSH is installed by typing ssh -V and pressing Enter. You should see a version number. If not, install OpenSSH client via your package manager (e.g., sudo apt install openssh-client on Debian/Ubuntu).
  3. Find your server’s IP address or hostname. You should have this from your hosting provider or network admin. For example, 192.168.1.100 or server.example.com.
  4. Connect using password authentication (simplest for first time): type ssh username@server_ip, e.g., ssh root@192.168.1.100. Press Enter.
  5. Accept the host key when prompted. The first time you connect, SSH shows a fingerprint and asks if you trust the host. Type yes and press Enter. This stores the host key in ~/.ssh/known_hosts.
  6. Enter your password when asked. You won’t see any characters as you type—that’s normal. Press Enter after typing it.
  7. You’re now logged in. You’ll see a shell prompt like root@server:~#. You can run commands, navigate files, and manage the server.
  8. To exit, type exit or press Ctrl+D. This closes the SSH session and returns you to your local terminal.
  9. Set up SSH key authentication for more secure and convenient logins (recommended). On your local machine, generate a key pair with ssh-keygen -t rsa -b 4096. Press Enter to accept the default file location (~/.ssh/id_rsa) and optionally set a passphrase.
  10. Copy your public key to the server using ssh-copy-id username@server_ip. This will ask for your password once, then adds your public key to the server’s ~/.ssh/authorized_keys file.
  11. Test key-based login by running ssh username@server_ip again. If you set a passphrase, you’ll be asked for it; otherwise, you should log in without a password.
  12. Optional: Disable password authentication on the server for better security. Edit /etc/ssh/sshd_config on the server, set PasswordAuthentication no, then restart SSH with sudo systemctl restart sshd. Make sure your key works before doing this!

Common Mistakes

  • Typing the wrong username or IP. Double-check the exact spelling and case—usernames are case-sensitive.
  • Forgetting the port. If your server uses a non-default SSH port (not 22), you must specify it with -p port_number, e.g., ssh -p 2222 user@server. Otherwise, the connection will time out.
  • Ignoring the host key warning. If you see a warning that the host key has changed, it could mean a man-in-the-middle attack or the server was reinstalled. Don’t blindly accept it—verify with your server provider if needed.
  • Using the wrong key file. If you have multiple keys, specify the one to use with -i /path/to/key, e.g., ssh -i ~/.ssh/mykey user@server.
  • Permissions issues on the server. Your ~/.ssh directory on the server must have permissions 700 and authorized_keys must be 600. If not, SSH will refuse the key. Fix with chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys.

Tips

  • Use SSH keys instead of passwords. They’re more secure and allow automation. Always protect your private key with a strong passphrase.
  • Create a config file (~/.ssh/config) to simplify connections. For example, add Host myserver, HostName 192.168.1.100, User root, Port 22. Then you can just type ssh myserver.
  • Use ssh -v for verbose output when debugging connection issues. It shows detailed steps of the handshake.
  • Keep your system updated. Regularly update OpenSSH on both client and server to get security patches.
  • Use a terminal multiplexer like tmux or screen on the server so your sessions survive disconnects and you can run long processes.

Troubleshooting

  • Connection refused or timeout. Check that the SSH service is running on the server (sudo systemctl status sshd). Ensure the firewall allows port 22 (or your custom port). On many cloud providers, you must open the port in their security group settings.
  • Permission denied (publickey,password). This usually means the username or password is wrong, or the server doesn’t allow password auth. Try ssh -v to see what’s happening. If using a key, ensure the public key is correctly placed in ~/.ssh/authorized_keys on the server.
  • Host key verification failed. This happens when the server’s key changed. If you trust the server, remove the old key from ~/.ssh/known_hosts using ssh-keygen -R server_ip and try again.
  • Connection closed by remote host. This could be due to a firewall, a fail2ban rule, or the server rejecting your IP. Check server logs with sudo tail -f /var/log/auth.log (on Debian/Ubuntu) or /var/log/secure (on RHEL/CentOS).
  • Slow connection. Try adding -o ConnectTimeout=10 to your command to set a timeout. Also check if your local network is slow or if the server is under load.

Frequently Asked Questions

Can I SSH into a server without a password?

Yes, by using SSH key authentication. Generate a key pair, copy the public key to the server’s authorized_keys file, and you can log in without a password (unless you set a passphrase on your key).

What is the default SSH port?

The default is port 22. If your server uses a different port, you must specify it with the -p option.

How do I copy files over SSH?

Use scp (secure copy) or rsync. For example, scp file.txt user@server:/path/to/destination copies a file to the server. To download, reverse the order.

Why do I get a ‘Permission denied’ error even with the correct password?

This can happen if the server has disabled password authentication, if the account is locked, or if you’re using the wrong username. Check the server’s SSH config and logs for details.

Can I SSH from Windows?

Yes, Windows 10 and 11 include OpenSSH client. Open PowerShell or Windows Terminal and use the same ssh command. You can also use third-party tools like PuTTY.

How do I change my SSH password?

Log in via SSH and run the passwd command. You’ll be prompted to enter your current password and then set a new one.

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 →