Forums

SSH public key is being rejected

I just upgraded from the free account to "Hacker". The current issue I'm dealing with is getting SSH to work using public key authentication, i.e. without a password. I've done this successfully many times on many other systems.

I have an existing SSH public/private key pair. I successfully did the ssh-copy-id command and confirmed that ~/.ssh/authorized_keys (on PythonAnywhere) has the public key listed. I also checked the permissions of ~/.ssh and contained files.

Performed on my local computer:

$ ssh-copy-id coops222@ssh.pythonanywhere.com                                                                                                     ─╯
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
coops222@ssh.pythonanywhere.com's password:

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'coops222@ssh.pythonanywhere.com'"
and check to make sure that only the key(s) you wanted were added.

Performed in a PythonAnywhere Bash console:

18:55 ~ $ ls -ld ~/.ssh
drwx------ 2 coops222 registered_users 4096 Aug 13 18:53 /home/coops222/.ssh                                                                                        
18:55 ~ $ ls -l ~/.ssh                                                                                                                                              
total 12                                                                                                                                                            
-rw------- 1 coops222 registered_users  413 Aug 13 19:20 authorized_keys                                                                                            
-rw------- 1 coops222 registered_users 2622 Aug 11 21:40 id_rsa                                                                                                     
-rw-r--r-- 1 coops222 registered_users  582 Aug 11 21:40 id_rsa.pub                                                                                                 
-rw-r--r-- 1 coops222 registered_users  666 Aug 11 21:45 known_hosts

I also checked that ~/.ssh/authorized_keys contains the public key.

But when I try to connect it prompts for the password:

$ ssh -vvv coops222@ssh.pythonanywhere.com
<removed some of the debug "noise">
<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred 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 public key: /Users/steve/.ssh/id_rsa RSA SHA256:tVLn5uPhftU4LP/1t2YDZiO+KefmiMgDoAFfMYPnrlM
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /Users/steve/.ssh/id_ecdsa
debug3: no such identity: /Users/steve/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /Users/steve/.ssh/id_ecdsa_sk
debug3: no such identity: /Users/steve/.ssh/id_ecdsa_sk: No such file or directory
debug1: Trying private key: /Users/steve/.ssh/id_ed25519
debug3: no such identity: /Users/steve/.ssh/id_ed25519: No such file or directory
debug1: Trying private key: /Users/steve/.ssh/id_ed25519_sk
debug3: no such identity: /Users/steve/.ssh/id_ed25519_sk: No such file or directory
debug1: Trying private key: /Users/steve/.ssh/id_xmss
debug3: no such identity: /Users/steve/.ssh/id_xmss: No such file or directory
debug1: Trying private key: /Users/steve/.ssh/id_dsa
debug3: no such identity: /Users/steve/.ssh/id_dsa: No such file or directory
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
coops222@ssh.pythonanywhere.com's password:

So it tries to use the key, but fails with response 51 (?).

I noticed that /etc/sshd_config has "#PubkeyAuthentication yes" commented out. Does that mean it doesn't support key-based authentication in my VM? If so, how can it be enabled?

Sorry for the verbose post, but I'm trying to provide complete information.

Thanks in advance!

Could you try regenerating your key pair?

I tried regenerating. Now I'm using a separate key file, ~/.ssh/id_rsa_pythonanywhere because my default key pair is used elsewhere.

$ ssh-keygen -C coops222@ssh.pythonanywhere.com -f ~/.ssh/id_rsa_pythonanywhere -t rsa -b 2048

Generating public/private rsa key pair.
/Users/steve/.ssh/id_rsa_pythonanywhere already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/steve/.ssh/id_rsa_pythonanywhere
Your public key has been saved in /Users/steve/.ssh/id_rsa_pythonanywhere.pub
The key fingerprint is:
SHA256:ophdNxGoD9OI6erlODHQtbIgYX4Wm3hxTuXJ8iHYvis coops222@ssh.pythonanywhere.com
The key's randomart image is:
+---[RSA 2048]----+
|      .o.        |
|.. o+o+ ..       |
|oo.=OO =.        |
|+o=*B.= ..       |
|oo+o =o.S        |
| oo+ ooo .       |
| .=.o.           |
|..+E  .          |
|.o....           |
+----[SHA256]-----+ 
$ ssh-copy-id -i ~/.ssh/id_rsa_pythonanywhere coops222@ssh.pythonanywhere.com

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/steve/.ssh/id_rsa_pythonanywhere.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
coops222@ssh.pythonanywhere.com's password:

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'coops222@ssh.pythonanywhere.com'"
and check to make sure that only the key(s) you wanted were added.
$ ssh -i ~/.ssh/id_rsa_pythonanywhere coops222@ssh.pythonanywhere.com

<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
coops222@ssh.pythonanywhere.com's password:
19:34 ~ $

Note that when I run ssh with `-vvv- I see the sam attempt to use the key, but it fails.

$ ssh -vvv -i ~/.ssh/id_rsa_pythonanywhere coops222@ssh.pythonanywhere.com
...
<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred 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 public key: /Users/steve/.ssh/id_rsa_pythonanywhere RSA SHA256:ophdNxGoD9OI6erlODHQtbIgYX4Wm3hxTuXJ8iHYvis explicit agent
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: /Users/steve/.ssh/id_rsa RSA SHA256:tVLn5uPhftU4LP/1t2YDZiO+KefmiMgDoAFfMYPnrlM agent
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
coops222@ssh.pythonanywhere.com's password:

Ok. Could you try now?