Theme editor

Solution Linux SSH: Permission denied (publickey) after moving ~/.ssh (Fix Permissions & SELinux)

  • Thread starter Thread starter Syns
  • Start date Start date
  • Views 161

Syns

First Liner
User
Thread owner
After copying my SSH keys to a new user, I get “Permission denied (publickey)”. What am I missing?
 
Solution
Fix ownership/permissions and contexts so sshd trusts your keys.
Bash:
# as the target user
mkdir -p ~/.ssh && chmod 700 ~/.ssh
mv id_rsa id_rsa.pub authorized_keys ~/.ssh/
chmod 600 ~/.ssh/id_rsa ~/.ssh/authorized_keys
chown -R $USER:$USER ~/.ssh

if SELinux is enforcing (RHEL/CentOS/Fedora):

restorecon -Rv ~/.ssh

Check
Bash:
ssh -v user@server

If still failing, ensure [/etc/ssh/sshd_config] has PubkeyAuthentication yes, then:
Bash:
sudo systemctl reload sshd
Fix ownership/permissions and contexts so sshd trusts your keys.
Bash:
# as the target user
mkdir -p ~/.ssh && chmod 700 ~/.ssh
mv id_rsa id_rsa.pub authorized_keys ~/.ssh/
chmod 600 ~/.ssh/id_rsa ~/.ssh/authorized_keys
chown -R $USER:$USER ~/.ssh

if SELinux is enforcing (RHEL/CentOS/Fedora):

restorecon -Rv ~/.ssh

Check
Bash:
ssh -v user@server

If still failing, ensure [/etc/ssh/sshd_config] has PubkeyAuthentication yes, then:
Bash:
sudo systemctl reload sshd
 
Solution
Back
Top