Search code examples
sshkeylocalhost

setting SSH access to localhost


I am trying to setup ssh access to my own local machine.

I have created the id_rsa key using ssh-keygen. I added id_rsa.pub to authroized_keys in .ssh/ I made sure the permission of authorized_keys is 640 I enabled public-key authentication in sshd_config & restarted ssh

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile %u/.ssh/authorized_keys

However I am unable to login to ssh.

The error I recieved is as follows

debug3: load_hostkeys: loaded 1 keys
debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /home/rahul/.ssh/known_hosts:6
debug1: ssh_ecdsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug2: key: /home/rahul/.ssh/id_rsa (0x7fa12de58e70),
debug2: key: /home/rahul/.ssh/gitHubKey ((nil)), explicit
debug2: key: /home/rahul/.ssh/id_rsa_buhlServer (0x7fa12de59060), explicit
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/rahul/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/rahul/.ssh/gitHubKey
no such identity: /home/rahul/.ssh/gitHubKey: No such file or directory
debug1: Offering RSA public key: /home/rahul/.ssh/id_rsa_buhlServer
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

Solution

  • Have you seen auth.log (or system.log, secure.log, ect where opensshd write its logs) possibly the problem is that .ssh/authorized_keys must have 600 not 640.

    Example: https://help.ubuntu.com/community/SSH/OpenSSH/Keys

    chmod go-w ~/
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    

    Hope it helps.

    Edit:

    We will uninstall openssh-server and purge configuration files:

    (root)# apt-get remove --purge openssh-server
    

    Now we will install again with default settings:

    (root)# apt-get install openssh-server
    

    Now we will generate our private/public key:

    (rahul)$ ssh-keygen
    

    Now we will copy the key in our local user, you will need to write your password.

    (rahul)$ ssh-copy-id rahul@localhost
    

    Try to connect now with:

    (rahul)$ ssh rahul@localhost
    

    Now it may work.