Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ssh to google colaboratory VM?

How can I ssh to google collaboratory VM? Is there a more straightforward way to do it than to create ssh tunnel from jupyter notebook to a remote server?

like image 657
Rastislav Rabatin Avatar asked Jan 26 '18 10:01

Rastislav Rabatin


3 Answers

Try pasting the following into a cell and running it (inspired by this gist):

import random, string, urllib.request, json, getpass  #Generate root password password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))  #Download ngrok ! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip ! unzip -qq -n ngrok-stable-linux-amd64.zip  #Setup sshd ! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null  #Set root password ! echo root:$password | chpasswd ! mkdir -p /var/run/sshd ! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config ! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config ! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc ! echo "export LD_LIBRARY_PATH" >> /root/.bashrc  #Run sshd get_ipython().system_raw('/usr/sbin/sshd -D &')  #Ask token print("Copy authtoken from https://dashboard.ngrok.com/auth") authtoken = getpass.getpass()  #Create tunnel get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')  #Get public address and print connect command with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:   data = json.loads(response.read().decode())   (host, port) = data['tunnels'][0]['public_url'][6:].split(':')   print(f'SSH command: ssh -p{port} root@{host}')  #Print root password print(f'Root password: {password}') 

ngrok is used to create a tunnel to the machine and give it a publicly accessible hostname. You will need to sign up, copy your auth token and provide it to the Colab notebook when prompted.

When it completes, it will print out your connection details, something like:

SSH command: ssh -p12312 [email protected] Root password: abcdeLMh6vpNViGHQbXi 

If this last step fails, try running the cell again or create a new cell with just the following and read the JSON output to find the hostname and port.

! curl -s http://localhost:4040/api/tunnels 
like image 52
Tamlyn Avatar answered Sep 24 '22 17:09

Tamlyn


Here's another solution - essentially, the same as presented by @tamlyn, but much shorter. There's a colab-ssh connector, which automates the process. All you have to do is register a free account on ngrok. Then, you need to copy your auth token. Now, in the destination Colab notebook:

# Install colab_ssh
!pip install colab_ssh --upgrade

from colab_ssh import launch_ssh
launch_ssh('YOUR_NGROK_AUTH_TOKEN', 'SOME_PASSWORD')

You should get the connection parameters (pay attention to the port). Now you can connect to the Colab VM by using the provided details, for example: ssh -p 15892 [email protected], where 15892 is the random port number you need to change. Naturally, you will be prompted for the previously set password.

This is obviously limited by free ngrok account restrictions, but it should be enough for small tasks.

If you get IndexError: list index out of range, don't worry. From my experience, it will still work - you can get the port and address from ngrok active tunnels list.

like image 38
Dominik Filipiak Avatar answered Sep 23 '22 17:09

Dominik Filipiak


Since Google had decided to block ngrok for Google Colab, you might need to use different kind of tunel for SSH. Which you can try this:

!pip3 install linus_colab_ssh

from colab_ssh import setup_ssh, loop_forever

public_key = '<YOUR_PUBLIC_SSH_KEY>'
setup_ssh(public_key)
loop_forever()

More details can be found in here. Good luck!

like image 24
lamhoangtung Avatar answered Sep 22 '22 17:09

lamhoangtung