假如我们现在有这样两台服务器,都用root登陆。
服务器1:vm01
服务器2:vm02
如何在vm01服务器中免密码登陆登陆vm02?我们使用ssh-keygen认证的方法。比如在ubuntu系统vm01服务器上,我们就可以用下面两个命令简单配置来实现。
ssh-keygen -t rsa -N '' ssh-copy-id root@vm02
按照这个设置后,使用SecureCRT登陆服务器vm01,ssh测试登陆vm02成功。但是博主今天碰到一个奇怪的问题,在vm01中用crontab计划运行脚本传文件到vm02的时候出现了错误,log信息提示“Host key verification failed.” 。
为了找到具体错误原因,需要看下详细登陆信息,在脚本中添加ssh -vvv root@vm02,看是否可以登陆。最后找到错误如下:
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 Pseudo-terminal will not be allocated because stdin is not a terminal. debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug2: ssh_connect: needpriv 0 debug1: Connecting to vm02[xxx.xxx.xxx.xxx] port 22. debug1: Connection established. debug1: permanently_set_uid: 0/0 ...中间省略... debug2: kex_parse_kexinit: debug2: kex_parse_kexinit: debug2: kex_parse_kexinit: first_kex_follows 0 debug2: kex_parse_kexinit: reserved 0 debug2: mac_init: found hmac-md5 debug1: kex: server->client aes128-cbc hmac-md5 none debug2: mac_init: found hmac-md5 debug1: kex: client->server aes128-cbc hmac-md5 none debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP debug2: dh_gen_key: priv key bits set: 135/256 debug2: bits set: 513/1024 debug1: SSH2_MSG_KEX_DH_GEX_INIT sent debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY debug3: check_host_in_hostfile: filename /root/.ssh/known_hosts debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts debug3: check_host_in_hostfile: filename /root/.ssh/known_hosts debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts debug3: check_host_in_hostfile: filename /root/.ssh/known_hosts debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts debug2: no key of type 0 for host master debug3: check_host_in_hostfile: filename /root/.ssh/known_hosts2 debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts2 debug3: check_host_in_hostfile: filename /root/.ssh/known_hosts debug3: check_host_in_hostfile: filename /etc/ssh/ssh_known_hosts debug2: no key of type 2 for host master debug1: read_passphrase: can't open /dev/tty: No such device or address <strong>Host key verification failed.</strong>
从上面log中可以看到主要是这个错误,“debug1: read_passphrase: can’t open /dev/tty: No such device or address” 。因为crontab里的脚本不带任何用户定义的环境变量,所以最好在脚本开头调用一下这些变量。
博主就用env命令看了下当前成功登陆vm01系统的环境变量,然后把这些变量放到脚本开头就解决问题了。。。