I know on GCP, we can set up a vscode server and connect to that. But what I\'m after here, is to know whether it is possible to connect to the runtime instance on Google Colab
I encountered the same issue with second login and I finally figured that out.
This issue is raised by no execution permission for the ./ngrok file. In my understand,when downloaded ngrok and unziped it at the first time, it gained the execute permission. So you were able to create a tunnel. But when the Colab runtime was restarted, I think it automatically recycled those permissions, because when I tried ./ngrok authtoken $authtoken ....
, it gave me permission denied error.
Therefore the only thing need to be done is to reassign ./ngrok the execute permission,
for example , run chmod 755 ./ngrok
. And script will work.
Yes, it is very possible. Just managed it today.
What you need to do is create an ssh connection with the google collab. Write this on a google collab jupyter Notebook:
import random, string
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")
import getpass
authtoken = getpass.getpass()
#Create tunnel
get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')
#Print root password
print("Root password: {}".format(password))
#Get public address
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
Check your ngrok status to get your portnum(via the website)
After that you can connect to collab via ssh:
This is the terminal command:
$ ssh <user>@0.tcp.ngrok.io -p <portNum>
(it will ask you for the password generated by the above snipet) You should be able to connect now.
But if you want to use vscode, repeat the connection with ssh via the Remote SSH extension
sources:
remote ssh: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh connection: Connect to google collab with ssh from console from PC
If you exit a session, you can not reconnect, because I believe it changes the port. I tried it too, then it throws an error, saying it's not possible.
But, if you run through the setup again and go the the https://dashboard.ngrok.com/status
, you will see, that the port changed. Then you will be able to use it as a new remote connection. This way, I managed it to connect again.
As far as I understand, once a session is terminated, Google will not keep your files.
There is a python package that was built for this, colab-shh. Although, you have to manually open colab from and browser to create an instance because colab dosn't have API to do that yet.
I've just found another method without using ssh.
# Install jupyterlab and ngrok
!pip install jupyterlab==2.2.9 pyngrok -q
# Run jupyterlab in background
!nohup jupyter lab --ip=0.0.0.0 &
# Make jupyterlab accessible via ngrok
from pyngrok import ngrok
print(ngrok.connect(8888))
It will then show a JupyterLab URL
http://f1fe6fb39df6.ngrok.io # for example
You can click it to run JupyterLab now. Or use the URL with VSCode for remote Jupyter kernel.