美文网首页git操作
使用 SSH 密钥进行远程登录

使用 SSH 密钥进行远程登录

作者: michael_jia | 来源:发表于2015-12-19 10:37 被阅读13125次

    云服务商 青云 提供的服务和编写的 使用 SSH 密钥远程登录访问主机 值得参考。

    Linux 环境


    CentOS 源于 Red Hat Enterprise Linux(RHEL),了解 RHEL 6: OpenSSHRHEL 7: OpenSSH

    • chmod 700 /path/to/

    • chmod 600 /path/to/kp-1234abcd
      // 将私钥文件设置为只有自己可读写。ssh 会检查这个访问权限是否合理,如果别人可访问,则会拒绝或者忽略该私钥文件。

    • ssh -i /path/to/kp-1234abcd [user@]hostname
      // -i 指明 identity_file。无参运行 ssh 可看帮助。

    • ssh [user@]hostname
      // 如果你已经配置过 ~/.ssh/config,则看一下这个配置文件,ssh 使用会更简单。
      参阅:/etc/ssh/ssh_config,man ssh_config

    # ~/.ssh/config 文件示例
    # Host 参数标明以下内容仅适用于访问 236 主机时适用,Host 参数本身只是一个入口字符串;
    Host 192.168.99.236
      HostName 192.168.99.236
      User git
      Port 22
      IdentityFile ~/.ssh/rsa-michael-236
    Host 3root
      HostName 192.168.99.3
      User root
      Port 22
      IdentityFile ~/.ssh/rsa-3root-michael
    
    # activehacker account
    Host github-activehacker
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_activehacker
    # jexchan account
    Host github-jexchan
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_jexchan
    

    使用时:git clone git@github-jexchan:jexchan/xtry.git,因为在 config 里面配置了 User git,在 git clone 时可以不再指定 git@ 了。

    Mac 环境


    在 Terminal 下参考 Linux 方法。
    通常下载的文件放在 ~/Downloads/目录下。

    Windows 环境


    Git Bash 方法

    如果你安装过 git for windows,想必熟悉 Git Bash,这个环境和 *nix 风格保持一致,可以参考 Linux 方法。

    Xshell 方法

    Create a New Session
    Xshell-Create a New Session Properties-Connection.png
    Xshell - Authentication - Method 选择 Public Key
    Xshell-New Session Properties-Authentication
    Xshell - 导入私钥文件
    User Keys 导入私钥文件.png
    Terminal Encoding:Unicode (UTF-8)

    设置 UTF-8 以正常显示中文。

    设置远程主机的编码 Encoding 为 UTF-8 文件 -> 属性 Properties-Terminal Encoding
    Appearance Font
    Appearance Font Name&Size
    ssh user@hostname

    在界面上配置好后,在 Xshell 命令行下:
    Xshell:> ssh user@hostname 即可。注意:Xshell 下的 ssh 功能比较弱。

    关于密钥和 authorized_keys 文件

    • 以下的内容由你的服务器管理员为你做好配置;
    • SSH 密钥对

    云主机在生成 SSH 公钥/私钥密钥对时,会要求你立刻下载其私钥,并保管好私钥,云服务商是不保存你的私钥的,只有公钥(public key)在云主机上。你在使用 SSH 时用到的 identity file 就是私钥(private key)文件。
    使用 ssh-keygen 命令也可以自己生成。

    • ssh-keygen 命令
      密钥对可通过 ssh-keygen -t rsa -C 'comment' -f filename-of-key-file 生成。该命令通常在 /usr/bin 下。
      生成 2 个文件:filename-of-key-file(私钥) 和 filename-of-key-file.pub(公钥)。
      不指定 -f 参数,则默认存到 ~/.ssh/下,生成 id_rsa 和 id_rsa.pub 两个文件。
      不指定 -C 参数,则 comment 内容默认为 user@hostname。我习惯指定 -C 参数值和密钥文件名一致,比如:rsa-hostip-usrrsa-3git-michael

      See ssh-keygen(1) for more information.**

      Users generate SSH keys locally and upload their public key to the server before being able to interact with it.

    • ~/.ssh/authorized_keys
      公钥文件存放在 ~/.ssh/authorized_keys 文件中,一行一个。凡持有和其中任一公钥配对的私钥的用户都可以访问。

    chmod -R 700 .ssh/ ;注意权限!
    chmod 600 .ssh/authorized_keys ;注意权限!
    

    Lists the public keys (DSA/ECDSA/RSA) that can be used for logging in as this user. The format of this file is described above. The content of the file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others.

    If this file, the ~/.ssh directory, or the user's home directory are writable by other users, then the file could be modified or replaced by unauthorized users.  In this case, sshd will not allow it to be used unless the StrictModes option has been set to “no”.
    
    • gitserver 上 git 用户 authorized_keys 文件访问权限问题
      如果 authorized_keys 文件、$HOME/.ssh 目录 或 $HOME 目录让本用户拥有者之外的用户有写权限,那么 sshd 就会拒绝使用 ~/.ssh/authorized_keys 文件中的 key 来进行认证。

      可以通过更多了解 sshd 命令,来了解对 authorized_keys 文件的要求。

      Git on the Server - Setting Up the Server 上也有相关内容的描述。

    • Xshell 登录时出现:The selected user key is not registered in the remote host. Try again.
      说明 authorized_keys 文件可能因某种原因被破坏,查看一下,把公钥正确填入即可,也有可能你的用户名(user name)填错了。

    • 相关文件
      /etc/ssh/ssh_config ;the ssh client system-wide configuration file.
      /etc/ssh/sshd_config ;the sshd server system-wide configuration file. # StrictModes 参数设置。
      ~/.ssh/config ; user-specific file.

      **See ssh_config(5) for more information. **

    • 上述文件访问权限过宽等原因可能导致以下现象
      git clone 时报告:Permission denied (publickey) ; 说明 publickey 有问题。

    git clone git@192.168.99.236:/home/git/wanpinghui/wphdoc.git
    Cloning into 'wphdoc'...
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights
    and the repository exists.

    公钥私钥示例

    公钥

    公钥文件内容示例:ssh-rsa AAAAB3NzaC1yc2E...W6xHD comment
    其中:通过 ssh-keygen 命令 -C 参数可以指定 comment 内容。

    私钥
    -----BEGIN RSA PRIVATE KEY-----
    MIIEoQIBAAKCAQEAxm+gAzG1HtQl27GBdpOGBJu9MPuTIT3Z/Wp8SlOKiCzhJhTV
    eVOwP/kG4wlIn4/p5QIMs3Fyf9itO9YEsRI2jtIKFeBldtmNAGTWRkAr2ZHuw1bX
    ...
    CqsCgYBwgLBNyGRguDiWq2Dt+yqmtNF9NqadCPoUiObhnRrhEPGURF0SfZt+xcCv
    y2vQmlg8an3aMi+LiIsex+m4Ty7opdHoBlmImlySxmWMQ+PHT8V5xqe8/NYQ4B3A
    V0wqjVbl6vM+9DM+mch7gIS8OV5k4ViOPvs7CjdjULJ12MK68g==
    -----END RSA PRIVATE KEY-----
    

    相关文章

      网友评论

        本文标题:使用 SSH 密钥进行远程登录

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