Configuring known_hosts in jgit

偶尔善良 提交于 2019-12-04 06:36:14
VonC

This answer mentions:

jsch.setKnownHosts("C:\\Users\\aUsername\\known_hosts");

But you are using jgit, and not jsch (the Java secure shell) directly, so let's see:

C:\Users\VonC\prog\git>git clone https://github.com/eclipse/jgit
Cloning into 'jgit'...
remote: Counting objects: 37854, done.
remote: Compressing objects: 100% (7743/7743), done.
remote: Total 37854 (delta 22009), reused 34367 (delta 18831)
Receiving objects: 100% (37854/37854), 6.73 MiB | 1.37 MiB/s, done.
Resolving deltas: 100% (22009/22009), done.

C:\Users\VonC\prog\git>cd jgit

C:\Users\VonC\prog\git\jgit>grep -nrHI "setKnownHosts" *
org.eclipse.jgit/src/org/eclipse/jgit/transport/JschConfigSessionFactory.java:262:                              sch.setKnownHosts(in);

Found it!

This comes from JschConfigSessionFactory.java#knownHosts(), and looks like:

new File(new File(home, ".ssh"), "known_hosts");
# with:
home = fs.userHome();

userHome is based on System.getProperty("user.home").

So make sure your java session has a user.home defined, and that you have a %USERPROFILE%/.ssh/known_hosts file in there.

(user.home should be set by java to %USERPROFILE% for Windows, that is, if you are on Windows: in some case, this won't always work).


Now if you do have a %USERPROFILE%/.ssh/known_hosts, then, as mentioned here

Just SSH to the client (using command-line ssh tool), this will add entry to your ~/.ssh/known_hosts file.


In this case, the StormeHawke mentions in the comments:

since I'm running this in Tomcat as a windows service, Jsch (and by extension JGit) was looking not in my user folder but in the SYSTEM account's home folder for the .ssh folder.
In this case I went ahead and just copied the .ssh folder into the SYSTEM home folder since Tomcat only runs on my machine for development and testing purposes (Probably not the best security policy but the risk is minimal in this case).

From this question, this one, that directory for the LocalSystem Account should be:

C:\Documents and Settings\Default User
# or Wind7 / 2008
C:\Windows\System32\Config\systemprofile

The OP mentions:

According to this call:

 System.out.println(System.getProperty("user.home")); 

the default SYSTEM home directory for Windows7 (and presumably any other NT-based Windows system) is simply C:\.
(so not ideal, but for a quick fix, it works).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!