# Connect to Elasticsearch over SSH tunnel

**URL:** <https://discuss.elastic.co/t/connect-to-elasticsearch-over-ssh-tunnel/284522>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 17, 2021, 10:58pm UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-over-ssh-tunnel/284522 "2021-09-17T22:58:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imy](https://avatars.discourse-cdn.com/v4/letter/i/3ab097/32.png) [@Imy](https://discuss.elastic.co/u/Imy)\
**Post date:** [September 17, 2021, 10:58pm UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-over-ssh-tunnel/284522/1 "2021-09-17T22:58:43Z")

</div>

I want to create a code that connects to Elasticsearch through a SSH tunnel. To do that, I created an EC2 instance where I was able to ssh into it and install Elasticsearch. Then, I created RestHighLevelClient using this code to connect to it:

```auto
RestClientBuilder builder = RestClient.builder(new HttpHost("127.0.0.1", 9200));
        builder.setHttpClientConfigCallback(b -> b
                .setProxy(new HttpHost("EC2publicIP", 22, "https"))

        );

        RestHighLevelClient client = new RestHighLevelClient(builder);
        GetBasicStatusResponse response = client.license().getBasicStatus(RequestOptions.DEFAULT);
        response.isEligibleToStartBasic(); 

```

However this code is failing and I feel like I am missing to add the public key to connect. Any help please

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 20, 2021, 4:38am UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-over-ssh-tunnel/284522/2 "2021-09-20T04:38:36Z")

</div>

This is not how you typically connect to Elasticsearch so I would not expect the client to have any special provisions for configuring this. I do not know for sure though as \>I am not familiar with this client.

If you can make it transparent to the client it might however work. I would therefore recommend securing your Elasticsearch properly and then make it available without needing to use a SSH tunnel.

---

<div class="post-metadata">

**Author:** ![Imy](https://avatars.discourse-cdn.com/v4/letter/i/3ab097/32.png) [@Imy](https://discuss.elastic.co/u/Imy)\
**Post date:** [September 20, 2021, 4:12pm UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-over-ssh-tunnel/284522/3 "2021-09-20T16:12:04Z")

</div>

I did create a tunnel and this is the code that I wrote, however it fails with a connection error ( ```  
java.net.ConnectException: Connection refused

```auto

```

Session session = null;  
String sshKeyFile = "/path/testSSH.pem";

```
    try {
        JSch jsc = new JSch();
        jsc.addIdentity(sshKeyFile);
        session = jsc.getSession("user", "ec2-instance", 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();

        int i = session.setPortForwardingL(30000, "localhost", 22, null);

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user_name", "pass"));

        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200));
        builder.setHttpClientConfigCallback(a -> a.setDefaultCredentialsProvider(credentialsProvider));
        RestHighLevelClient client = new RestHighLevelClient(builder);
        GetBasicStatusResponse response = client.license().getBasicStatus(RequestOptions.DEFAULT);
        response.isEligibleToStartBasic();
        System.out.println(response.toString());

    } catch (JSchException e) {
        e.printStackTrace();
    }

```

```auto

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 18, 2021, 4:12pm UTC](https://discuss.elastic.co/t/connect-to-elasticsearch-over-ssh-tunnel/284522/4 "2021-10-18T16:12:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
