ยท 1 min read
SSH keys for a non-root user
Generate a key, install it for a regular account, prove it works, then turn passwords off.
Password login works, but a key is better: nothing reusable travels over the wire, and brute-force attempts become noise in the log.
1. Create the key on your machine
ssh-keygen -t ed25519 -C "khori@laptop"
Press Enter to accept the default path (~/.ssh/id_ed25519) and set a passphrase. The comment is just a label; use something that tells you which machine the key belongs to.
2. Install the public key
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
This logs in once with the password, creates ~/.ssh if needed, and appends the key to ~/.ssh/authorized_keys with the right permissions. If your client has no ssh-copy-id, paste the contents of the .pub file into that path by hand:
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... khori@laptop" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
SSH refuses to use authorized_keys when the home directory, .ssh, or the file itself is writable by other users, so the permissions are part of the install, not cosmetics.
3. Prove the key works
ssh -o PreferredAuthentications=publickey [email protected]
Forcing the method means a password fallback cannot quietly make a broken key look fine.
4. Then disable passwords
PasswordAuthentication no
Restart sshd, open a second session, and confirm:
sudo sshd -T | grep -i passwordauthentication
Only after that should you close the session you kept open.
Source: Vultr โ using an SSH key to log in as a non-root user.