Ubuntu – sftp server throws Permission denied (publickey)

sftpsshUbuntuvsftpd

I'm setting up WordPress site on Apache server with SFTP enabled.

After configuring all the necessary in /etc/vsftpd.conf
I generated ssh keys with ssh-keygen -t rsa which produced private and public keys.

I can confirm sftp server is running by running systemctl status vsftpd:

● vsftpd.service - vsftpd FTP server
     Loaded: loaded (/lib/systemd/system/vsftpd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-06-03 11:10:44 UTC; 1h 22min ago
   Main PID: 605 (vsftpd)
      Tasks: 1 (limit: 4713)
     Memory: 1.3M
     CGroup: /system.slice/vsftpd.service
             └─605 /usr/sbin/vsftpd /etc/vsftpd.conf

Jun 03 11:10:44 blog systemd[1]: Starting vsftpd FTP server...
Jun 03 11:10:44 blog systemd[1]: Started vsftpd FTP server.

sftp -vvv newftpuser@127.0.0.1 returns:

OpenSSH_8.2p1 Ubuntu-4ubuntu0.2, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug3: /etc/ssh/ssh_config line 19: Including file /etc/ssh/ssh_config.d/50-cloudimg-settings.conf depth 0
debug1: Reading configuration data /etc/ssh/ssh_config.d/50-cloudimg-settings.conf
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 127.0.0.1 is address
debug2: ssh_connect_direct
debug1: Connecting to 127.0.0.1 [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /home/mark/.ssh/id_rsa type 0
debug1: identity file /home/mark/.ssh/id_rsa-cert type -1
debug1: identity file /home/mark/.ssh/id_dsa type -1
debug1: identity file /home/mark/.ssh/id_dsa-cert type -1
debug1: identity file /home/mark/.ssh/id_ecdsa type -1
debug1: identity file /home/mark/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/mark/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/mark/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/mark/.ssh/id_ed25519 type -1
debug1: identity file /home/mark/.ssh/id_ed25519-cert type -1
debug1: identity file /home/mark/.ssh/id_ed25519_sk type -1
debug1: identity file /home/mark/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/mark/.ssh/id_xmss type -1
debug1: identity file /home/mark/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.2
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.2 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 127.0.0.1:22 as 'newftpuser'
debug3: hostkeys_foreach: reading file "/home/mark/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/mark/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from 127.0.0.1
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-c
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256
debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:VoKLp0efIZDYddCrvaGWGxEwjOm/lY8zmZP+7qvBmG0
debug3: hostkeys_foreach: reading file "/home/mark/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/mark/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from 127.0.0.1
debug1: Host '127.0.0.1' is known and matches the ECDSA host key.
debug1: Found key in /home/mark/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/mark/.ssh/id_rsa RSA SHA256:QUf6bnT6YZ4Q5EWKbfGIo4iTVkb1yMb4MsuDqYe1nos
debug1: Will attempt key: /home/mark/.ssh/id_dsa 
debug1: Will attempt key: /home/mark/.ssh/id_ecdsa 
debug1: Will attempt key: /home/mark/.ssh/id_ecdsa_sk 
debug1: Will attempt key: /home/mark/.ssh/id_ed25519 
debug1: Will attempt key: /home/mark/.ssh/id_ed25519_sk 
debug1: Will attempt key: /home/mark/.ssh/id_xmss 
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred 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 public key: /home/mark/.ssh/id_rsa RSA SHA256:QUf6bnT6YZ4Q5EWKbfGIo4iTVkb1yMb4MsuDqYe1nos
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
debug1: Trying private key: /home/mark/.ssh/id_dsa
debug3: no such identity: /home/mark/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /home/mark/.ssh/id_ecdsa
debug3: no such identity: /home/mark/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /home/mark/.ssh/id_ecdsa_sk
debug3: no such identity: /home/mark/.ssh/id_ecdsa_sk: No such file or directory
debug1: Trying private key: /home/mark/.ssh/id_ed25519
debug3: no such identity: /home/mark/.ssh/id_ed25519: No such file or directory
debug1: Trying private key: /home/mark/.ssh/id_ed25519_sk
debug3: no such identity: /home/mark/.ssh/id_ed25519_sk: No such file or directory
debug1: Trying private key: /home/mark/.ssh/id_xmss
debug3: no such identity: /home/mark/.ssh/id_xmss: No such file or directory
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
newftpuser@127.0.0.1: Permission denied (publickey).
Connection closed. 

ls .ssh:

authorized_keys  id_rsa  id_rsa.pub  known_hosts

I can tell from the output that it finds the ssh key OK but it can't work it.
It is the first time I'm experiencing this issue so I would be thankful
for any pointers.

output from /var/log/auth.log:

Jun  3 12:10:36 blog sudo:     mark : TTY=pts/0 ; PWD=/home/mark ; USER=root ; COMMAND=/usr/bin/systemctl status vsftpd
Jun  3 12:10:36 blog sudo: pam_unix(sudo:session): session opened for user root by mark(uid=0)
Jun  3 12:10:36 blog sudo: pam_unix(sudo:session): session closed for user root
Jun  3 12:17:01 blog CRON[2039]: pam_unix(cron:session): session opened for user root by (uid=0)
Jun  3 12:17:01 blog CRON[2039]: pam_unix(cron:session): session closed for user root
Jun  3 12:20:58 blog sshd[2057]: Connection closed by authenticating user newftpuser 127.0.0.1 port 53342 [preauth]
Jun  3 12:21:06 blog sshd[2061]: Connection closed by authenticating user newftpuser 127.0.0.1 port 53344 [preauth]
Jun  3 12:32:06 blog sshd[2067]: error: kex_exchange_identification: read: Connection reset by peer
Jun  3 12:33:40 blog sudo:     mark : TTY=pts/0 ; PWD=/home/mark ; USER=root ; COMMAND=/usr/bin/systemctl status vsftpd
Jun  3 12:33:40 blog sudo: pam_unix(sudo:session): session opened for user root by mark(uid=0)
Jun  3 12:33:40 blog sudo: pam_unix(sudo:session): session closed for user root
Jun  3 12:39:01 blog CRON[2074]: pam_unix(cron:session): session opened for user root by (uid=0)
Jun  3 12:39:01 blog CRON[2074]: pam_unix(cron:session): session closed for user root
Jun  3 12:55:31 blog sshd[2188]: error: kex_exchange_identification: Connection closed by remote host
Jun  3 12:55:31 blog sshd[2187]: error: kex_exchange_identification: Connection closed by remote host
Jun  3 12:55:31 blog sshd[2189]: error: kex_exchange_identification: Connection closed by remote host
Jun  3 12:55:38 blog sshd[2190]: error: kex_exchange_identification: Connection closed by remote host
Jun  3 13:00:35 blog sudo:     mark : TTY=pts/0 ; PWD=/etc ; USER=root ; COMMAND=/usr/bin/systemctl status sftp
Jun  3 13:00:35 blog sudo: pam_unix(sudo:session): session opened for user root by mark(uid=0)
Jun  3 13:00:35 blog sudo: pam_unix(sudo:session): session closed for user root
Jun  3 13:00:39 blog sudo:     mark : TTY=pts/0 ; PWD=/etc ; USER=root ; COMMAND=/usr/bin/systemctl status sftp-server
Jun  3 13:00:39 blog sudo: pam_unix(sudo:session): session opened for user root by mark(uid=0)
Jun  3 13:00:39 blog sudo: pam_unix(sudo:session): session closed for user root
Jun  3 13:00:47 blog sudo:     mark : TTY=pts/0 ; PWD=/etc ; USER=root ; COMMAND=/usr/bin/systemctl status vsftpd
Jun  3 13:00:47 blog sudo: pam_unix(sudo:session): session opened for user root by mark(uid=0)
Jun  3 13:00:47 blog sudo: pam_unix(sudo:session): session closed for user root
Jun  3 13:09:01 blog CRON[2300]: pam_unix(cron:session): session opened for user root by (uid=0)
Jun  3 13:09:01 blog CRON[2300]: pam_unix(cron:session): session closed for user root
Jun  3 13:17:01 blog CRON[2310]: pam_unix(cron:session): session opened for user root by (uid=0)
Jun  3 13:17:01 blog CRON[2310]: pam_unix(cron:session): session closed for user root
Jun  3 13:25:33 blog sshd[2315]: Invalid user deploy1 from 81.69.190.192 port 54748
Jun  3 13:25:33 blog sshd[2315]: Received disconnect from 81.69.190.192 port 54748:11: Bye Bye [preauth]
Jun  3 13:25:33 blog sshd[2315]: Disconnected from invalid user deploy1 81.69.190.192 port 54748 [preauth]
Jun  3 13:39:01 blog CRON[2371]: pam_unix(cron:session): session opened for user root by (uid=0)
Jun  3 13:39:01 blog CRON[2371]: pam_unix(cron:session): session closed for user root
Jun  3 13:40:08 blog sshd[2466]: Invalid user db2inst from 125.213.128.88 port 54260
Jun  3 13:40:09 blog sshd[2466]: Received disconnect from 125.213.128.88 port 54260:11: Bye Bye [preauth]
Jun  3 13:40:09 blog sshd[2466]: Disconnected from invalid user db2inst 125.213.128.88 port 54260 [preauth]
Jun  3 13:45:07 blog sshd[2470]: Invalid user 6 from 192.241.139.160 port 46130
Jun  3 13:45:07 blog sshd[2470]: Received disconnect from 192.241.139.160 port 46130:11: Bye Bye [preauth]
Jun  3 13:45:07 blog sshd[2470]: Disconnected from invalid user 6 192.241.139.160 port 46130 [preauth]
Jun  3 13:48:40 blog sshd[2473]: Invalid user x-man from 129.204.131.218 port 56358
Jun  3 13:48:41 blog sshd[2473]: Received disconnect from 129.204.131.218 port 56358:11: Bye Bye [preauth]
Jun  3 13:48:41 blog sshd[2473]: Disconnected from invalid user x-man 129.204.131.218 port 56358 [preauth]
Jun  3 13:51:20 blog sshd[2476]: Invalid user gmod from 68.183.137.83 port 40424
Jun  3 13:51:20 blog sshd[2476]: Received disconnect from 68.183.137.83 port 40424:11: Bye Bye [preauth]
Jun  3 13:51:20 blog sshd[2476]: Disconnected from invalid user gmod 68.183.137.83 port 40424 [preauth]

Best Answer

So, if your SFTP server is like mine you have to generate the key pair in Linux:

ssh-keygen -t rsa

and then convert the PUB key to PEM format with this:

ssh-keygen -e -f id_rsa.pub > id_rsa_pem.pub

and then import the PEM key on the SFTP server and attach to the account.