43

To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in Pycharm only accepts one ssh tunnel.

Configure Remote Python Interpreter dialog box

Is there any way to set a multi-hop ssh to have access to the interpreter and data files on the host from local?

mehrtash
  • 910
  • 3
  • 9
  • 16

3 Answers3

74

You can use port forwarding on ssh.

1. Open a terminal and run:

On your local system:

ssh -L 6000:<target_server_ip>:22 <proxy_server_user>@<proxy_server_ip>

You should be connected to the proxy now. You can substitute 6000 with any port.

2. (optional) Test

Now you can ssh into the target server on another terminal with:

ssh -p 6000 <target_server_user>@localhost

3. Configure PyCharm

Keep in mind not to close the first terminal!

The same goes for the PyCharm. Just set the remote interpreter connection through ssh with the following configuration:

  • host: localhost
  • port: 6000
  • user: target_server_user
jmilloy
  • 7,875
  • 11
  • 53
  • 86
Amir
  • 2,259
  • 1
  • 19
  • 29
  • I tried this, but am getting the following error: `Could not list the contents of "sftp://127.0.0.1:9999/" because it is not a folder` – xApple Aug 09 '16 at 15:45
  • what if the connection was from b to c was established via ssh reverse tunnel? – Nickpick Nov 30 '17 at 23:06
  • 1
    Check ~/.ssh/known_hosts and remove any hosts that is causing the problem – Amir May 01 '20 at 18:46
  • Your solution works. However, it always requires password when I ssh into the target server in step 2. Is there any way to make it password-less? – Thang Pham Jun 04 '21 at 14:31
  • @ThangM.Pham Have you tried copying your public ssh rsa key to the server? – Amir Jun 27 '21 at 17:44
  • @AHA I tried but it did not work. It turns out that Pycharm was required root permission to read/write so it asked for password all the time. I just need to use `chown` to grant permission for the specific user and it solved the problem. – Thang Pham Jun 27 '21 at 22:09
7

PyCharm seemingly parses the local .ssh/config too.

If you already have configured ssh hopping there, you can just specify the target server in your pycharm ssh-config.

~/.ssh/config (source)

Host bastion
   Hostname bastion.domain.com
   Port 2222 # a non-standard port is a good idea
   User ironicbadger

Host servera
   Hostname servera.lan.local
   User servera-user
   ProxyCommand ssh bastion -W %h:%p

in pycharm: Host servera, User name server-user

Harald Thomson
  • 730
  • 2
  • 8
  • 18
  • This solution is much better than the accepted answer in case you have ssh config file already setup. – risahbhc32 Aug 04 '21 at 14:35
  • In [this blog post](https://blog.jetbrains.com/pycharm/2017/12/creating-a-development-environment-on-amazon-ec2/), in the **"Setting up PyCharm"** part they also explain the same setup. One extra thing you have to do is to set "Authentication type" to "OpenSSH Config and authentication agent". – risahbhc32 Aug 04 '21 at 15:02
  • Does anybody know how this might be achieved on windows? – mkohler Aug 05 '21 at 19:20
  • 1
    @mkohler this also works on windows (10+) – Vincent.W. Jan 24 '22 at 13:00
3

For users on ssh version 7.3 or later this can be simplified using the ProxyJump parameter.

Host bastion
   Hostname bastion.domain.com
   User bastion-user

Host servera
   Hostname servera.lan.local
   User servera-user
   ProxyJump bastion
FatherShawn
  • 211
  • 2
  • 8