C.W.K.
Stream
Lesson 05 of 12 · published

Installing Your Public Key

~15 min · ssh-copy-id, authorized-keys, permissions

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

Getting the public key onto the server

For key-based auth to work, your public key needs to be in the server's ~/.ssh/authorized_keys file (one key per line). Three ways to do that — easy, manual, and direct edit.

The easy way — ssh-copy-id

If ssh-copy-id is available (Linux yes, macOS no by default but brew install ssh-copy-id), it handles everything: connects via password, creates ~/.ssh/ if needed, appends your key, sets permissions correctly. One command, done.

The manual way

If ssh-copy-id isn't around, pipe your public key over an SSH session that creates the directory and appends. Or paste it by hand into the server's authorized_keys.

Permissions matter — silently

SSH refuses to use authorized_keys (or even the .ssh directory) if permissions are too loose. The failure mode is silent — auth just falls back to password without telling you why. Whenever key auth mysteriously stops working, check permissions first.

Code

Three ways to install your public key·bash
# Easy — ssh-copy-id (install with `brew install ssh-copy-id` on macOS)
ssh-copy-id you_username@192.168.1.100
ssh-copy-id -i ~/.ssh/id_ed25519.pub you_username@192.168.1.100

# Manual — pipe through ssh
cat ~/.ssh/id_ed25519.pub | ssh you_username@192.168.1.100 \
  'mkdir -p ~/.ssh && chmod 700 ~/.ssh && \
   cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

# Direct — copy your key, ssh in, paste it into authorized_keys
cat ~/.ssh/id_ed25519.pub   # copy the output
ssh you_username@192.168.1.100
# Then on the server:
echo 'PASTE_THE_KEY_HERE' >> ~/.ssh/authorized_keys
Fix the permissions·bash
# On the server side
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# Home directory itself must not be group-writable
chmod o-w,g-w ~

# Test from a fresh terminal
ssh you_username@192.168.1.100

External links

Exercise

Pick a target machine on your LAN. Use any of the three methods to install your public key, then test with a fresh terminal: ssh you_username@target should let you in with no password prompt. Bonus: deliberately break it — chmod 644 ~/.ssh/authorized_keys on the target — try to SSH again, watch it fall back to password (run with -v to see SSH refuse the key), then chmod 600 to fix.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.