Google Colaboratoryをsshで操作する方法がわかったので情報を整理する。
引用情報
https://towardsdatascience.com/colab-free-gpu-ssh-visual-studio-code-server-36fe1d3c5243
手順
- ngrokのtalkenを取得する。
- Colabにngorkをインストールし実行する。
- 手元のPC(mac)でremote接続する。
1. ngrokのtalkenを取得する。
ngrokで無料の登録をし、authentication setting にてAuthtokenを取得します。
2. Colabにngorkをインストールし実行する。
# Install useful stuff ! apt install --yes ssh screen nano htop ranger git > /dev/null # SSH setting ! echo "root:carbonara" | chpasswd ! echo "PasswordAuthentication yes" > /etc/ssh/sshd_config ! echo "PermitUserEnvironment yes" >> /etc/ssh/sshd_config ! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config ! service ssh restart > /dev/null # 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 # Run ngrok
PUT_YOUR_TOKEN_HEREには1. で取得したAuthtokenを代入します。
authtoken = "PUT_YOUR_TOKEN_HERE" get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &') ! sleep 3 # Get the address for SSH import requests from re import sub r = requests.get('http://localhost:4040/api/tunnels') str_ssh = r.json()['tunnels'][0]['public_url'] str_ssh = sub("tcp://", "", str_ssh) str_ssh = sub(":", " -p ", str_ssh) str_ssh = "ssh root@" + str_ssh print(str_ssh)
- 手元のPC(mac)でremote接続する。
ssh root@0.tcp.ngrok.io -p 14407
passwordを求められた場合には、carbonaraを入力する。
無事にログインできました。
補足
何度かトライしてると以下の様なエラーが発生してしまう。この点は調査を進めていく。
ConnectionError: HTTPConnectionPool(host='localhost', port=4040): Max retries exceeded with url: /api/tunnels (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7feef3c55b70>: Failed to establish a new connection: [Errno 111] Connection refused',))