美文网首页架构师
linux 本地终端 SSH 连接 gcp (Google Cl

linux 本地终端 SSH 连接 gcp (Google Cl

作者: hiekay | 来源:发表于2019-03-06 16:13 被阅读4次

    准备

    首先在gcp创建实例

    1.设置当前用户的新密码

    $ sudo passwd ${whoami} // 下面以 user 代替 ${whoami}
    # 输入新密码
    

    2.设置下 root 的新密码

    $ sudo passwd root
    # 输入新密码
    

    3.在本地生成私钥和公钥

    $ cd ~/.ssh
    $ ssh-keygen -f myKey
    或者
    $ ssh-keygen -t rsa -f ~/.ssh/my-ssh-key -C [USERNAME]
    
    Generating public/private rsa key pair.
    Enter passphrase (empty for no passphrase): ( 按enter键即可)
    Enter same passphrase again: ( 按enter键即可)
    Your identification has been saved in myKey.
    Your public key has been saved in myKey.pub.
    The key fingerprint is:
    SHA256:EW7ow1aaaaaf8mNvk user@computer-name.local
    The key's randomart image is:
    +---[RSA 2048]----+
    |=.o+= o .        |
    |o+.o+= + .       |
    |o.o..oo *        |
    |..o+ +o+ o       |
    |.oo+  =+S o      |
    | o. * o. = o     |
    | ..o =  . =      |
    |. o.. .    E     |
    | . .o.           |
    +----[SHA256]-----+
    # 此时会生成 公钥 myKey.pub 和 私钥 myKey
    

    4.复制公钥

    $ cat myKey.pub
    ssh-rsa AAAAaaaaaaaeglRVJzAhNq+W
    中间部分省略。。。
    dKx8sJ0Rw4aaaaaa845UVp1 user@computer-name.local
    # 把这长长的一段复制下来,把其中的 user@computer-name.local 改为你在浏览器 SSH 登入之后的当前用户名 ${whoami}
    

    5.导入公钥

    • 进入谷歌云平台页面 -> 计算引擎 -> 元数据 -> SSH 密钥,粘贴保存
    • 谷歌就会把上面这段 public key 写入到 ~/.ssh/authorized_keys

    6.本地通过私钥登录

    $ ssh -i myKey user@ip
    $ ssh-rsa [KEY_VALUE] [USERNAME]
    
    Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.11.2-041102-generic x86_64)
    

    7.或者通过 SSH 密码验证登录

    $ ssh user@ip
    Permission denied (publickey).
    
    # 之所以会出现这种情况,因为谷歌默认把密码验证登录关了,需要自行打开
    $ sudo vi /etc/ssh/sshd_config
    PasswordAuthentication yes 
    :wq!
    

    8.改完要重启 ssh 服务

    $ sudo service sshd restart
    

    9.再次连接

    $ ssh user@ip
    user@IP's password: (输入实例用户的密码)
    Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.11.2-041102-generic x86_64)
    

    相关文章

      网友评论

        本文标题:linux 本地终端 SSH 连接 gcp (Google Cl

        本文链接:https://www.haomeiwen.com/subject/lccxpqtx.html