我有2 個實例在AWS中。其中一個在一個公有子網(堡壘),第二個私有子網路。
它們都是使用相同的密鑰對啟動的(.pem文件)。
這就是我連接到堡壘的方式:
ssh -i secret.pem ec2-user@public-ip
效果很好,我加入了。
現在,我想在私有子網路中透過 ssh 方式連接實例。谷歌搜尋說我應該轉發代理:
ssh -A ubuntu@private-ip
但不幸的是我收到錯誤:
Permission denied (publickey).
有人可以解釋一下我做錯了什麼以及如何 ssh 私有實例嗎? (ping和安全群組都沒問題)
答案1
我就是這樣做的:
在您自己的筆記型電腦中,建立(或編輯現有文件)~/.ssh/config,並添加以下內容:
Host [host or ip of the bastion server]
User ec2-user
IdentityFile ~/.ssh/pem_file_required_to_connect_to_bastion
Host [host or ip of the bastion server]
User ec2-user
IdentityFile ~/.ssh/pem_file_required_to_connect_to_server
ProxyCommand ssh ec2-user@CHOSEN_HOST -W %h:%p
將 CHOSEN_HOST 替換為您為堡壘伺服器設定的相同主機。
例子:
cat ~/.ssh/config
Host 3.126.138.136
User ec2-user
IdentityFile ~/.ssh/itaig.pem
Host 172.31.22.212
User ec2-user
IdentityFile ~/.ssh/itaig.pem
ProxyCommand ssh [email protected] -W %h:%p
~/.ssh at 02:40:57 PM
❯
ssh 172.31.22.212
Last login: Sun Aug 8 11:40:41 2021 from ip-172-31-29-253.eu-central-1.compute.internal
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[ec2-user@ip-172-31-22-212 ~]$