Run PyCharm IDE over SSH using Remote Host Interpreter


January 27, 2020
Assume you have installed a VM on the VirtualBox. We first see how we can access the VM terminal within the host machine. Then we will see how to run our python code using PyCharm IDE, which is installed on the host while codes and the interpreter are all located on the VM.
SSH to VM over NAT, PyCharm with SSH interpreter

Getting Started

VM Network Configuration

There are possible configurations for the VM within VirtualBox, namely “Bridge” and “NAT” mode. NAT is the simplest way of accessing an external network from a virtual machine. We set the network adaptor mode to NAT and configure Port Forwarding in order to get access to the VM from the host.

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 1: Select the VM and click on Setting

Go to the Network and then select NAT in “Attached to” and click on Port Forwarding:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 2: VM Network Configuration

In the following step we forward all queries sent to the port 2229 of the Host to the port 22 of the VM which is the default port for SSH. The port number 2229 can be any arbitrary available port in the host. Add a new entry and enter the number 2229 for Host Port and number 22 for the Guest Port. Leave everything else and click on Ok.

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 3: VM Network NAT Configuration

Then start the VM and you are done with VM’s network configuration!

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 4: Running the VM

Connect to the VM from the Host (your computer)

Now you are able to open your terminal and connect to the VM from your machine (host). Thus, minimize the VM (you do not have to work with the VM’s actual window anymore!) and continue using SSH.

Now open a new terminal (either your Linux/MacOS terminal or putty) and connect to the VM:

# In MacOS/Linux terminal you can enter:
ssh ubuntu@localhost -p 2229
SSH to VM over NAT, PyCharm with SSH interpreter
Fig 5: Log in to the VM within the Host terminal
SSH to VM over NAT, PyCharm with SSH interpreter
Fig 6: You've loged in to the VM!

Then it prompts for the password, after entering the password you will log into the VM. Now you can open as many terminals as you need and as long as you have made an SSH connection (like fig. 5) then you can think of each of them as a VM terminal. Every command you enter is executed in the VM.


Using PyCharm over SSH Connection

Now we are looking for an IDE that lets us work on files on the remote server and execute them! Thus, download and install PyCharm Professional Edition. If you install community edition, it does not provide you with the feature we are looking for. You can use your university mail to activate the license.

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 7: Download the Professional Edition
SSH to VM over NAT, PyCharm with SSH interpreter
Fig 8: Run the PyCharm

After installation lunch the IDE and select Create new project:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 9: Select "Create new project"

Enter a new name for the project and move forward according to the image below:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 10: Configure the Interpreter

According to the image below, enter localhost and ubuntu for the Host and Username fields, respectively. Enter the port number 2229 in the port number field. Then press next.

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 11: Configure the interpreter

In the next step you give the path where the python binary is located. This can be the default python path in your system (you can get that using whereis python). However, we have to use the binaries provided within the lab. Similar to when you are using command source /home/ubuntu/lab1/sourceMe. Therefore, enter the path according to the figure below and press finish:

/home/ubuntu/lab1/.venv/bin/python3
SSH to VM over NAT, PyCharm with SSH interpreter
Fig 12: Path for python binary

In the next step, make sure you have selected the Remote interpreter like what you see in the picture below:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 13: Select the newly added remote interpreter

Now you have created a project using VM’s python interpreter. Let’s copy files from the VM into the host in order to start editing them. Go to tools, Deployment and then Browse Remote Host:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 14: Enable browsing remote host

In the newly opened window, look for home, then ubuntu and lab1. Open the lab1 folder and select both folders track1 and track2 by pressing ctrl button. Then press right-click and select copy according to the image below:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 15: Copy folders from remote server (VM) to your machine (host)

Go to the Project windows in the left side of IDE and past folders inside your project folder:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 16: Past folders from VM to your host

So now you have lab files from the VM. Before starting to open and work on them, let’s sync your host with the VM so that the changes you are going to make are being synced with the files in the VM. Thus, click on folder project, go to the Deployment and select upload to ubuntu@localhost:2229, like the image below:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 17: Upload and sync files with the VM

Now you are ready to open files, edit and execute them. So let’s open folder track1/module4 and then receiver.py and sender.py. Then run the receiver.py and then do the same for the sender.py:

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 18: Open the files, edit and run them

Now you get results of the execution of each file on the bottom window and it’s like when you are running on two separate terminals inside the VM.

SSH to VM over NAT, PyCharm with SSH interpreter
Fig 19: Reciver.py is ruuning and you can see its status on the terminal

Congratulations! You have successfully edited and executed your files on the remote VM!


Linux Networking PyCharm Remote