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?
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
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
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
Related
I'm trying to set up SSH Python interpreter on Pycharm. In the wizard, it is askin for:
Host
Username
Port
I know the the name of my remote workstation and my username. But where can I get the port number from? does it require settings of Pycharm on remote server?
Instructions are not clear.
I have W10 on both sides.
https://www.jetbrains.com/help/pycharm/configuring-remote-interpreters-via-ssh.html
On your Windows 10 remote server you have to enable/install an SSH server. Windows does have a native one, I saw that you can also use OpenSSH now.
And your SSH server should run on port 22 by default.
Try default port 63342 of pycharm besides ssh connection port on 22 :
https://www.jetbrains.com/help/pycharm/settings-debugger.html
You have to setup an SSH server on your remote machine in order to connect to it.
See: https://phoenixnap.com/kb/ssh-to-connect-to-remote-server-linux-or-windows
Once you have done that, the host will be the address of the remote machine, the username and password will be the credentials of a user that exists on the remote machine and the port will be a number that you have chosen when you were setting up the SSH server (22 by default).
I'm having a unique problem with Windows Azure that I don't see on other providers. I've been running connections from remote VMs to a MySQL database running on a DigitalOcean VM. I've successfully connected with AWS, Rackspace, Google, and all other providers, but for some reason, Microsoft Azure VMs don't seem to work.
VM OS: Ubuntu 14.04
I'm trying to connect using PyMySQL and SQLAlchemy.
What I've Tried:
The port is open and listening
The user definitely has permission to upload data into the DB (other remote connections with this user all work fine).
I have even tried "ufw disable" for the Firewall on the Windows Azure VM
I've set 3306 as an endpoint on the Azure VM
Despite all my attempts, the connection cannot be established. Is there something I'm missing on the setup?
As Azure VMs disable ICMP and we can use SSH tunnels to allow outside access to internal network resources. However I don’t have resource to create a DigitalOcean VM, but I have created 2 Azure VMs in 2 Cloud Services to try to reproduce the issue.
I installed mysql-server in VM.1 and mysql-client in VM.2.
Then I tried to connect MySQL server directly from VM.2, I got message “can’t connect to MySQL…”.
To work around this issue, I followed this post, created a SSH tunnel in VM.1 which hosted the MySQL server:
Open port 3306, so a remote client can connect to your MySQL Server. Run the following command to open TCP port 3306
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
Now let’s check if the port 3306 is open by running this command:
sudo netstat -anltp|grep :3306
Create a SSH tunnel for port 3307
sudo ssh -fNg -L 3307:127.0.0.1:3306 azurevmuser#servername
Create an endpoint for the port 3307 in the dashboard of the VM in Azure management portal. For more details , See how to add endpoint to you Virtual machine. Now your Database host name is <vm_name>.cloudapp.net:3307
Then connect MySQL server from VM.2 using command:
# mysql -h <vm_1_name>.cloudapp.net -P 3307 -u user –pPassword
and it would work fine. Feel free to let us know if we have any misunderstood on your issue.
I am wondering if I can use ipython notebook remotely by ssh twice.
The scenario is: Machine B is the machine I want to run ipython notebook. However, I am only allowed to access machine B through another server (machine A) first.
There are tutorials about using ipython notebook remotely, but none of them mentions the situation I've encountered.
Thanks in advance !
Assuming you are referring to ssh tunnelling, and the ipython notebook is servering on port 1234 on machine B:
If machine A can access machine B on any port, you can setup machine A to forward a remote port to you via SSH:
ssh -L 9999:machineB.com:1234 -N machineA.com
This says
ssh to machineA.com without executing a remote command (-N) and setup machine A to forward requests from client port 9999, over an ssh tunnel, to machine B port 1234
However if machine A can only access machine B via ssh, then you will need to create two tunnels. One from your client PC to machineA, and another from machineA to machineB. To do this, the two tunnels connect to a local port on machineA instead of a remote port:
ssh -L 9999:localhost:8888 machineA.com ssh -L 8888:localhost:1234 -N machineB.com
This says
ssh to machineA.com and setup machine A to forward requests from our client PC port 9999, over an ssh tunnel, to machine A port 8888. Then execute the command "ssh -L 8888:localhost:1234 -N machineB.com". This command sets up a second tunnel from machineA port 8888 to machineB port 1234 (where iPython is listening).
Now, with that command running in the background, connect to your local PC port 9999. The first ssh tunnel will forward that request to machineA where it connects to localhost:8888, the second ssh tunnel will then forward it to machineB where it connects to localhost:1234.
Note that machineA will need to be able to connect to machineB automatically (using public/private key authentication) for this to work in a single command.
Here is a post that explains ssh tunnelling nicely https://superuser.com/questions/96489/ssh-tunnel-via-multiple-hops
I use ssh deployment on servers via ssh tunnels,
and each of its has specific options and port forwarding placed in ~/.ssh/config.
PyCharm uses by default its own ssh client when using SFTP deploy.
So, it doesn't work with these deployment servers.
How I could force PyCharm to use my default system ssh client or force to use options from ~/.ssh/config file.
Thanks.
PS: PyCharm version is 3.0.1
Provided PyCharm can be made to use port different than 22 (don't know that), you actually have two workarounds:
Simple workaround
Use port forwarding on localhost:
http://www.debian-administration.org/article/449/SSH_dynamic_port_forwarding_with_SOCKS
..and either use -F specific_config for each tunnel, or use -o to specify relevant options (that you normally have in ~/.ssh/config on ssh commandline) directly. Of course, you have to tell PyCharm to connect to localhost:forwarded_port.
Fancy/sophisticated workaround
Use dynamic port forwarding + tsocks, again described in:
http://www.debian-administration.org/article/449/SSH_dynamic_port_forwarding_with_SOCKS
I tried creating a SSH tunnel using
ssh -L 3306:localhost:22 <hostip>
Then running my python script to connect via localhost
conn = MySQLdb.connect(host'localhost', port=3306, user='bob', passwd='na', db='test')
However, I receive the following error
(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
How can I make sure I'm hitting the correct host and not just some problem with the bind?
Try changing "localhost" to "127.0.0.1", it should work as you expect. This behavior is detailed in the manual:
UNIX sockets and named pipes don't
work over a network, so if you specify
a host other than localhost, TCP will
be used, and you can specify an odd
port if you need to (the default port
is 3306):
db=_mysql.connect(host="outhouse", port=3307, passwd="moonpie", db="thangs")
If you really had to, you could
connect to the local host with TCP by
specifying the full host name, or
127.0.0.1.
Does mysqld run on port 22 on the remote? Call me ignorant but I think what you're trying to do is
ssh -n -N -f -L 3306:localhost:3306 remotehost
Then making MySQL connections on local machine will transparently get tunneled over to the target host.
You can't specify localhost as the hostname, as this suggests that MySQLdb should try to use a UNIX socket. Use 127.0.0.1 for the host instead.
If you want to make sure the connection works, you can use the standard mysql client.