Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I re-add remote host to known_host using JSCH?

Tags:

java

jsch

I want to be able to remove the remote server key from known_hosts and add it again. The remote server gets updated often so I want to automatically remove the remote host key and add its new key to known_hosts. I can remove the key from known_hosts though it is clunky and uses a Process instead of going through JSCH. This works but I encounter this message whenever I try to access the server:

The authenticity of host '192.168.1.1 (192.168.1.1)' can't be established.
RSA key fingerprint is 10:10:30:00:e7:0c:d3:18:cf:ac:42:e2:f3:51:25:bg.
Are you sure you want to continue connecting (yes/no)? 

I know it is possible to get around this message using a UserInfo but I use other ways of connecting to the remote server, such as a Process and the message would appear when I run those commands.

Is it possible to use JSCH to remove and add a host id from known_hosts?

Possibly related though it does not use jsch:

How can I write a program (script) to remove obsolete host keys from ~/.ssh/known_hosts?

like image 812
Aboutblank Avatar asked Mar 26 '13 16:03

Aboutblank


People also ask

What is JSch in Java?

JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.

What is the difference between known_hosts and Authorized_keys?

The known_hosts file lets the client authenticate the server, to check that it isn't connecting to an impersonator. The authorized_keys file lets the server authenticate the user.

What is known_hosts in .SSH folder?

The known_hosts File is a client file containing all remotely connected known hosts, and the ssh client uses this file. This file authenticates for the client to the server they are connecting to. The known_hosts file contains the host public key for all known hosts.


Video Answer


1 Answers

Yes you can add a remote host entry into your known_hosts file using JSch. As Jim Garrison answered, there are other ways around the issue, but here is how to do it in your code:

First, understand that as a default, if you do not specify a known_hosts file, JSch can still work, it will just create a run-time known_host file in memory, and add entrys automatically so long as Strict Host Key Checking is set to 'no'.

If you DO specify Known Hosts file, then JSch will add new entries to that file when Strict Host Key Check is set to 'no'

        JSch jsch = new JSch();

        jsch.setKnownHosts(knownHostsFile);
        logger.info("known hosts file set: " + knownHostsFile);

        jsch.addIdentity(privateKey);
        logger.info("rsa private key loaded: " + privateKey);

        Session session = jsch.getSession(user, host, port);
        java.util.Properties config = new java.util.Properties();

        // this setting will cause JSCH to automatically add all target servers' entry to the known_hosts file
        config.put("StrictHostKeyChecking", "no");  
        session.setConfig(config);

        session.connect();

This is not very secure behavior, but is a handy way to get JSCh to setup a new server entry for you. After your known hosts file is setup, I recommend turning your StrictHostKeyChecking back to yes.

like image 68
Damienknight Avatar answered Sep 19 '22 01:09

Damienknight