Skip to main content

how to ssh between to ec2 instance with ssh keys [Resolved]

I have two Ec2 Instance.

Serve 1, OS: Centos - 7.5 Serve 2, OS: Centos - 7.5 Default username: centos

Both are configured for passwordless login, so I have the ssh key with me.

I am able to ssh to both servers using the ssh key from my host.

But when I am trying to ssh from Server 1 to Server 2, it's failing.

I found out the prvt Keys are under /etc/ssh

-rw-r-----. 1 root ssh_keys   1679 Oct  7 14:42 ssh_host_rsa_key


sudo ssh -i ./ssh_host_rsa_key centos@
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

So where I am going wrong.

Question Credit: Hulk711
Question Reference
Asked October 23, 2018
Posted Under: Network
3 Answers

The best way to approach this would be to create a new key for the user you want on the Server1 to be connecting to Server2, using ssh-keygen, and copying that user's PUBLIC key into ~/.ssh/authorized_keys of Server2, then repeating the same procedure for the reverse trip.

YOUR private key must remain yours, it must never leave your PC, don't use it by copying it on the servers, I can assure you that you're not simplifying anything by doing that.

In all my CentOS 7 servers the default SSH configuration resides in ~/.ssh, the private key is usually ~/.ssh/id_rsa and the public is usually ~/.ssh/ these are the only 3 files you should need to create or edit for every machine:


ensure that the ~/.ssh directory is chmod'ed 700 and the files inside 600.

You can use ssh-copy-id to send the public key from one server to another once you have created both sets of keys.

credit: Fanfurlio
Answered October 23, 2018

It will be easier for you to create a new key for user centos in Server_A:

$ ssh-keygen

Once created, copy your /home/centos/.ssh/ content to /home/centos/.ssh/authorized_keys on Server_B

Once completed, you can ssh in to Server_B from Server_A without a password.

credit: RoseHosting
Answered October 23, 2018
Your Answer