Skip to content

Permission denied (publickey) with SSH.NET - Can connect via FileZilla or Powershell. #956

Closed

Description

@wfrx

I'm trying to extend our SFTP capability to allow connection via key-authentication.

The code examples are very straightforward (thanks for that) and I'm pretty sure I've done it right:
connectionInfo = New ConnectionInfo(e.Host, e.Port, e.Username, GetAuthenticationMethods(e))
with GetAuthenticationMethods(e) giving me an array of AuthenticationMethods, depending on the provided information:

[...]
authMethod.Add(New PasswordAuthenticationMethod(e.Username, e.Password))
[...]
If e.FilePassphrase = "" Then
  authMethod.Add(New PrivateKeyAuthenticationMethod(e.Username, New PrivateKeyFile(keyFile)))
ElseIf e.FilePassphrase <> "" Then
  authMethod.Add(New PrivateKeyAuthenticationMethod(e.Username, New PrivateKeyFile(keyFile, e.FilePassphrase)))
End If
[...]
m_Client = New SftpClient(connectionInfo)
m_Client.Connect()

I have a Server set up and generated keys. Originally, I had a ssh-rsa key, giving me a openssh key type: ssh-rsa is not supported Error while trying to instantiate the PrivateKeyAuthenticationMethod. (afaict this should be supported but is not yet in the NuGet package, is there any ETA on when this will be available?)

I converted this key to PEM, which gets me one step further but now I get a Permission denied (publickey). Error.

I can connect to the server without issues using either the PEM or the ssh-rsa key over FileZilla or ssh on console.

Activity

  1. gregolsky commented on May 5, 2022

    @gregolsky

    We are experiencing this issue when using latest Ubuntu release Jammy Jellyfish.
    @wfrx Are you using Ubuntu 22.04?

    Related to ssh-rsa deprecation?
    https://discourse.ubuntu.com/t/jammy-jellyfish-release-notes/24668
    https://bugs.launchpad.net/ubuntu/+source/openssh/+bug/1961833

  2. wfrx commented on May 5, 2022

    @wfrx
    Author

    @wfrx Are you using Ubuntu 22.04?

    Yes, but as I wrote I can connect via ssh on console:

    >ssh -vvvv -i .\#keyfile# #user#@#host#
    [...]
    debug1: Next authentication method: publickey
    debug1: Trying private key: .\\###keyfile###
    debug3: sign_and_send_pubkey: RSA SHA256:###sha###
    debug3: sign_and_send_pubkey: signing using rsa-sha2-512
    debug3: send packet: type 50
    debug2: we sent a publickey packet, wait for reply
    debug3: receive packet: type 52
    debug1: Authentication succeeded (publickey).
    [...]
    Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-27-generic x86_64)
    
  3. gregolsky commented on May 20, 2022

    @gregolsky

    The sshd on Ubuntu 22.04 log shows this error when trying to connect with SSH.NET:

    userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]
    Connection closed by authenticating user ubuntu XX.XX.XX.XX port 50358 [preauth]
    

    Does SSH.NET have an option to use another algorithm for that?

  4. gregolsky commented on Jun 2, 2022

    @gregolsky

    I think this can serve as a workaround: #825 (comment)

  5. wfrx commented on Jun 21, 2022

    @wfrx
    Author

    I think this can serve as a workaround: #825 (comment)

    This actually seems to work using the PEM key. I'll have to do more tests to confirm, but at least I no longer get the "permission denied" error.

  6. SibghatU commented on May 10, 2023

    @SibghatU

    Can someone please refer to my problem and help me please, here is the question: on stackoverflow

    I am getting exception Renci.SshNet.Common.SshAuthenticationException: 'Permission denied (publickey).'

    The host is acceesible via LAN, the ip is 192.168.A.XXX and the client IP is 192.168.B.xxx

    Only this particular system has the proble, I tried same key pair on another machine, that worked well

  7. added this to the 2023.0.0 milestone on Sep 18, 2023
  8. WojciechNagorski commented on Oct 10, 2023

    @WojciechNagorski
    Collaborator

    Version 2023.0.0 has been published https://www.nuget.org/packages/SSH.NET/2023.0.0

  9. blounsbury-usbr commented on Dec 20, 2023

    @blounsbury-usbr

    I'm getting this error as well using 2023.0.0. I just generated a key pair using:

    openssl genrsa -out private_rsa 2048
    openssl rsa -in private_rsa -pubout -out public_rsa

    Is this not supported, or how do I need to do this? Thanks!

    image

  10. Rob-Hague commented on Dec 20, 2023

    @Rob-Hague
    Collaborator

    @blounsbury-usbr has the key you just generated been installed into the server?

  11. blounsbury-usbr commented on Dec 20, 2023

    @blounsbury-usbr

    @Rob-Hague It has. I copied it to .ssh/public_key, I could login by console. Then I decided to put it into .ssh/authorized_keys. Could still login by console or MobaXterm, haha, no problems. It is rw for username:username and world readable. Confused...

  12. Rob-Hague commented on Dec 20, 2023

    @Rob-Hague
    Collaborator

    Some issues have been reported in #1233 but since finding a potential workaround, I have not had any further clue on the cause. Does it seem related?

  13. blounsbury-usbr commented on Dec 20, 2023

    @blounsbury-usbr

    Hmm ... I haven't read through it all, it may be similar. I saw you said look at the logs, good idea! I see this in the log file:

    Dec 20 13:56:48 server sshd[122408]: Authentication refused: bad ownership or modes for file /home/username/.ssh/authorized_keys

    Do you happen to know the "proper" ownership or modes to set?

  14. Rob-Hague commented on Dec 20, 2023

    @Rob-Hague
    Collaborator

    Based on https://man.openbsd.org/sshd.8#FILES, I think you need something like

    chmod 644 /home/username/.ssh/authorized_keys

  15. blounsbury-usbr commented on Dec 20, 2023

    @blounsbury-usbr

    Hmm...

    Yeah, I tried following a couple advice posts and did:
    https://superuser.com/questions/215504/permissions-on-private-key-in-ssh-folder

    chmod go-w /home/user
    chmod 700 /home/user/.ssh
    chmod 644 /home/user/.ssh/authorized_keys
    systemctl restart sshd

    Same response. I realized I hadn't tried a Command Prompt. It's asking for a username. Thinking sshd needs set to not allow passwords, maybe...

  16. blounsbury-usbr commented on Dec 20, 2023

    @blounsbury-usbr

    Oh my gosh, hours researching and trying ... the solution is that the public key needs to be formatted as one line like this:

    ssh-rsa abcdefghijklmnopqrstuvwxyzetcetcetc rsa-key-20121022

    https://askubuntu.com/questions/204400/ssh-public-key-no-supported-authentication-methods-available-server-sent-publ

  17. vsvsav commented on Feb 27, 2024

    @vsvsav

    It works with converted to OpenSSH key :
    -----BEGIN OPENSSH PRIVATE KEY-----

    It does not work for:
    PuTTY-User-Key-File-3: ssh-rsa
    Encryption: none
    Comment: rsa-key-20230808
    Public-Lines: 6
    ...
    Private-Lines: 14
    ...==
    Private-MAC:...

    PS
    v 2024.0.0

  18. hinmanjp commented on Feb 29, 2024

    @hinmanjp

    In addition to all of the comments about proper formatting & permissions of the ssh keys, when connecting to an Ubuntu 22.04 server (and possibly others), there is a need to add the following line to the Ubuntu sshd config, and then restart the sshd service.
    PubkeyAcceptedAlgorithms +ssh-rsa
    Stack Exchange answer

    This can be done as a one liner
    echo "PubkeyAcceptedAlgorithms +ssh-rsa" >> /etc/ssh/sshd_config.d/allow_ssh-rsa.conf ; systemctl restart sshd

    Additionally, it should be noted that if connecting to a remote server under Visual Studio for debugging, Visual Studio does not currently appear to accept Elliptic Curve (ed25519) keys, and it does not allow the use of keys that are not password protected. The method for generating a ssh key that VS will accept (as provided by MS Support) is:
    ssh-keygen -t rsa -b 4096 -m PEM -f <key_path_and_name_goes_here>

    Drop the -f option if you want it to create / replace your default ssh key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions