Green Huang's homepage
Homelab/Linux

How to Set Up SSH on Linux (and Troubleshoot Common Errors)

How to Set Up SSH on Linux (and Troubleshoot Common Errors)

Edit on GitHub

Step 1 β€” Install and Start the SSH Server

sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh

sudo systemctl status ssh

πŸ”‘ Step 2 β€” Generate SSH Keys (Client)

On your local machine, generate a key pair:

ssh-keygen -t ed25519 -C "[email protected]"

This creates:

  • ~/.ssh/id_ed25519 β€” your private key

  • ~/.ssh/id_ed25519.pub β€” your public key

Never share your private key; it stays on your local device.

Step 3 β€” Copy Your Public Key to the Server

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
# Paste the contents of your `id_ed25519.pub` file into `authorized_keys`, save, and exit.

chmod 600 ~/.ssh/authorized_keys
systemctl restart ssh

Step 4 β€” SSH Client Login

ssh username@server_ip

troubleshoot

Every SSH server has a host key that uniquely identifies it. The first time you connect to a new server, your SSH client saves the server’s host key in the known_hosts file in your home directory.

If you reinstall the server or regenerate host keys, the saved key on your local machine will no longer match the server’s current one. When this happens, SSH shows this warning:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Host key for 192.168.x.x has changed and you have requested strict checking.
Host key verification failed.

If you reinstalled the host or rebuilt it, you can be confident the host key actually changed β€” not a security breach.

Remove the old key from your local known_hosts On most systems (Linux/macOS/Windows PowerShell):

ssh-keygen -R server_ip_or_hostname

the retry the connection

ssh username@server_ip

# SSH will prompt:
The authenticity of host 'server_ip (server_ip)' can't be established.
Are you sure you want to continue connecting (yes/no)?