git(UnknownHostKey)问题

git(UnknownHostKey)问题

起因:

在一次git服务迁移后IP地址发生了变化,好几十台服务器的版本更新都报UnknownHostKey错如下:

git配置好的密钥还有UnknownHostKey问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
▒▒δ▒ύ▒▒PUSH:D:/open_engine/git_grammar/open_grammar
org.eclipse.jgit.api.errors.TransportException: git@39.96.69.230:open_grammar: UnknownHostKey: 39
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:180)
at com.ll.vbox.commonlibns.https.JGitUtil.push(JGitUtil.java:150)
at com.ll.vbox.commonlibns.https.JGitUtil.main(JGitUtil.java:216)
Caused by: org.eclipse.jgit.errors.TransportException: git@39.96.69.230:open_grammar: UnknownHost
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactor
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:140)
at org.eclipse.jgit.transport.TransportGitSsh$SshPushConnection.<init>(TransportGitSsh.ja
at org.eclipse.jgit.transport.TransportGitSsh.openPush(TransportGitSsh.java:176)
at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:160)
at org.eclipse.jgit.transport.Transport.push(Transport.java:1344)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:169)
... 2 more
Caused by: com.jcraft.jsch.JSchException: UnknownHostKey: 39.96.69.230. RSA key fingerprint is 3a
at com.jcraft.jsch.Session.checkHost(Session.java:805)
at com.jcraft.jsch.Session.connect(Session.java:345)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactor
... 8 more

解决方法:

1
ssh-keyscan -t ssh-rsa 39.96.69.230 >> ~/.ssh/known_hosts

如果需要改的服务器多可以用ansible操作:

1
2
ansible haifly-bj:children -m shell -a "sed -i 's?deploy.feiersmart.com,39.96.69.230 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBN95RStr4VdA34rmJ/BL9zkwHtbyAh6vhS8dH7+7R+kqU9uEt2Ifdv8rFS9PM5773UdYcE01PbVvAkmJ2871pPg=
192.168.3.108 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCzrtXI/sl0PGw8GT+FH3/+1N7Z7BTQi64JzX7vV4VOHSo9HrzUq5FOGto++jX1cevFAtSi3EPshTAGme68xtXU=?deploy.feiersmart.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQChq5F6A8fGDrKMPfxiGNaLmk7m+Q2Fff+JipSBOTxjPOO79BW/0ArZ50uc4ww4XQFtLxfdaUPesheXKrXl5hO7yLhotIHrw70UM9qz7D5blXocb7uF885CJZyd2lEHmxhN3VCAeK4J2j/PrIyM7GGycUbJevZu6fzWfB6Ws2vCaC6BFlWejsWQWARaumNxGQrvBjszVvCzn31ePIwc6ztEsty/8PMRNRBM1Gq7WBSAUbMLBGllEcDcEV4n5ORhEd3Qp10Qy4AMabqcVJC4TS1GCwPrYvtTteY41INRnNoB+MicW7MiJZqCIrqbyFnE/u7LY8csxyCW9eo59ZpH2oe3?g' ~/.ssh/known_hosts"

known_hosts是做服务器认证的。

当你用ssh连接到一个新的服务器的时候,ssh会让你确认服务器的信息(域名、IP、公钥),如果你确认了,就会写到known_hosts里。

以后你再连接到这个服务器,但是信息改变了(通常是公钥改变了),就会提示你服务器信息改变了,你可以把它从known_hosts里删除,然后重新确认。

-------------本文结束感谢您的阅读-------------