十四、SSH免密登入
阿新 • • 發佈:2018-12-16
SSH免密登入
1、客戶端生成公鑰、私鑰
[email protected] MINGW64 ~
$ cd ~ //進入家目錄
[email protected] MINGW64 ~
$ rm -r .ssh //刪除原先的目錄.ssh
rm: cannot remove '.ssh': No such file or directory
[email protected] MINGW64 ~
$ ssh-keygen -t rsa -C [email protected] //生成公鑰、私鑰
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yang/.ssh/id_rsa):
Created directory '/c/Users/yang/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/yang/.ssh/id_rsa.
Your public key has been saved in /c/Users/yang/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:pPSUKaoux0qw9t66ynPdkt2FOn0vNISfZx+v6H5MAIE [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| ... |
| Eo. |
| o = .. |
| o * . .. |
|. . . S + .. |
|.. . . * o.. |
|.+. . + + o +o. o|
|=o+..+ = o o .o..|
|.**=o.. . .o*o.. |
+----[SHA256]-----+
[email protected] MINGW64 ~
$ cd .ssh/
[email protected] MINGW64 ~/.ssh
$ ll
total 5
-rw-r--r-- 1 yang 197121 1679 10月 23 09:12 id_rsa
-rw-r--r-- 1 yang 197121 398 10月 23 09:12 id_rsa.pub
[email protected] MINGW64 ~/.ssh
$ cat id_rsa.pub //獲取公鑰並複製
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCZQfyIhOvVKgmO0aFWGevGHfHx1o4eD2lvZWviJR8VKBDl0bKMD/bjA9Iv6+VkWiFCMQsUGmXBHAPOtB0uvE3feAsDIePCxP1LeeV7CMHLUH04TS7kO+zj99kJddkyGwMejBdnD1rh3UDGn1qDL40G1SFxZnm+AnDH7eO0G9rmEcln4BEhk5Ug5QoTe1td/4vdZtWe9vL0EOPHKi4c/H4+iewTsSISXUvD3SBXZhdp2FnMhtpHa7VHgC0sn+P7T2/Z4UGZZZEfXD0/+xznlAran2xkVZuNnPlvpfkyUkGzMO9CBzzpApMEENfbLs2Byh3ShzIMqKnJR5eHaE+JyEWn [email protected]
2、伺服器端配置公鑰
3、驗證免密登入
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ vim test.txt //編輯檔案
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ git add test.txt //暫存
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ git commit -m "test ssh for remote" test.txt //提交到本地庫
[master a4f04be] test ssh for remote
1 file changed, 2 insertions(+)
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ git remote -v //檢視遠端庫地址及別名
origin https://github.com/907486852/weChat.git (fetch)
origin https://github.com/907486852/weChat.git (push)
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ git remote add origin_ssh [email protected]:907486852/weChat.git //為ssh地址新增別名
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ git remote -v //檢視遠端庫地址
origin https://github.com/907486852/weChat.git (fetch)
origin https://github.com/907486852/weChat.git (push)
origin_ssh [email protected]:907486852/weChat.git (fetch)
origin_ssh [email protected]:907486852/weChat.git (push)
[email protected] MINGW64 /d/GitWorkSpace/weChat (master)
$ git push origin_ssh master //推送到origin_ssh master分支
The authenticity of host 'github.com (192.30.253.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.112' (RSA) to the list of known hosts.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 246 bytes | 61.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:907486852/weChat.git
3eebe6e..a4f04be master -> master