Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SidharthArya/a81069e41e40a1b6e2ef695151ed567b to your computer and use it in GitHub Desktop.
Save SidharthArya/a81069e41e40a1b6e2ef695151ed567b to your computer and use it in GitHub Desktop.
SSH into google colab
#CODE
#Generate root password
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 "X11Forwarding 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'])"
#Copy authentication token after authenticating your ID using google or github and
###./ngrok authtoken 616pgtVvEMKxN6C********************************************
Password will be generated
How to SSH ?
On client side:
echo ForwardX11 yes >> ~/.config/ssh_config
ssh root@0.tcp.ngrok.io -p 10965 -Y <-- port number dictated in colab output
passord : In colab output
@WassimBenzarti
Copy link

Thank you sharing!
Did you successfully connect to Colab using x11 Forwarding ?

@SidharthArya
Copy link
Author

Yes i did. @WassimBenzarti are you facing some issue connecting with X11 forwarding?

@WassimBenzarti
Copy link

WassimBenzarti commented Mar 20, 2020

I managed to make it work (from windows using vcXsrv) although it was really slow.

@SidharthArya Was the UI smooth (or laggy) ?

What I did

For future reference (if anyone found a problem with X11 forwarding), my problem was that:

1. The DISPLAY environment variable wasn't inside Windows

The DISPLAY environment variable needs to be present on Windows and Linux (in this case Colab VM) in order for this to work (or at least with vcXsrv)

You can see all the environment variable in Windows using this command

SET

and Linux with

env

2. I didn't know what to put inside the DISPLAY env variable

When the vcXsrv launches, you will find a little icon in the notifications (bottom left). When you hover on the icon, you will see your pc name along with the display number (let's say it's X.X, by default it's 0.0 )
Now you can set the env variable on both Windows and Linux like this

SET DISPLAY=localhost:0.0 # Replace the X.X with your own values
export DISPLAY=localhost:0.0 # Of course you can add this line to the ~/.bashrc

This approach is based on this Youtube Video made by knary.

@SidharthArya
Copy link
Author

SidharthArya commented Mar 21, 2020

It was very laggy. I doubt it can't be made to work like native applications though, because of the dependence on network.
Thank you for your update on this. It's very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment