linux ssh首次连接时自动接受公钥
2014-06-22 14:07:42 阿炯

当与另外一台主机第一次连接时,对方会把自己的公钥发送过来,ssh命令行会提示是否将该key加入到主目录下的'.ssh/known_hosts'中。必须要加入到该文件后才能继续连接认证的过程。于是便要输入'yes'后才能开始认证。

如果要一次性通过for循环语句连接多台机器,这个确认的过程就比较麻烦了。像下面的一个rsync,我有几十台机器需要同步,用for循环来实现,如果要一个个地确认就很麻烦。

rsync -av -e 'ssh -i freeoa.rsa' site/wxmaster 192.168.30.156:/home/freeoa/

for i in $(cat hosts);do rsync_fun;done

当然这个问题是有办法解决的,过程如下:

方法一:设置取消严格的key检查
can set the StrictHostKeyChecking option to no on the command line, and/or send the key to a null known_hosts file. You can also set these options in your config file, either for all hosts or for a given set of IP addresses or host names.

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

方法二:在配置文件中设置对某些连接的地址的key自动跳过
To disable (or control disabling), add the following lines to the beginning of /etc/ssh/ssh_config.

Host 192.168.0.*
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null

Options:

The Host subnet can be * to allow unrestricted access to all IPs.

Edit /etc/ssh/ssh_config for global configuration or ~/.ssh/config for user-specific configuration.

当然可以使用'*'来对所有的ssh连接都这样操作,这个设置可在主配置文件或用户的个人配置中设置。