[TOC]
0x00 場(chǎng)景
當(dāng)你想在不同網(wǎng)站使用不同密鑰的時(shí)候,可以參考。
當(dāng)你想讓別人免密鑰登錄你的服務(wù)器時(shí),可以參考。
在此做個(gè)記錄。
0x01 創(chuàng)建ssh Key
ssh-keygen -t rsa -C "name@domain"
例如:
? .ssh ssh-keygen -t rsa -C "ttdevs@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/ttdevs/.ssh/id_rsa): id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
The key fingerprint is:
SHA256:ppip7b06WzMF6Qr/tnxubzR11aEXkL6/G8YOi/3Vkcw ttdevs@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
| .ooo|
| . .. +|
| o .. o |
| . . ..= .|
| . . S . ..E |
| o = + o .. o|
| * = . . ..+o|
| o.=.oo . o =.o|
| ..==**.o.. o.=o|
+----[SHA256]-----+
? .ssh ll
total 64
-rw------- 1 ttdevs staff 0B 11 4 09:51 authorized_keys
-rw-r--r--@ 1 ttdevs staff 663B 10 22 16:03 config
-rw------- 1 ttdevs staff 1.6K 11 4 09:48 id_rsa
-rw-r--r-- 1 ttdevs staff 398B 11 4 09:48 id_rsa.pub
-rw------- 1 ttdevs staff 1.6K 11 4 09:48 id_rsa_github
-rw-r--r-- 1 ttdevs staff 398B 11 4 09:48 id_rsa_github.pub
-rw-r--r-- 1 ttdevs staff 4.5K 10 30 01:21 known_hosts
-r--------@ 1 ttdevs staff 1.7K 1 7 2016 rpikey.pem
? .ssh
在 Enter file in which to save the key 后輸入key的名字,其他直接回車。
0x02 配置
看到我的列表中有個(gè) config 文件,這個(gè)就是用來配置的多key的,如果沒有過請(qǐng)自行創(chuàng)建:touch config。下面編輯這個(gè)文件,輸入下面內(nèi)容:
Host gitee.com
HostName gitee.com
IdentityFile ~/.ssh/id_rsa
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
User ttdevs
意思很簡(jiǎn)單,制定相應(yīng)的服務(wù)使用不同的key。
0x03 作為服務(wù)器
上面提到的是登錄別的服務(wù)器,如果希望別人通過ssh免密鑰登錄我們可以可以實(shí)現(xiàn)??吹缴厦娴?authorized_keys 文件了,如果沒有創(chuàng)建一個(gè),修改權(quán)限為 0600。將對(duì)應(yīng)的pub key添加進(jìn)去就可以:
? .ssh touch authorized_keys
? .ssh chmod 0600 authorized_keys
? .ssh cat id_rsa.pub >> authorized_keys