Skip to content

Instantly share code, notes, and snippets.

@paveltretyakovru
Last active February 18, 2023 15:30
Show Gist options
  • Save paveltretyakovru/8736bc3bd563bc3b918431195f78053e to your computer and use it in GitHub Desktop.
Save paveltretyakovru/8736bc3bd563bc3b918431195f78053e to your computer and use it in GitHub Desktop.
Linux SSH public key authentication
[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa;
ssh <username>@<remote_machine> 'mkdir -p ~/.ssh; chmod 700 ~/.ssh'
cat ~/.ssh/id_rsa.pub | ssh <username>@<remote_machine> 'cat >> ~/.ssh/authorized_keys'

Linux SSH public key authentication

Server side

  1. Edit etc/ssh/sshd_config:
  • Uncomment PubkeyAuthentication yes

  • Setup auth key values path with AuthorizedKeysFile .ssh/authorized_keys

  • Set next configs to disable default password authentification (if you need it)

ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no
  1. Create .ssh folder: mkdir -p ~/.ssh
  2. Applay access ruels to the folder: chmod 700 .ssh
  3. Reload ssh server to apply changes sudo /etc/init.d/ssh force-reload

Client side

  1. Gereate and add to ssh-agent SSH key
  2. Copy public key value to authorized_keys:
cat ~/.ssh/id_rsa.pub | ssh <username>@<remote_machine> 'cat >> ~/.ssh/authorized_keys'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment