云服務(wù)商 青云 提供的服務(wù)和編寫的 使用 SSH 密鑰遠(yuǎn)程登錄訪問主機 值得參考。
Linux 環(huán)境
CentOS 源于 Red Hat Enterprise Linux(RHEL),了解 RHEL 6: OpenSSH,RHEL 7: OpenSSH
chmod 700 /path/to/
chmod 600 /path/to/kp-1234abcd
// 將私鑰文件設(shè)置為只有自己可讀寫。ssh 會檢查這個訪問權(quán)限是否合理,如果別人可訪問,則會拒絕或者忽略該私鑰文件。ssh -i /path/to/kp-1234abcd [user@]hostname
// -i 指明 identity_file。無參運行 ssh 可看幫助。ssh [user@]hostname
// 如果你已經(jīng)配置過 ~/.ssh/config,則看一下這個配置文件,ssh 使用會更簡單。
參閱:/etc/ssh/ssh_config,man ssh_configUsing the SSH Config File,這篇講得很清晰。
# ~/.ssh/config 文件示例
# Host 參數(shù)標(biāo)明以下內(nèi)容僅適用于訪問 236 主機時適用,Host 參數(shù)本身只是一個入口字符串,比如:·`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
- 在一個 config 文件中可以 配置多個訪問遠(yuǎn)程 git 庫 的 Host,指定不同的 ssh key IdentityFile;talks on stackoverflow;
# 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
。端口默認(rèn)就是 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 的默認(rèn)樣式:
git@github.com:jexchan/xtry.git
,就用默認(rèn)即可,通用挺好;
Host github.com
HostName github.com
User git
Port 22
IdentityFile ~/.ssh/github_brahmsjia
ssh options
-i identity_file
-l login_name
-p port-
sshd 服務(wù)
文件 /etc/ssh/sshd_config 中 PermitUserEnvironment 參數(shù)設(shè)置 @ sshd_config.5PermitUserEnvironment yes
該參數(shù)設(shè)置后,文件 ~/.ssh/authorized_keys 中的 environment= options,才會生效,否則會報告Permission denied (publickey)
。
PermitUserEnvironment 設(shè)置為 yes 存在一定安全性;文件
AUTHORIZED_KEYS FILE FORMAT
@ sshd.8environment="NAME=value"
Mac 環(huán)境
在 Terminal 下參考 Linux 方法。
通常下載的文件放在 ~/Downloads/目錄下。
Windows 環(huán)境
Git Bash 方法
如果你安裝過 git for windows,想必熟悉 Git Bash,這個環(huán)境和 *nix 風(fēng)格保持一致,可以參考 Linux 方法。
Xshell 方法
Create a New Session
Xshell - Authentication - Method 選擇 Public Key
Xshell - 導(dǎo)入私鑰文件
Terminal Encoding:Unicode (UTF-8)
設(shè)置 UTF-8 以正常顯示中文。
Appearance Font
ssh user@hostname
在界面上配置好后,在 Xshell 命令行下:
Xshell:> ssh user@hostname 即可。注意:Xshell 下的 ssh 功能比較弱。
關(guān)于密鑰和 authorized_keys 文件
- 以下的內(nèi)容由你的服務(wù)器管理員為你做好配置;
- SSH 密鑰對
云主機在生成 SSH 公鑰/私鑰密鑰對時,會要求你立刻下載其私鑰,并保管好私鑰,云服務(wù)商是不保存你的私鑰的,只有公鑰(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 參數(shù),則默認(rèn)存到 ~/.ssh/下,生成 id_rsa 和 id_rsa.pub 兩個文件。
不指定 -C 參數(shù),則 comment 內(nèi)容默認(rèn)為user@hostname
。我習(xí)慣指定 -C 參數(shù)值和密鑰文件名一致,比如:rsa-hostip-usr,rsa-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/ ;注意權(quán)限!
chmod 600 .ssh/authorized_keys ;注意權(quán)限!
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 文件訪問權(quán)限問題
如果 authorized_keys 文件、HOME 目錄讓本用戶擁有者之外的用戶有寫權(quán)限,那么 sshd 就會拒絕使用 ~/.ssh/authorized_keys 文件中的 key 來進(jìn)行認(rèn)證。
可以通過更多了解 sshd 命令,來了解對 authorized_keys 文件的要求。
Git on the Server - Setting Up the Server 上也有相關(guān)內(nèi)容的描述。
Xshell 登錄時出現(xiàn):The selected user key is not registered in the remote host. Try again.
說明 authorized_keys 文件可能因某種原因被破壞,查看一下,把公鑰正確填入即可,也有可能你的用戶名(user name)填錯了。-
相關(guān)文件
/etc/ssh/ssh_config
;the ssh client system-wide configuration file.
/etc/ssh/sshd_config
;the sshd server system-wide configuration file. # StrictModes 參數(shù)設(shè)置。
~/.ssh/config
; user-specific file.**See ssh_config(5) for more information. **
上述文件訪問權(quán)限過寬等原因可能導(dǎo)致以下現(xiàn)象
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.
公鑰私鑰示例
公鑰
公鑰文件內(nèi)容示例:ssh-rsa AAAAB3NzaC1yc2E...W6xHD comment
其中:通過 ssh-keygen 命令 -C 參數(shù)可以指定 comment 內(nèi)容。
私鑰
-----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-----