Skip to content

Instantly share code, notes, and snippets.

@sugarme
Last active December 16, 2022 03:43
Show Gist options
  • Save sugarme/1094d22d324b5e0183f8562ca46820cc to your computer and use it in GitHub Desktop.
Save sugarme/1094d22d324b5e0183f8562ca46820cc to your computer and use it in GitHub Desktop.
script to ssh to google colab using tunnel ngrok
# Setup sshd
apt-get update
apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null
# create an account on ngrok if not already and copy the authtoken
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
read -p 'Enter the authtoken from ngrok :' authtoken
./ngrok authtoken $authtoken
./ngrok tcp 22 &
apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null
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
/usr/sbin/sshd -D &
mkdir -p /root/.ssh
read -p "run 'ssh-keygen' on your local pc and \ncopy paste the content of ~/.ssh/id_rsa.pub file" rsa
echo $rsa >> /root/.ssh/authorized_keys
echo "You can connect to colab server on terminal with following command"
curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print('ssh [email protected] -p ' + json.load(sys.stdin)['tunnels'][0]['public_url'][-5:])"
# Install Go
add-apt-repository ppa:longsleep/golang-backports -y
apt update
apt install golang-go
rm -rf /usr/local/share/jupyter/kernels/gophernotes
export GOPATH=/root/go
# Install tmux and jupyter notebook
apt install tmux
pip install jupyterlab
# Setup Gophernotes
GO111MODULE=on go get github.com/gopherdata/gophernotes
mkdir -p ~/.local/share/jupyter/kernels/gophernotes
cd ~/.local/share/jupyter/kernels/gophernotes
cp "$(go env GOPATH)"/pkg/mod/github.com/gopherdata/[email protected]/kernel/* "."
chmod +w ./kernel.json # in case copied kernel.json has no write permission
sed "s|gophernotes|$(go env GOPATH)/bin/gophernotes|" < kernel.json.in > kernel.json
# Install ngrok
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
# Run jupyter notebook
# tmux
# jupyter notebook --ip 0.0.0.0 --port 50000
# split tmux terminal `ctl b` `shift "` (switch between them by `ctl b arrow-up/down`
# Forward port 5000 to ngrok
# login ngrok.io and copy authtoken
# ./ngrok authtoken AUTH_TOKEN
# ./ngrok http 50000
# If successfully, there will be URLs displayed.
# Prerequisites: Go and GOPATH environment have been setup.
export GOPATH=/root/go
GO111MODULE=on go get github.com/sugarme/[email protected]
cd $GOPATH/pkg/mod/github.com/sugarme/[email protected]
bash setup-gpu.sh
# Export environment variables - NOTE: there's no `.bashrc` in google colab instance.
export LIBTORCH=/root/go/pkg/mod/github.com/sugarme/[email protected]/libtch/libtorch
export LIBRARY_PATH=/root/go/pkg/mod/github.com/sugarme/[email protected]/libtch/libtorch/lib
export CPATH=/root/go/pkg/mod/github.com/sugarme/[email protected]/libtch/libtorch/lib:/root/go/pkg/mod/github.com/sugarme/[email protected]/libtch/libtorch/lib/include:/root/go/pkg/mod/github.com/sugarme/[email protected]/libtch/libtorch/include/torch/csrc/api/include
export LD_LIBRARY_PATH=/root/go/pkg/mod/github.com/sugarme/[email protected]/libtch/libtorch/lib:/usr/lib64-nvidia:/usr/local/cuda-10.1/lib64
@ucalyptus2
Copy link

@sugarme can u explain how to use this?

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