0

Please I created 2 Ubuntu Linux EC2 instances(master and slave) when I tried to ssh from the master server to the slave I get the error Permission denied (publickey).

Server Details

ubuntu@ip-172-31-41-115:~$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

MY Logs

ubuntu@ip-172-31-41-115:~$ ssh -v [email protected]
OpenSSH_8.9p1 Ubuntu-3ubuntu0.6, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 172.31.39.155 [172.31.39.155] port 22.
debug1: Connection established.
debug1: identity file /home/ubuntu/.ssh/id_rsa type -1
debug1: identity file /home/ubuntu/.ssh/id_rsa-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_ecdsa type -1
debug1: identity file /home/ubuntu/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/ubuntu/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_ed25519 type -1
debug1: identity file /home/ubuntu/.ssh/id_ed25519-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_ed25519_sk type -1
debug1: identity file /home/ubuntu/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_xmss type -1
debug1: identity file /home/ubuntu/.ssh/id_xmss-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_dsa type -1
debug1: identity file /home/ubuntu/.ssh/id_dsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.9p1 Ubuntu-3ubuntu0.6
debug1: compat_banner: match: OpenSSH_8.9p1 Ubuntu-3ubuntu0.6 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 172.31.39.155:22 as 'ubuntu'
debug1: load_hostkeys: fopen /home/ubuntu/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:VXmUTrEoG6aXhqqnRZpg8RX4i7b8L5r0iAbyKtCmJwU
debug1: load_hostkeys: fopen /home/ubuntu/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host '172.31.39.155' is known and matches the ED25519 host key.
debug1: Found key in /home/ubuntu/.ssh/known_hosts:1
debug1: ssh_packet_send2_wrapped: resetting send seqnr 3
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: ssh_packet_read_poll2: resetting read seqnr 3
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/ubuntu/.ssh/id_rsa
debug1: Will attempt key: /home/ubuntu/.ssh/id_ecdsa
debug1: Will attempt key: /home/ubuntu/.ssh/id_ecdsa_sk
debug1: Will attempt key: /home/ubuntu/.ssh/id_ed25519
debug1: Will attempt key: /home/ubuntu/.ssh/id_ed25519_sk
debug1: Will attempt key: /home/ubuntu/.ssh/id_xmss
debug1: Will attempt key: /home/ubuntu/.ssh/id_dsa
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected]>
debug1: kex_input_ext_info: [email protected]=<0>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/ubuntu/.ssh/id_rsa
debug1: Trying private key: /home/ubuntu/.ssh/id_ecdsa
debug1: Trying private key: /home/ubuntu/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/ubuntu/.ssh/id_ed25519
debug1: Trying private key: /home/ubuntu/.ssh/id_ed25519_sk
debug1: Trying private key: /home/ubuntu/.ssh/id_xmss
debug1: Trying private key: /home/ubuntu/.ssh/id_dsa
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).

I have made changes to my /etc/ssh/sshd_config (on master and slave )file with the following settings.

PermitRootLogin yes 
PubkeyAuthentication yes
 PasswordAuthentication yes 
GSSAPIAuthentication no 
GSSAPICleanupCredentials no

I have also allow open all ports on the security group if maybe that was the issue. But yet i still get the same issue

2
  • Edit the question and add the exact commands that you used to create keys and put the public key onto the machine to which you are trying to SSH. Also, to be clear, does the ubuntu user exist on both machines with the same UID? Mar 16 at 0:40
  • @NasirRileyI didn't generate any keys during the whole process, I am trying to ssh from one linux instance to the other
    – Godwin
    Mar 16 at 1:13

2 Answers 2

1

In a simple setup and with a simple public IP, I'd highly advise against opening all ports, using password authentication and permit root login. Just for two VMS to interact.

I don't know much about how you created those VMs, but I'd advise to have an another go at it if possible. From start, with the least amount of change in the configuration.

I don't know much about AWS, but you should have set up or downloaded a private key from the slave, if you lost it, you can refer to this : https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html#replacing-lost-key-pair

Send it to your master server and start by reducing access to it :

chmod 400 .ssh/your_private_key.pem

Make sure you can ping the server beforehand, just to make sure.
Or go back to your AWS configuration to make sure.

if everything seems fine, and the servers are running. Try the simple command :

ssh -i .ssh/your_private_key.pem your_user@your_ip

Obviously, change your user with the actual name. Check twice your username just to make sure.

If that doesn't work, and you can still access the servers through AWS, you can try checking these :

  • The actual name of the user
  • Check the SSH configuration using sshd -t
  • Check if you reloaded your ssh daemon after that configuration update sudo systemctl sshd reload and if it's up
  • Check if any firewall is on in your instance

That's the basic rundown. I do still think you should go back and redo it all again, as a security consideration but also a learning step for future ssh'ing. For me, just starting a VM gets my /var/log/auth.log in a mess, so I don't imagine what it'll be for you.

If you'd rather use this, we'd have to have more informations about your whole setup. Do avoid any specific details like the used IP, or the host.

0

Thanks everyone for the support finally resolved the issue with the steps below:

  1. Generate SSH Key Pair (if not already done) - On Master server

    $ ssh-keygen -t rsa -b 4096

  2. Add Private Key to SSH Agent (if not already done): - on Master server

    $ eval "$(ssh-agent -s)" $ ssh-add ~/.ssh/id_rsa

  3. Print the public Key content - on Master server

    $ cat ~/.ssh/id_rsa.pub

  4. Copy the output of the cat command, which represents your public key.

  5. Log in to the Slave server

  6. open the authorized_keys file using a text editor and paste the public key copied from the master server

    $ nano ~/.ssh/authorized_keys

  7. Set appropriate permissions on the ~/.ssh directory and the authorized_keys file - on slave server

    $ chmod 700 ~/.ssh $ chmod 600 ~/.ssh/authorized_keys

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .