SSH lets you log in to a remote Linux machine and run commands on it as if you were sitting in front of it. If you manage servers, you'll use it every day.
This post covers how SSH works, connecting to a server, the client config file, SSH keys, securing the server, reaching your server from anywhere with Tailscale, and troubleshooting. Every command here is something you can run yourself.
You'll need a Linux server you can reach: a cloud VM, a local VM, or a spare PC on your network. You need its IP address and a user account on it.
What is OpenSSH?
OpenSSH is the SSH implementation that ships with almost every Linux distribution. It was built by the OpenBSD project. In this post, "SSH" and "OpenSSH" mean the same thing.
It's a set of tools, and the two main ones are:
- The client (
ssh) runs on your computer and connects out to other machines. - The server (
sshd) runs on the remote machine and accepts incoming connections.
Having the client installed does not let anyone connect to you. Only the server accepts connections.
The network path
When you connect to a remote server, your traffic passes through several devices on the way:
- Your laptop and home router
- The internet
- The remote site's router
- Their firewall
- The server, where
sshdmust be running
SSH uses port 22 by default. If any of these blocks port 22, or sshd isn't running, the connection fails, and SSH often won't tell you why. When troubleshooting, the first question is always: how far does the connection get?
Connecting to a Server
Check the client
Linux and macOS have the SSH client installed by default. Windows 10 and 11 include it too, so ssh works in PowerShell or Windows Terminal.
which ssh
If you get /usr/bin/ssh, it's installed. If not, install it. On Debian or Ubuntu:
sudo apt install openssh-client
On Fedora and RHEL the package is openssh-clients.
Connect
ssh username@server-ip
For example:
ssh furi@203.0.113.10
The username is an account on the server. Some cloud providers give you root for the first login. Use it only to create a normal user with sudo rights, then use that user from then on.
The first time you connect to a server, you'll see this:
Every server has a unique host key, and SSH is asking whether you trust this one. Type yes, enter your password, and you're in.
To disconnect, type exit or press Ctrl+D.
known_hosts
When you accept a server, SSH saves its fingerprint to ~/.ssh/known_hosts:
cat ~/.ssh/known_hosts
The next time you connect, SSH doesn't ask again. It checks the server's key against this file.
This protects you from man-in-the-middle attacks. If someone puts a different machine on that IP address, its host key won't match, and SSH shows a warning instead of connecting.
If the change is expected (for example, you rebuilt the server), remove the old entry:
ssh-keygen -R 203.0.113.10
Watch logins in real time
Open two terminals. In the first, connect to the server and follow the auth log:
sudo tail -f /var/log/auth.log
In the second, connect to the same server. You'll see an Accepted password for furi from ... line appear in the first terminal.
SSH doesn't tell a failing user much about why they failed, because that would also help attackers. The server log does tell you. When someone says "I can't connect," follow the log while they try.
The SSH Client Config File
Instead of typing ssh furi@203.0.113.10 every time, save the details in ~/.ssh/config:
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/config
Host myserver
HostName 203.0.113.10
Port 22
User furi
- Host: a nickname, anything you want
- HostName: the real IP or domain
- Port: the SSH port (22 is the default, so this line is optional)
- User: the username on the server
Now connect with:
ssh myserver
Add as many servers as you need:
Host myserver
HostName 203.0.113.10
User furi
Host server2
HostName 192.168.1.50
User furi
Using Public and Private Keys
Passwords can be guessed, and bots on the internet try to guess them all day. SSH keys are the better option.
A key pair has two parts:
- A private key that stays on your computer
- A public key that you put on each server
When you connect, the server checks that your private key matches a public key it has. If it matches, you're in without a password.
Adding a key does not turn off password login. If the key fails, SSH falls back to asking for a password. We'll disable passwords later in this post.
Generate a key
ssh-keygen -t ed25519
OpenSSH 9.5 and newer use Ed25519 by default, but it's worth being explicit for older versions.
It asks two things:
Where to save it. Press Enter for the default, ~/.ssh/id_ed25519. If a key with that name already exists, it will be overwritten, and you could lose access to servers that use it. Run ls ~/.ssh first if you're not sure.
A passphrase. Use one. It encrypts the private key file, so if someone copies the file, they can't use it without the passphrase. You type it locally, not on the server, and with ssh-agent (covered below) you only type it once per session.
You now have two files:
~/.ssh/id_ed25519: your private key. Never share it, paste it anywhere, or commit it to Git. If it leaks, remove it from every server and make a new one.~/.ssh/id_ed25519.pub: your public key. Safe to share. It's useless without the private key.
Copy the key to the server (manual way)
Doing it by hand once shows you what's going on. Print your public key and copy it:
cat ~/.ssh/id_ed25519.pub
Log in to the server with your password, then:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
Paste the key on its own line, save, then:
chmod 600 ~/.ssh/authorized_keys
authorized_keys holds the public keys allowed to log in as that user, one per line.
Disconnect and connect again. You'll be asked for the key's passphrase instead of the account password. On a Linux desktop like GNOME, it shows up as a popup:
If you tick "Automatically unlock this key whenever I'm logged in," you won't be asked again while you're logged in to your desktop.
Copy the key to the server (easy way)
ssh-copy-id -i ~/.ssh/id_ed25519.pub furi@203.0.113.10
It asks for your password once, then creates ~/.ssh and authorized_keys on the server, adds your key, and sets the permissions. This is the same as the manual steps above.
Debug with -v
To see what SSH is doing during a connection, add -v:
ssh -v myserver
It shows the IP and port, which keys it tries, and which one the server accepts. Use -vvv for more detail.
Managing SSH Keys
One key per client
If you work with several clients, use a separate key for each. If one key is stolen, only that client is affected.
ssh-keygen -t ed25519 -C "acme" -f ~/.ssh/acme_id_ed25519
-t ed25519: the key type. More secure than RSA and much shorter.-C "acme": a comment. By default it'suser@hostname, which doesn't tell you what the key is for. Once the key is in a server'sauthorized_keys, the comment is the only way to tell keys apart.-f: the file name. I useclient_id_type.
Always set a passphrase on client keys.
The public key looks like this:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... acme
Copy it to the server:
ssh-copy-id -i ~/.ssh/acme_id_ed25519.pub furi@203.0.113.10
Choose which key to use
With several keys, SSH tries them one by one. Servers only allow a few attempts, so you can get Too many authentication failures before it reaches the right key.
Point to a specific private key with -i:
ssh -i ~/.ssh/acme_id_ed25519 furi@203.0.113.10
Or set it in your config:
Host acme
HostName 203.0.113.10
User furi
IdentityFile ~/.ssh/acme_id_ed25519
IdentitiesOnly yes
IdentitiesOnly yes makes SSH use only that key for this host.
ssh-agent
ssh-agent keeps your unlocked key in memory, so you type the passphrase once instead of on every connection.
Desktop Linux usually runs an agent for you. On a server with no GUI, such as a bastion host (a server you connect to first and then jump to others from), you have to start it yourself.
Check if it's running:
ps aux | grep ssh-agent
If only the grep line shows up, it's not running. Start it:
eval "$(ssh-agent)"
Add your private key:
ssh-add ~/.ssh/acme_id_ed25519
Enter the passphrase once. Connections now work without asking, until you close that terminal.
List loaded keys:
ssh-add -l
Configuring the SSH Server
Now the server side.
The sshd service
sudo systemctl status ssh
On Debian and Ubuntu the service is called ssh. On Fedora, RHEL, and most others it's sshd.
It should be active (running) and enabled (starts at boot). Common commands:
sudo systemctl restart ssh
sudo systemctl stop ssh
sudo systemctl start ssh
sudo systemctl enable ssh
Stopping or restarting sshd doesn't close sessions that are already open. But once you disconnect, you can't get back in until it's running again, so be careful with stop on a remote machine.
Host keys
The ssh_host_* files in /etc/ssh are the server's host keys, the ones behind the fingerprint you accepted earlier. Don't delete them unless you mean to replace them.
If you clone a server from an image, the clones share the same host keys. Clients then see the "same" server on different IPs and show man-in-the-middle warnings. Regenerate them on each clone:
sudo rm /etc/ssh/ssh_host_*
sudo ssh-keygen -A
sudo systemctl restart ssh
/etc/ssh also has two similarly named files:
ssh_config: system-wide client settings. Your~/.ssh/configoverrides it.sshd_config: server settings. This is the one we'll edit.
Edit sshd_config
sudo nano /etc/ssh/sshd_config
Lines starting with # are commented out and show the default. These are the three settings that matter most:
Port
Port 2222
Disable root login. Attackers try root first. Only set this once your normal sudo user can log in over SSH.
PermitRootLogin no
Disable password login. This is the most important change. Only set it after you've confirmed key login works.
PasswordAuthentication no
Many distros also load files from /etc/ssh/sshd_config.d/, and SSH uses the first value it finds. On Ubuntu cloud images, 50-cloudimg-settings.conf often sets PasswordAuthentication yes and overrides your change. Check:
sudo grep -ri passwordauthentication /etc/ssh/sshd_config.d/
See the settings the server will actually use:
sudo sshd -T | grep -Ei 'port|permitrootlogin|passwordauthentication'
Apply the changes safely
If you changed the port and use a firewall, allow the new port first:
sudo ufw allow 2222/tcp
Check the config for errors (no output means it's fine), then restart:
sudo sshd -t
sudo systemctl restart ssh
On Ubuntu 22.10 and newer, the port is controlled by ssh.socket. If the port doesn't change, run:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
Don't close your current session. Open a new terminal and test:
ssh -p 2222 furi@203.0.113.10
Without -p, SSH uses port 22 and you'll get Connection refused. If the login works, you can close the old session. Update the Port in your ~/.ssh/config too.
Changing the port won't stop someone targeting your server, since a port scan finds it in minutes. It does cut down the bots that only scan port 22, which keeps your logs cleaner. Disabling passwords is what actually protects you.
Test it
From a machine without your key:
furi@203.0.113.10: Permission denied (publickey).
No password prompt at all. Without a valid key, there's nothing to try.
SSH From Anywhere With Tailscale
So far we've assumed the server is reachable on the internet. But what if it's a machine at home or in an office, behind a router, with no public IP? You'd need port forwarding and you'd have to expose SSH to the internet.
Tailscale avoids that. It's a VPN built on WireGuard that connects your devices (laptop, phone, servers) into a private network, called a tailnet. Each device gets a stable 100.x.y.z address, and it works behind NAT without opening any ports. The free personal plan is enough for most people.
Install Tailscale on the server
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up
tailscale up prints a login link. Open it and sign in. Then get the server's Tailscale IP:
tailscale ip -4
Install it on your devices
Install the Tailscale app on your laptop and phone and sign in with the same account. Check that everything is connected:
tailscale status
Connect over SSH
Use the Tailscale IP like any other address:
ssh furi@100.101.102.103
With MagicDNS (on by default for new tailnets), you can use the machine name instead:
ssh furi@homeserver
Add it to your config:
Host home
HostName homeserver
User furi
IdentityFile ~/.ssh/id_ed25519
This works from anywhere your laptop has internet. On a phone, install Tailscale plus any SSH client app and connect to the same address.
Close port 22 to the internet (optional)
Once SSH over Tailscale works, you can allow SSH only through the Tailscale interface. With UFW:
sudo ufw allow in on tailscale0 to any port 22 proto tcp
sudo ufw delete allow 22/tcp
If you added the rule with sudo ufw allow OpenSSH, remove it with sudo ufw delete allow OpenSSH instead.
Test from a new terminal over Tailscale before closing your current session. If you changed the SSH port, use that port instead of 22.
Tailscale SSH (optional)
Tailscale can also handle SSH login itself, using your Tailscale identity instead of SSH keys:
sudo tailscale up --ssh
Who can log in as which user is then controlled by access rules in the Tailscale admin console. It's convenient, but it ties your server access to your Tailscale account. Regular SSH keys over Tailscale work fine if you'd rather keep them separate.
Troubleshooting SSH
1. Check the network
Go back to the network path diagram. SSH only works if traffic can get from your machine to sshd on the right port.
The error tells you how far it got:
- Connection timed out: a firewall is dropping the traffic, or nothing is at that address. Firewalls usually don't reply to blocked traffic, so it just times out.
- Connection refused: you reached the machine, but nothing is listening on that port.
sshdis stopped or on a different port. - Permission denied: the network is fine. The problem is the login: password, key, or permissions.
If you use Tailscale, also check tailscale status on both ends.
2. Check permissions
SSH refuses keys if the files can be read or changed by other users, and it doesn't always tell you that's the reason.
ls -la ~ | grep .ssh
ls -l ~/.ssh
| File | Permissions |
|---|---|
~/.ssh/ |
700 (drwx------) |
| Private keys | 600 (-rw-------) |
Public keys (.pub) |
644 (-rw-r--r--) |
~/.ssh/config |
600 |
~/.ssh/authorized_keys (server) |
600 |
Fix them:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519 ~/.ssh/config
chmod 644 ~/.ssh/id_ed25519.pub
On the server:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Check both ends.
3. Read the logs
Follow the SSH log on the server while someone tries to connect. The reason almost always shows up there.
On systemd distros:
sudo journalctl -fu ssh # Debian, Ubuntu
sudo journalctl -fu sshd # most others
-u selects the service and -f follows new lines. Or use the log file:
sudo tail -f /var/log/auth.log # Debian, Ubuntu
sudo tail -f /var/log/secure # Fedora, RHEL
A wrong password looks like this:
Failed password for furi from 198.51.100.7 port 51234 ssh2
After a few failures, the server closes the connection. Key and permission problems show up here too.
Checklist
- Does the connection reach the server? (timed out vs. refused)
- Is
sshdrunning? (systemctl status ssh) - Right port, user, and key? (
ssh -v) - Correct permissions on both ends?
- What does the server log say?
SSH Cheat Sheet
| Task | Command |
|---|---|
| Check the client | which ssh |
| Install the client (Debian/Ubuntu) | sudo apt install openssh-client |
| Connect | ssh user@server-ip |
| Connect on another port | ssh -p 2222 user@server-ip |
| Connect with a specific key | ssh -i ~/.ssh/keyname user@server-ip |
| Verbose output | ssh -v user@server-ip |
| Connect using a config entry | ssh myserver |
| Remove an old fingerprint | ssh-keygen -R server-ip |
| Generate a key | ssh-keygen -t ed25519 -C "comment" |
| Generate a named key | ssh-keygen -t ed25519 -C "acme" -f ~/.ssh/acme_id_ed25519 |
| Copy a key to a server | ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip |
| Start the agent | eval "$(ssh-agent)" |
| Add a key to the agent | ssh-add ~/.ssh/id_ed25519 |
| List agent keys | ssh-add -l |
| Service status | sudo systemctl status ssh (or sshd) |
| Restart the service | sudo systemctl restart ssh (or sshd) |
| Edit server config | sudo nano /etc/ssh/sshd_config |
| Test server config | sudo sshd -t |
| Show active server config | sudo sshd -T |
| Regenerate host keys | sudo ssh-keygen -A |
| Install Tailscale | curl -fsSL https://tailscale.com/install.sh | sh |
| Join the tailnet | sudo tailscale up |
| Show Tailscale IP | tailscale ip -4 |
| Show tailnet devices | tailscale status |
| Follow SSH logs | sudo journalctl -fu ssh (or sshd) |
Fix .ssh permissions |
chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_ed25519 |
Wrapping Up
The main points:
- Use SSH keys with a passphrase, and
ssh-agentso you only type it once. - Disable password login on every server once key login works.
- Use Tailscale to reach machines that aren't on the public internet, without opening ports.
- When something breaks, check the network, then permissions, then the logs.
The best way to learn this is to set up a test VM, try each command, and break things on purpose to see what the logs show.
SSH keys are public-key cryptography in action. To see how the same idea keeps every website secure, read HTTPS, TLS and Certificates Explained.


