使用SSH协议,您可以连接和验证远程服务器和服务。使用SSH密钥,您可以在每次访问时无需提供用户名或密码即可连接到GitHub。
1. 在生成SSH密钥之前,您可以检查是否已存在SSH密钥。
在OpenSSH 7.0中不推荐使用DSA密钥,而使用RSA密钥。 例如,MacOS Sierra操作系统
$ cd ~/.ssh
$ ls -al ~/.ssh
total 40
drwxr-xr-x 6 apple staff 192 6 12 2018 .
drwxr-xr-x+ 69 apple staff 2208 2 20 13:36 ..
-rw-r--r--@ 1 apple staff 6148 6 12 2018 .DS_Store
-rw------- 1 apple staff 1679 6 12 2018 id_rsa
-rw-r--r-- 1 apple staff 399 6 12 2018 id_rsa.pub
-rw-r--r-- 1 apple staff 419 6 12 2018 known_hosts
.pub 文件是你的公钥,另一个则是私钥
2. 查看文件内容
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDaAMofa59is3t5L7yzIxDiYsP1iLwTeeqARosE/AjXiZCQumFNlT+5b5qfPyWBT2vZGsP7QMUecPPJCmaCc61efUw/M8ES5gUi4MAIGETp8ovYDyEJOqnNDS6xE/+mForB98ONoMS4K5Tr+Z+DsEIGExyxrhBq6lSEk2xEVar1eOAbTNs+rzi9Mb1FxQ7Co+pliNisG8nTceKoCd67byxuJsjqebirWR8ZUDNytFdn55gI8zCFhzGzv+UL+VD10aL9PekfhFhltrnjydiKDmvfqdr6QN2WkG4N7MsERquIbIe7IKDvqTOU7G8lzLtopXuzqU78Fl0Wc0+8E+6vUTVF apple@grj-MacBookPro.local
$ cat ~/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAQEA2gDKH2ufYrN7eS+8syMQ4mLD9Yi8E3nqgEaLBPwI14mQkLphTZU/
uW+anz8lgU9r2RrD+0DFHnDzyQpmgnOtXn1MPzPBEuYFIuDACBhE6fKL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
SFNhvbjso2gqp8ug1iaGGjDJHtH6hyp3BrIiuITwPKQ7iiliW24+6M8AlI6EsfRKNgzusV
vZKqVo2y4NuwAAAIEA/U/mE/mZadKLyUiQ6pn15JsJU6zeaRuYuweBQgryAZLJr1DUgNty
6J5yOgcryA7G6GyXIogHLUJSjApoCpGXZOnau+31rPmvMj8Ths/GojY66iYh88yKZSp2KI
9xKXq3DgJVc077fG+lAhiS5wu6ubY5zxtK4J0XTHPYpWK86qUAAACBANxQ+g57HmMb12kt
eSTJr1njOcx13ceg4mnXc+D4RtD1yLkld3i1HVZ/qM5a4ahMlB1Zu5HswNG22bKdufv6iS
aF+ahnBgmMiPlJaioAvagu6xejSqhSC9eIxgmlWmACpRqXY4OHLFqHY3TyGFP5SWJf+MCb
y9mRvGzM/bSRJz4hAAAAGmFwcGxlQGdyai1NYWNCb29rUHJvLmxvY2Fs
-----END OPENSSH PRIVATE KEY-----
$ cat ~/.ssh/known_hosts
[git.hb.cn]:9999,[192.168.0.199]:9999 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbcxiG0auRvY5d4asMyD8W/RoIJbeYEhuOLSeOhtJkfei3OT+fP5pk/n+Fyw2nj4RDANqrC/P4X/7Ja4zE5FE+lh3TgWiAFORHgfnuHqBI6ILinc8UbfsSbhzot3ASg6Z84augM5rBj8jP/Nt/rl7oXb1fu389HVcUmMGB1YLzc5XPH+3xlJ4aPGngmlfqB1BLuoyQS7Q1ubBsQzYk1ccggudks3cso+EvABDyAPYql03/NK+xB6IStFwTl2nKawscv9+e2+gMIcyvLKwZhuvXXptsgPXxtDhGCakj7yRqztp1UaF51cYMjc7folGajOTmf7S77iP1tK5dLpAk6twV
3. 创建新的
首先 ssh-keygen
会确认密钥的存储位置(默认是.ssh/id_rsa
),然后它会要求你输入两次密钥口令。如果你不想在使用密钥时输入口令,将其留空即可.
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/apple/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/apple/.ssh/id_rsa.
Your public key has been saved in /Users/apple/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NMh7hAvqSclzSwx82eHgYJmM5xdH237bZocBnxrCOqg apple@grj-MacBookPro.local
The key's randomart image is:
+---[RSA 2048]----+
| ooo..o |
|.o=o.*.* |
| oo =oB = . |
| ..*.. B . o . |
| *.+ o S o + |
| o = o o o = o |
| o o o o = . |
| . . o . |
| E |
+----[SHA256]-----+
关于在多种操作系统中生成 SSH 密钥的更深入教程,请参阅 GitHub 的 SSH 密钥指南
网友评论