How to SSH into Google Colab?

Meet
3 min readNov 22, 2019

--

Have you ever wanted to have terminal based GPU system instead of Notebook based system in Colab?

Of course, notebooks are great for displaying already computed outputs, step wise code block etc. But sometimes, it is very boring to convert your project consisting multiple .py files into a jupyter notebook. :(

So, I wanna show you how to connect to Google Colab terminal using SSH protocol.

1) Create a new notebook. Make sure to enable GPU before going ahead if you plan to use one.

2) [Optional] Connect to google drive using below two lines of code.

from google.colab import drive

drive.mount(‘/content/gdrive’)

3) Now, You need to create an account on ngrok.com. But, wait what is ngrok.com. So, I googled the same and got the below answer.

Ngrok is a multiplatform tunneling, reverse proxy software that establishes secure tunnels from a public endpoint such as the internet to a locally running network service while capturing all traffic for detailed inspection and replay.

In short, it captures the global internet traffic through its own server and transmits them to the locally running network. By this you can give a demo of your service running on local network to the targeted customer for demo purpose.

4) Copy and paste below code in colab which installs ngrok and creates a tunnel for us (code taken from this source)

#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 “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’])”

(Note: you can set your own password instead of generating the random string because you will need to enter the same again in your local machine)

This will prompt you to enter the authorization token which can be found in your ngrok account. So copy and paste it. Then you will be given output as following.

Root password: <Password generated or you entered earlier>

tcp://0.tcp.ngrok.io:<port_num_randomly_generated>

5) Now, in your local machine (mine is Windows), open command prompt and type

ssh root@0.tcp.ngrok.io -p <port_num_generated_above>

to connect to the Colab System. You will be asked to enter root password which was created earlier.

There you go. Now you have access to the whole system through SSH terminal.

But wait, what if I want to transfer files from my local machine to the Colab System.

6) Install WinSCP (or any other alternate software).

7) Login using following credentials to access the filesystem and upload your files to Colab System.

Host name: 0.tcp.ngrok.io

Port number: <Port number generated above>

User name: root

Password: <Generated above>

That’s it.

--

--