首页·终端命令

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