I can't ssh with root or setup keys properly

September 30, 2014 5.2k views

I was trying to following this tutorial about ssh keys and security, but the server kept me asking password for the user.

So I tried to do it with the root,(to follow the tutorial integrally) Since I had disable the root for remote login (PermitRootlogin No), I tried to re-enable it but it doesn't me allow to ssh with the root. so I have this problem and I don't know were to look (logs or errors).

I can sudo and su - without problems and I login trough ssh with a created user. I did "sudo systemctl reload sshd.service" and rebooted after changing (PermitRootlogin yes) with no avail, I simply can't ssh with root nor I can setup the keys for the user right.

laptop:~ username$ ssh -v root@XXX.XXX.XXX.XXX
OpenSSH_5.2p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /etc/ssh_config
debug1: Connecting to XXX.XXX.XXX.XXX[XXX.XXX.XXX.XXX] port 22.
debug1: Connection established.
debug1: identity file /Users/username/.ssh/identity type -1
debug1: identity file /Users/username/.ssh/id_rsa type 1
debug1: identity file /Users/username/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.4
debug1: match: OpenSSH_6.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.2
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'XXX.XXX.XXX.XXX' is known and matches the RSA host key.
debug1: Found key in /Users/username/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/username/.ssh/identity
debug1: Offering public key: /Users/username/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /Users/username/.ssh/id_dsa
debug1: Next authentication method: password
root@XXX.XXX.XXX.XXX's password: 
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password

1 comment
  • *I still can't login with the root or login with the ssh key.

    Found the problem with the user ssh key,

    debug1: Authentication succeeded (password).
    debug1: channel 0: new [client-session]
    debug1: Requesting no-more-sessions@openssh.com
    debug1: Entering interactive session.
    debug1: Remote: Ignored authorized keys: bad ownership or modes for file /home/username/.ssh/authorized_keys


    chmod g-w /home/your_user
    chmod 700 /home/your_user/.ssh
    chmod 600 /home/your_user/.ssh/authorized_keys


2 Answers

The log indicates that public key authentication failed, so it tried your password next.

Sudo has literally nothing to do with SSH, so there's no point in changing its config to resolve this.

As for permit root login, there's no difference in how public key authentication works for users and root, other than where sshd will look for the authorized_keys file (their home directories)

So you need to go look in the authentication logs on the server, /var/log/auth.log usually, and read the error there that will indicate why log in failed.

  • I tailed,

    sudo tail -f /var/log/secure

    and found the solution,

    Sep 30 16:14:57 hostname sshd[2107]: User root from xxx.xxx.xxx.xxx not allowed because not listed in AllowUsers
    Sep 30 16:14:57 hostname sshd[2107]: input_userauth_request: invalid user root [preauth]

    So I had to change the permissions of the file ) and folders first and then, since I added a list of login permissions to (/etc/ssh/sshd_config -- "AllowUsers myuser root") I had to add the root too. (now I can remove it since it is working)

  • Yes, AllowUsers needs to be set to the list of users allowed to log in via SSH. There are also other similar clauses like AllowGroups.

Have another answer? Share your knowledge.