-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
Description
ssh-copy-id 可以把本地的公钥复制到服务器上,这样子就可以不用密码登录了。
使用方法如下:
$ ssh-copy-id -h
Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
-f: force mode -- copy keys without trying to check if they are already installed
-n: dry run -- no keys are actually copied
-h|-?: print this help基本使用
假设服务器的用户名为 root,地址为 192.168.1.1
如果你本地没有
id_rsa.pub文件的话,需要先用ssh-keygen命令生成公钥和私钥。
不指定参数执行
ssh-copy-id root@192.168.1.1执行以上命令的时候会让你输入服务器密码,输入完后会提示你添加的条数
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.1'"
and check to make sure that only the key(s) you wanted were added.如果你重复执行命令的话,会提示你已经添加过了,会出现如下提示,如果你想覆盖,可以使用 -f 参数。
$ ssh-copy-id root@192.168.1.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
(if you think this is a mistake, you may want to use -f option)使用
-n参数可以模拟命令执行过程,但实际上不会被添加。
指定公钥
ssh-copy-id 和 ssh 命令类似,可以指定 -i 参数,即指定公钥所在位置,使用方法如下
前提还是需要用 ssh-keygen 生成公钥私钥文件,如 ssh-keygen -f demo
ssh-copy-id -i ./demo root@192.168.1.1