Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 "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 ?
ssh root@0.tcp.ngrok.io -p 10965 <-- port number dictated in colab output
passord : In colab output
@avinabsaha

This comment has been minimized.

Copy link

@avinabsaha avinabsaha commented Dec 20, 2019

Code crashes when Hardware Accelerator is set to GPU? Any fixes for that?
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.6/json/init.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.6/json/init.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@KShivendu

This comment has been minimized.

Copy link

@KShivendu KShivendu commented Dec 28, 2019

You can try using only
! curl -s http://localhost:4040/api/tunnels

@Clemente420

This comment has been minimized.

Copy link

@Clemente420 Clemente420 commented Mar 3, 2020

@KShivendu

您只能尝试使用
! curl -s http://localhost:4040/api/tunnels

THANKS,IT WORKS
THANK YOU FOR YOUR WORK~!!

@doantientai

This comment has been minimized.

Copy link

@doantientai doantientai commented May 19, 2020

Thank you for your work! I followed your instruction. On google colab, everything looks fine, but when I tried ssh in my terminal. It gives me the error below. Does anyone have the same problem? Please help.
ssh_exchange_identification: Connection closed by remote host

@ethanm88

This comment has been minimized.

Copy link

@ethanm88 ethanm88 commented May 30, 2020

@doantientai I ran into the same problem.

@Laenka-Oss

This comment has been minimized.

Copy link

@Laenka-Oss Laenka-Oss commented May 31, 2020

Am trying to work with Windows 10 <===> Google Colab and I ran into thesame problem.
! curl -s http://localhost:4040/api/tunnel gives the public url "public_url":"tcp://0.tcp.ngrok.io:17410" among others but does not show the password. Somehow, I changed the port to:
! curl -s http://localhost:4041/api/tunnel and it diplayed a password like JoeKrRmoT4GLqL2Cq36D which I used for SSHing.

C:\Users\me>ssh root@0.tcp.ngrok.io -p 17410. To confirm that I am connected to the instance:
!hostname on Google colab gives me 3pp4f682af3f similar to root@3pp4f682af3f:~# on my Windows cmd.

@ethanm88

This comment has been minimized.

Copy link

@ethanm88 ethanm88 commented May 31, 2020

I just found a solution to this that's working for me. See this Stack Overflow post: https://stackoverflow.com/questions/48459804/how-can-i-ssh-to-google-colaboratory-vm/53252985#53252985.

@minhtus

This comment has been minimized.

Copy link

@minhtus minhtus commented Jun 19, 2020

Hi, I also got the ssh_exchange_identification: Connection closed by remote host because sshd unable to start because of misconfiguration. Looks like echo command append the quotes together into configuration file, so removing those quote fix for me. Hope this helps.

!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
@vdivakar

This comment has been minimized.

Copy link

@vdivakar vdivakar commented Jun 25, 2020

Hi, thanks for the solution. I faced a few problems, so I had put the steps together here with screenshots:
Will be helpful for someone who is stuck.
https://github.com/vdivakar/Colab-SSH-and-GDrive

@libinruan

This comment has been minimized.

Copy link

@libinruan libinruan commented Jul 19, 2020

Hi, thanks for the solution. I faced a few problems, so I had put the steps together here with screenshots:
Will be helpful for someone who is stuck.
https://github.com/vdivakar/Colab-SSH-and-GDrive

Hi @vdivakar,

Is it possible to specify a user name instead of root when configuring the SSH setting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.