Skip to content

Instantly share code, notes, and snippets.

@radames
Last active March 4, 2024 06:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save radames/b513277703a8bb2104a80c81e0677aa2 to your computer and use it in GitHub Desktop.
Save radames/b513277703a8bb2104a80c81e0677aa2 to your computer and use it in GitHub Desktop.
How to connect to your Google Colab Notebook via SSH!

Connect to your Google Colab Notebook via SSH

Using this amazing project tmate.io you can SSH tunnel to your colab notebook machine. You can with few lines of code download the binary, run an instance in the background and output the generated SSH address.

!wget -nc https://github.com/tmate-io/tmate/releases/download/2.4.0/tmate-2.4.0-static-linux-i386.tar.xz &> /dev/null
!tar --skip-old-files -xvf tmate-2.4.0-static-linux-i386.tar.xz &> /dev/null
!rm -f nohup.out; bash -ic 'nohup ./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock new-session -d & disown -a' >/dev/null 2>&1
!./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock wait tmate-ready
!./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock display -p "Connect with SSH address: #{tmate_ssh}"
!./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock display -p "Connect with web: #{tmate_web}"

Google Colab Notebook Example

@NyaMisty
Copy link

Today I tested it and it does not work :(
But I still managed to fix it after learning the trick from google.colab.drive:

!pkill -9 tmate
!wget -nc https://github.com/tmate-io/tmate/releases/download/2.4.0/tmate-2.4.0-static-linux-i386.tar.xz &> /dev/null
!tar --skip-old-files -xvf tmate-2.4.0-static-linux-i386.tar.xz &> /dev/null
!rm -f nohup.out; bash -ic 'nohup ./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock new-session -d & disown -a' >/dev/null 2>&1
!./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock wait tmate-ready
!./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock display -p "Connect with SSH address: #{tmate_ssh}"
!./tmate-2.4.0-static-linux-i386/tmate -S /tmp/tmate.sock display -p "Connect with web: #{tmate_web}"
#!ps auxef | cat
#!cat nohup.out

@radames
Copy link
Author

radames commented May 18, 2021

HI @NyaMisty Thanks for the fix!

@eduardo4jesus
Copy link

eduardo4jesus commented Oct 4, 2021

When I first used it, I was able to be connected for about 15 min. Now, not even 15 seconds. I get runtime disconnected.

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