现在使用git等很多,自己也尝试了下,结果在安装后执行一下命令等时候总是报permission denied
ssh -T git@github.com
各种baidu终于搞定。
想要使用git必须先安装git,可以自行baidu安装并注册github账号。这里就不必多说。
打开终端输入以下命令查看是否存在.ssh文件夹
$cd ~/.ssh
如果存在会返回以下信息
Last login: Sat Jul1 17:45:48 on ttys000
pantieruideMacBook-Pro:~ pantierui$ cd ~/.ssh
pantieruideMacBook-Pro:.ssh pantierui$
如果不存在,可以使用mkdir创建一个.ssh文件夹
通过以下命令默认参数生成ssh的key
ssh-keygen -t rsa -C xxxxx@gmail.com(注册github时的email)
回车后如下:
pantieruideMacBook-Pro:~ pantierui$ ssh-keygen -t rsa -C "pantierui@hotmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/pantierui/.ssh/id_rsa): xxxxxxx(此处为key等名字)
Enter passphrase (empty for no passphrase):(此处为密码,git验证时用)
Enter same passphrase again:(再次输入密码)
Your identification has been saved in pantierui.
Your public key has been saved in pantierui.pub.
The key fingerprint is:
SHA256:QlhWPjXi1AjbWFiMvalu+yFv4d2NfUj41mhWZkJ196c pantierui@hotmail.com
The key's randomart image is:
+---[RSA 2048]----+
|+O*oo|
|+oO+o..+|
|. + =o.+|
|.o.. o|
|..So ..|
|.... E +|
|....o . * O |
|ooo.. o X o|
|..+o+ . |
+----[SHA256]-----+
pantieruideMacBook-Pro:~ pantierui$
此时用以下命令查看生成等密钥(确保当前目录为.ssh)
cat pantierui.pub (此文件为刚刚生成的,记录下来以下步骤会用到)
登陆github网站,找到自己用的的【setting】下的【SSH and GPG keys】,点击【New SSH key】
【title】此处随便起个名字
【Key】此处就是刚刚cat出来的内容
执行以下命令验证是否可以链接到github
ssh -T git@github.com
会提示以下信息
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
回复yes,继续。此时会报以下错误
Permission denied (publickey).
使用以下命令查看log,诊断信息如下
pantieruideMacBook-Pro:.ssh pantierui$ ssh -T -v git@github.com
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [192.30.255.112] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/pantierui/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/pantierui/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/pantierui/.ssh/id_rsa
debug1: Trying private key: /Users/pantierui/.ssh/id_dsa
debug1: Trying private key: /Users/pantierui/.ssh/id_ecdsa
debug1: Trying private key: /Users/pantierui/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
因为ssh会找id_rsa和id_rsa.pub文件,注意我们生成的pantierui和pantierui.pub不等于前面的两个文件,顾验证失败。使用以下命令复制出上名的文件
cp pantierui id_rsa && cp pantierui.pub id_rsa.pub
重新执行验证命令
ssh -T git@github.com
需要回复密码
Enter passphrase for key '/Users/pantierui/.ssh/id_rsa':(此处为创建key时的密钥)
Hi pantierui! You've successfully authenticated, but GitHub does not provide shell access.
如果出现上述信息就可以使用github了。