![ssh-copy-id 公鑰被拒絕](https://rvso.com/image/1444049/ssh-copy-id%20%E5%85%AC%E9%91%B0%E8%A2%AB%E6%8B%92%E7%B5%95.png)
我正在嘗試將我的公共 ssh 密鑰複製到我的 vps,以便我可以使用ssh
.
但是,當我輸入命令時:
ssh-copy-id me@myserver
我收到此錯誤訊息
/usr/local/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/local/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Permission denied (publickey).
有人知道如何解決這個問題嗎?我在 Mac 上。
答案1
正如 Camilo 已經建議的那樣,您可以在遠端伺服器上手動添加正確的 SSH 公鑰。
就我而言,當 SSH 程式嘗試使用與使用 ssh-copy-id 時定義的身份檔案不同的識別檔案或無法找到定義的私鑰/識別檔案時,會發生相同的錯誤。你可以透過加入 -v 來觀察 SSH 程式在執行 ssh 指令時做了什麼:
ssh -v username@your-host-ip-or-domain
然後,您只需在本機上取得 SSH 程式嘗試使用私鑰的任何公鑰,例如(在 Mac/Linux 上)預設金鑰:
cat ~/.ssh/id_rsa.pub
...並將其新增至遠端的authorized_keys檔案:
~/.ssh/authorized_keys
另一個(就我而言)更好的解決方案是在本地 ssh 設定檔中新增自訂主機。在我的 Mac 上是:
~/.ssh/config
例如,您可以在這裡添加如下內容:
Host mynewserver
HostName some.IP.number.or.domain
Port 20000 #if custom port is used and not the default 22
User the_root
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_for_my_new_server
然後你只需要執行:
ssh mynewserver
⋯⋯瞧
答案2
我遇到了同樣的問題,對我有用的方法是手動放置,在此連結中您可以找到如何操作手動錄入。透過這種方式,即使使用配對金鑰,我也能夠透過 ssh 連線。當我配置它時,.ssh/authorized_keys 檔案包含另一個指向初始電腦的金鑰,因此我用初始電腦的 /.ssh/id_rsa.pub 中包含的資訊覆蓋該訊息