使用 ssh 密鑰進行遠程登錄

云服務商 青云 提供的服務和編寫的 使用 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

  • Using the SSH Config File,這篇講得很清晰。

# ~/.ssh/config 文件示例
# Host 參數標明以下內容僅適用于訪問 236 主機時適用,Host 參數本身只是一個入口字符串,比如:·`ssh 236` 就表示你使用 236 這個 Host 的配置。
Host 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,其中 git@github-jexchan 指明用戶和主機,github-jexchan 就是 Host 的值,因為在 config 里面配置了 User git,在 git clone 時可以不再指定 git@ 了,即 git clone github-jexchan:jexchan/xtry.git。端口默認就是 22。

  • GIT_SSH_COMMAND 方法 @ How to tell git which private key to use?
    GIT_SSH_COMMAND="ssh -i ~/.ssh/rsa-michael-gitolite-3 -F /dev/null" git clone michael@192.168.99.3:gitolite-admin.git了解 gitolite

  • GitHub Clone with SSH 的默認樣式:git@github.com:jexchan/xtry.git,就用默認即可,通用挺好;

Host github.com
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/github_brahmsjia

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-----

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容