[英]Unable to connect ssh to ec2 through config file in ubuntu. Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
[英]EC2 ssh Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
當我想ssh
到我的ec2
主機時,我遇到了這個權限被拒絕的問題。 我嘗試了現有的解決方案chmod 600 "My.pem"
但仍然沒有用。 這是我的調試信息:
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 54.223.47.74 [54.223.47.74] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file My.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file My.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1
debug1: match: OpenSSH_6.6.1 pat OpenSSH_6.6.1* compat 0x04000000
debug1: Authenticating to 54.223.47.74:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:tfjxcE5kePSv1cJK7SWBp/56kgm2DQkyPLSLZ4d73Io
debug1: Host '54.223.47.74' is known and matches the ECDSA host key.
debug1: Found key in /Users/tan/.ssh/known_hosts:24
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Trying private key: My.pem
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
我使用以下命令在我的 centos 機器上解決了這個問題:
ssh -i <Your.pem> ec2-user@<YourServerIP>
這是關於userName ,在我的情況下是ec2-user
。
引用自: AMAZONT疑難解答
通過連接用戶centos
而不是ec2-user
。
我注意到每個 Linux 實例都使用默認的 Linux 系統用戶帳戶啟動。 這可能與ec2-user
不同,具體取決於您的實例。 您使用以下命令登錄,其中DefaultUserName
代表下面引用中的用戶名。
ssh -i <Your.pem> <DefaultUserName>@<YourPublicServerIP>
每個 Linux 實例都使用默認的 Linux 系統用戶帳戶啟動。 默認用戶名由啟動實例時指定的 AMI 確定。
- 對於Amazon Linux 2或 Amazon Linux AMI,用戶名為ec2-user 。
- 對於CentOS AMI,用戶名為centos 。
- 對於Debian AMI,用戶名為admin 。
- 對於Fedora AMI,用戶名為ec2-user或fedora 。
- 對於RHEL AMI,用戶名為ec2-user或root 。
- 對於SUSE AMI,用戶名為ec2-user或root 。
- 對於Ubuntu AMI,用戶名為ubuntu 。
- 否則,如果 ec2-user 和 root 不起作用,請與 AMI 提供商聯系。
您可以在此處找到 ect2 實例的默認用戶名: https ://alestic.com/2014/01/ec2-ssh-username/
但如果您想查找實例的用戶名:單擊“連接”按鈕以查看默認用戶名。
找到用戶名后,運行此命令,以確保您的密鑰不可公開查看。
chmod 400 <private-key-file.pem>
然后使用其公共 DNS 或 IP 連接到您的實例:
ssh -i <private-key-file.pem> ec2-user@
將用戶添加到/etc/sshd_special_user
最近我遇到了這個問題,不小心我使用chmod -R g+rw .
更改了 Home 權限chmod -R g+rw .
它更改了.ssh
文件夾權限。
執行以下權限更改,
[ec2-user ~]$ chmod 600 mount_point/home/ec2-user/.ssh/authorized_keys
[ec2-user ~]$ chmod 700 mount_point/home/ec2-user/.ssh
[ec2-user ~]$ chmod 700 mount_point/home/ec2-user
從臨時實例卸載源磁盤
將其連接回源機器
現在使用相同的命令登錄,
ssh -i 文件名.pem 用戶名@MachineIP
有關更多詳細信息,請參閱此AWS 故障排除文檔
檢查您是否在您的密鑰所在的同一目錄中。 我遇到了同樣的問題,並發現這是我嘗試連接的錯誤目錄形式
cd .ssh
rm authorized_keys
文件或編輯並刪除您嘗試訪問的機器的已保存密鑰。
使用chmod 400 keyname
更改密鑰文件的權限(確保keyname
與您在 Amazon 上擁有的完全匹配)。
使用ec2-user@IPaddress -i keypair.pem
再試一次
確保您位於 .pem 文件所在的同一目錄中。
使用命令,chmod 0400 example.pem
然后使用命令,ssh -i example.pem ec2-user@YOUR-IP
確保ssh
命令指定了 ec2 用戶:
# ec2 user is missing
ssh -i <identity_file.pem> <hostname>
# ec2 user is specified
ssh -i <identity_file.pem> ec2-user@<hostname>
除了這里提到的用戶名問題,它很可能是一個問題。
對我來說,這是公鑰不匹配。 這就是我解決它的方法。 我使用的是帶有 ssh 客戶端的 Mac。
您可以通過運行以下命令從 .pem 文件中獲取本地公鑰:
ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem
在您的實例上,導航到您的 authorized_keys 文件,該文件通常位於此處:
/home/username/.ssh/authorized_keys
將您的公鑰添加到此文件中。 保存並完成。 那應該這樣做。
只是我為什么遇到這個問題的一點背景。 我必須創建一個新的 .pem 文件,因為我丟失了啟動實例時下載的文件。 出於安全原因,此文件無法再次下載。 因為我創建了一個新的 .pem 文件,這用它創建了一個新的公鑰。 這個公鑰需要在實例上手動更新,因為authorized_keys 文件仍然指向舊的公鑰。
還有更正式的 9 步過程來進行排序。 看這里。
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/replacing-lost-key-pair.html
那么, EC2
實例的默認用戶名由啟動實例時指定的 AMI 確定。
對於Amazon Linux 2
或Amazon Linux AMI
,用戶名為ec2-user
。
因此,您需要在SSH
時使用ec2-user
。
之后,在 Linux 中通過 chmod 授予文件權限。(確保您與您的 .pem 文件位於同一目錄中)
使用命令, chmod 400 <Ur_Pem_File_Name>.pem
現在使用命令, ssh -i <Ur_Pem_File_Name>.pem ec2-user@<Public_IP_of_EC2>
通過執行上述操作,我能夠通過 CLI 將 SSH 連接到我的 EC2。
在 Mac 上:
cd .ssh
sudo nano know_hosts
並刪除有問題的主機信息。
檢查 selinux 是否阻止訪問該文件。
請嘗試以下操作:
restorecon -r -vv .ssh/authorized_keys
我有同樣的問題並通過 -
在 Window 機器中,將密鑰保存到 Pageant 參見https://aws.amazon.com/es/blogs/security/securely-connect-to-linux-instances-running-in-a-private-amazon-vpc/
然后在 Putty 中選擇 SSH->Auth-> 選中“Allow Agent Forwarding”並將 ppk 文件放入“Private Key file for Authentication”。
我遇到了同樣的問題,就我而言,問題是應該使用管理員用戶創建文件“My.pem”
因此,解決方案是,首先使用 sudo 創建文件“My.pem”並將權限更改為 400
$ sudo su
$ sudo vim My.pem
#paste the content
$ sudo vim chmod 400 My.pem
$ ssh -i My.pem user@host
# Login ok
我遇到了同樣的問題,但就我而言,這是因為我創建了一個新密鑰以從不同的設備進行連接。 只有在創建新實例時才會添加密鑰對,如果要在創建實例后創建新密鑰,則必須手動添加。
或者
使用舊密鑰對通過ssh
進入 AWS 實例
cd
進入~/.ssh
文件夾和
使用nano
或任何您喜歡的方式打開authorized_keys
文件
然后轉到您新的.pem
密鑰對並使用檢索公鑰
ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem
復制返回的公鑰並將其粘貼到其他密鑰下方的 authorized_keys 文件中,保存並退出。
chmod 400 my-key-pair.pem
如果你還沒有
然后,您應該可以使用新的密鑰對進行連接。
如果您在 mac 上使用終端 (.zsh) 通過 ssh 連接 ec2,那么您必須在所有命令中將名稱從 .pem 更改為 .cer,因為在 mac 上您下載的 key_pair 文件的擴展名為 .cer,否則,您可以遵循與 aws 上給出的命令相同的命令。
除了 harneet singh 的回答之外,您還可以將您的 ec2 實例用戶名從“ec2-user”更改。 導航到 EC2 實例-> 連接-> ec2InstanceConnect-> 用戶名。
一個很好的解決方案: https://bobbyhadz.com/blog/aws-ssh-permission-denied-publickey在 AWS EC2 控制台中,單擊實例名稱旁邊的復選框,然后單擊 Actions 和 select Connect。 單擊 SSH 客戶端選項卡並復制 ssh 命令示例。
在終端解決錯誤
權限被拒絕(publickey、gssapi-keyex、gssapi-with-mic、password)。
ssh-keygen -f " ~/.ssh/known_hosts" -R xx.xx.xxx.xxx
xx - ip 主機
您必須在以下命令中運行 ssh 到您的 ec2 主機
ssh -i <user.pem> ec2-user@<public ip>
如果有
WARNING: UNPROTECTED PRIVATE KEY FILE!
Permissions 0644 for 'user.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
然后運行chmod 0400 <user.pem>
運行以上命令后運行ssh -i <user.pem> ec2-user@<public ip>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.