首頁·終端機命令

ssh-copy-id

網路

用法

ssh-copy-id -i <key.pub> <user>@<host>

把你的公鑰附加到伺服器的 ~/.ssh/authorized_keys,好讓它不再問密碼;第一次做這件事時密碼登入必須還開著,而 `-i` 要給 `.pub` 檔而不是私鑰。

常用選項

選項含義
-i <key.pub>Which public key to install. Point it at the .pub file.
-p 2222Port of the remote host.
-fInstall it without first checking whether it is already there.
-nDry run: print what would be appended and stop.

範例

ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@example.com

Append your key to the remote authorized_keys.

ssh-copy-id -p 2222 deploy@example.com

The same on a non-standard port.

連不上的時候,這些能把範圍縮小——是名稱解析不了、沒有路由,還是連接埠被擋。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. ssh-copy-id 是做什麼的?

把你的公鑰附加到伺服器的 ~/.ssh/authorized_keys,好讓它不再問密碼;第一次做這件事時密碼登入必須還開著,而 `-i` 要給 `.pub` 檔而不是私鑰。

Q. 怎麼寫?

ssh-copy-id -i <key.pub> <user>@<host> —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 4 個,完整列表在 man ssh-copy-id。這條命令屬於網路。

相關命令

man 手冊: man ssh-copy-id