云服務(wù)商 青云 提供的服務(wù)和編寫的 使用 SSH 密鑰遠(yuǎn)程登錄訪問主機(jī) 值得參考。
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 會(huì)檢查這個(gè)訪問權(quán)限是否合理帆吻,如果別人可訪問域那,則會(huì)拒絕或者忽略該私鑰文件。ssh -i /path/to/kp-1234abcd [user@]hostname
// -i 指明 identity_file猜煮。無參運(yùn)行 ssh 可看幫助次员。ssh [user@]hostname
// 如果你已經(jīng)配置過 ~/.ssh/config,則看一下這個(gè)配置文件王带,ssh 使用會(huì)更簡(jiǎn)單淑蔚。
參閱:/etc/ssh/ssh_config,man ssh_configUsing the SSH Config File愕撰,這篇講得很清晰刹衫。
# ~/.ssh/config 文件示例
# Host 參數(shù)標(biāo)明以下內(nèi)容僅適用于訪問 236 主機(jī)時(shí)適用,Host 參數(shù)本身只是一個(gè)入口字符串搞挣,比如:·`ssh 236` 就表示你使用 236 這個(gè) 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
- 在一個(gè) config 文件中可以 配置多個(gè)訪問遠(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
使用時(shí):git clone git@github-jexchan:jexchan/xtry.git
,其中 git@github-jexchan
指明用戶和主機(jī)蝇摸,github-jexchan
就是 Host 的值婶肩,因?yàn)樵?config 里面配置了 User git
办陷,在 git clone 時(shí)可以不再指定 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,才會(huì)生效跷跪,否則會(huì)報(bào)告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橡羞,這個(gè)環(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 密鑰對(duì)
云主機(jī)在生成 SSH 公鑰/私鑰密鑰對(duì)時(shí),會(huì)要求你立刻下載其私鑰覆致,并保管好私鑰侄旬,云服務(wù)商是不保存你的私鑰的,只有公鑰(public key)在云主機(jī)上煌妈。你在使用 SSH 時(shí)用到的 identity file 就是私鑰(private key)文件儡羔。
使用 ssh-keygen 命令也可以自己生成。
-
ssh-keygen 命令
密鑰對(duì)可通過ssh-keygen -t rsa -C 'comment' -f filename-of-key-file
生成璧诵。該命令通常在 /usr/bin 下汰蜘。
生成 2 個(gè)文件:filename-of-key-file(私鑰) 和 filename-of-key-file.pub(公鑰)。
不指定 -f 參數(shù)之宿,則默認(rèn)存到 ~/.ssh/下族操,生成 id_rsa 和 id_rsa.pub 兩個(gè)文件。
不指定 -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 文件中娇昙,一行一個(gè)。凡持有和其中任一公鑰配對(duì)的私鑰的用戶都可以訪問笤妙。
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 就會(huì)拒絕使用 ~/.ssh/authorized_keys 文件中的 key 來進(jìn)行認(rèn)證铃诬。可以通過更多了解 sshd 命令,來了解對(duì) authorized_keys 文件的要求薄嫡。
Git on the Server - Setting Up the Server 上也有相關(guān)內(nèi)容的描述氧急。
Xshell 登錄時(shí)出現(xiàn):The selected user key is not registered in the remote host. Try again.
說明 authorized_keys 文件可能因某種原因被破壞,查看一下毫深,把公鑰正確填入即可,也有可能你的用戶名(user name)填錯(cuò)了毒姨。-
相關(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 時(shí)報(bào)告: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-----