代码之家  ›  专栏  ›  技术社区  ›  Stepan Yakovenko

从PC的控制台使用ssh连接到google collab

  •  1
  • Stepan Yakovenko  · 技术社区  · 6 年前

    我在网上找到了一条操作指南:

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

    它需要一些密钥,但我不知道从哪里获取这个密钥,以及如何使用这种方法从我的电脑通过ssh连接。有人能给点提示吗?我怎么用这个?

    1 回复  |  直到 6 年前
        1
  •  5
  •   hellter    6 年前

    我也有同样的问题。要解决此问题,必须以根用户身份通过ssh连接到实例:

    ssh root@0.tcp.ngrok.io -p <your_port>
    

    password 脚本中的变量)