是否可以将vscode(在本地计算机上)与Google Colab(免费服务)运行时连接? [英] Is it possible to connect vscode (on a local machine) with Google Colab (the free service) runtime?

查看:1103
本文介绍了是否可以将vscode(在本地计算机上)与Google Colab(免费服务)运行时连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在GCP上,我们可以设置一个vscode服务器并连接到该服务器.但是我想知道的是,是否可以连接到Google Colab上的运行时实例(免费实例,即:

I know on GCP, we can set up a vscode server and connect to that. But what I'm after here, is to know whether it is possible to connect to the runtime instance on Google Colab (the free one ie: https://colab.research.google.com) from a locally run vscode. If I'm not mistaken, we can connect to any remote Jupyter kernel in vscode instead of creating a new instance locally and connecting to it.

所以我想知道是否有可能先创建一个Python 3笔记本,然后在您的本地计算机上启动vscode,连接到Colab运行时并在vscode中进行编码?

So I want to know if it is possible to first create a Python 3 notebook and then from your local machine, fire up vscode, connect to the Colab runtime and code inside vscode ?

这里没有本地内容,所有文件,笔记本,所有内容都驻留在Google Colab上,而只是使用vscode而不是Google自己的编辑器进行编码和执行代码(调试等).

There is nothing local here, all files, notebooks, everything resides on Google Colab, it's just the coding and executing the code (debugging, etc) using vscode instead of Google's own editor.

感谢您的回答,我可以成功连接到Google Colab.但是,当我退出ssh并尝试再次登录时,我遇到了这个问题:

Thanks to the answer, I could successfully connect to Google Colab. However, when I exited the ssh and tried to log in again I faced this:

Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
2048 SHA256:yxFwLslfRq7YZFWNIhAD8TfJdp6sTfFbR2CXOWcysOA root@7561da0610da (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:6Yo/7I9JPyYfKJYvtiVelNFHrIL7R1xaB09fDWbVYf4 root@7561da0610da (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:r1HvJi/Y9twPkXoayNA4cSF55eH4MdOETHhXNSiC4ok root@7561da0610da (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Copy authtoken from https://dashboard.ngrok.com/auth
··········
Root password: aCsRocquey6953P9tHhF
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  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)

我重新启动了Google Colab运行时,甚至更改了ngrok令牌并再次尝试,不走运!

I restarted the Google Colab runtime, I even changed the ngrok token and tried again, no luck!

推荐答案

是的,这很有可能.今天才处理.

Yes, it is very possible. Just managed it today.

您需要做的是与google collab建立ssh连接. 将其写在google collab jupyter笔记本上:

What you need to do is create an ssh connection with the google collab. Write this on a google collab jupyter Notebook:

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'])"

检查您的ngrok状态以获取您的端口号(通过网站)

Check your ngrok status to get your portnum(via the website)

之后,您可以通过ssh连接到collab:

After that you can connect to collab via ssh:

这是终端命令:

$ ssh <user>@0.tcp.ngrok.io -p <portNum>

(它将要求您提供上述代码段生成的密码) 您应该现在就可以连接.

(it will ask you for the password generated by the above snipet) You should be able to connect now.

但是,如果您想使用vscode, 通过远程SSH扩展与ssh重复连接

But if you want to use vscode, repeat the connection with ssh via the Remote SSH extension

来源:

远程ssh: https://marketplace.visualstudio. com/items?itemName = ms-vscode-remote.remote-ssh 连接:使用ssh从PC控制台连接到Google collab

remote ssh: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh connection: Connect to google collab with ssh from console from PC

这篇关于是否可以将vscode(在本地计算机上)与Google Colab(免费服务)运行时连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆