How to Set Up SSH Key Login and Disable Password Authentication

Linux 5 min readUpdated Nov 16, 2026
4 views0 comments

Any server with SSH open to the internet is attacked constantly by bots guessing passwords. Key-based login is both more convenient and far more secure. This guide sets it up step by step, with a safety net so you do not lock yourself out.

Step 1: Create a key pair on your computer

ssh-keygen -t ed25519 -C "you@example.com"

Accept the default location and set a passphrase. You get two files:

  • ~/.ssh/id_ed25519: the private key. Never share it.
  • ~/.ssh/id_ed25519.pub: the public key. This is the one you install on servers.

This works the same in Linux, macOS and Windows PowerShell (OpenSSH is built in to current Windows).

Ed25519 is the modern default: short keys, fast, and secure. Very old servers may not understand it. For those, create an RSA key instead with ssh-keygen -t rsa -b 4096.

To see the fingerprint of a key (a short identifier you can compare on two machines), run:

ssh-keygen -lf ~/.ssh/id_ed25519.pub

It prints the key size, a SHA256:... fingerprint, your comment and the key type, for example 256 SHA256:... you@example.com (ED25519).

Step 2: Install the public key on the server

The easy way, from Linux or macOS:

ssh-copy-id user@your-server

Or manually, on the server:

mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo 'ssh-ed25519 AAAA...your public key... you@example.com' >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

The permissions matter: SSH ignores the file if it is writable by other users.

Step 3: Test the key login before changing anything

ssh user@your-server

You should get in using the key (and your passphrase), without the server password. Keep this session open. It is your safety net.

Type the passphrase only once: ssh-agent

A passphrase protects the key if someone copies the file, but typing it for every connection gets old. The ssh-agent keeps the unlocked key in memory for your session.

# Linux / macOS
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

On Windows the agent service is installed but disabled by default. In an Administrator PowerShell, enable it once, then add your key from a normal window:

Get-Service ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

ssh-add -l lists the keys the agent holds.

Save your settings in ~/.ssh/config

Instead of typing the user, address and key every time, give the server a short name. Create or edit ~/.ssh/config (on Windows: C:\Users\you\.ssh\config):

Host myserver
    HostName 203.0.113.10
    User admin
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

Now ssh myserver is enough. IdentitiesOnly yes tells SSH to offer only that key and not every key in your agent, which avoids “too many authentication failures” when you have many keys. To check how SSH reads the name without connecting, run ssh -G myserver: it prints the user, host name, port and identity file it would use.

Step 4: Turn off password login

On the server, edit the SSH daemon configuration:

sudo nano /etc/ssh/sshd_config

Set or change these lines:

PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin prohibit-password

On some distributions extra files in /etc/ssh/sshd_config.d/ override the main file. Check them with grep -r PasswordAuthentication /etc/ssh/.

Step 5: Validate and reload

sudo sshd -t                  # prints nothing if the configuration is valid
sudo systemctl reload sshd    # on Debian/Ubuntu the service is called ssh

Now open a second terminal and log in again. Only when that works, close the first one. If something went wrong, the first session lets you fix it.

Confirm that password login is really off

Do not trust the file alone. Ask the running server for the settings it actually applies:

sudo sshd -T | grep -iE 'passwordauthentication|pubkeyauthentication|permitrootlogin'

You want to see passwordauthentication no and pubkeyauthentication yes. Then test it from a client by forcing SSH not to use your key:

ssh -o PubkeyAuthentication=no user@your-server

The refusal ends with a list of the methods the server still accepts. On a server that still allows passwords it reads like Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). After your change the word password must be gone from that list.

Troubleshooting “Permission denied (publickey)”

  1. Run the client in verbose mode: ssh -v user@your-server shows which keys are offered and why they are refused. Add -vv for more.
  2. Check the user name. The key must be in the authorized_keys of the account you log in as.
  3. Check permissions on the server. They should look like this, owned by the user:
    stat -c '%a %U %n' ~/.ssh ~/.ssh/authorized_keys
    700 user /home/user/.ssh
    600 user /home/user/.ssh/authorized_keys

    The home folder must also not be writable by other users.

  4. On RHEL, AlmaLinux, Rocky and Fedora check SELinux. A wrongly labelled file can be ignored. ls -Z ~/.ssh/authorized_keys should show the type ssh_home_t. If it does not, fix it with restorecon -Rv ~/.ssh.
  5. Read the server log. On RHEL-based systems use sudo journalctl -u sshd -e. On Debian and Ubuntu look at /var/log/auth.log. It usually says exactly what was wrong, such as “bad ownership or modes”.

If you lock yourself out

This is why Step 3 says to keep a session open. If you did lose access, use your provider’s web console or rescue mode to log in locally, set PasswordAuthentication yes again, fix the key, and repeat the steps. Keeping a second administrator account with its own key, or a saved copy of your private key in a password manager, makes recovery much easier.

Extra hardening

  • Install fail2ban to ban addresses that keep failing.
  • Use a separate key per device, so you can revoke a lost laptop by deleting one line from authorized_keys. The comment at the end of each line (the -C text you chose) tells you which device it belongs to.
  • Consider moving SSH behind a VPN, or allowing it only from known addresses in your firewall.
  • Back up your private key somewhere safe. If you lose it and password login is off, you will need console access to get back in.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top