大网数据广告活动
服务器运维问答

How to ssh between two ec2 instance with ssh keys?

· 52CE 我爱测

I have two EC2 instances: 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 …

问题原文

原帖链接 · Hulk711 · CC BY-SA 4.0

I have two EC2 instances:

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 that the private keys are under /etc/ssh

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

Error

sudo ssh -i ./ssh_host_rsa_key [email omitted]
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

So where I am going wrong?

已采纳解答

原帖链接 · Fanfurlio · CC BY-SA 4.0

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/id_rsa.pub these are the only 3 files you should need to create or edit for every machine:

~/.ssh/id_rsa
~/.ssh/id_rsa.pub
~/.ssh/authorized_keys

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。