mac使用git管理Github以及生成 SSH 公钥
现在开始设置Mac管理Github,有一点需要知道的是本地的git仓库和Github服务器之间是通过ssh加密的。
在终端执行
dreamdeMacBook-Air:~ dream$ ssh -v usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname [command]
明显Mac已经安装了ssh。
创建SSH Key
dreamdeMacBook-Air:~ dreamn$ cd ~ dreamdeMacBook-Air:~ dream$ pwd /Users/dream dreamdeMacBook-Air:~ dream$ cd .ssh -bash: cd: .ssh: No such file or directory dreamdeMacBook-Air:~ dream$
进入当前的用户目录,波浪线表示的是当前目录。判断是否已经安装了.ssh,避免默认安装会覆盖之前安装的。明显当前目录没有该文件
执行创建 ssh key
ssh-keygen -t rsa -C youremail@example.com(你的Github登陆名)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
首先在本地初始化一个git仓库 由于之前没有配置用户名,所以首次commit会有提示,自动建立 设置方式 git config --global user.name Your Name git config --global user.email email@example.com
接着都是回车,选择默认的目录,默认的密码即可
dreamdeMacBook-Air:~ dream$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/Users/dream/.ssh/id_rsa): Created directory '/Users/dream/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/dream/.ssh/id_rsa. Your public key has been saved in /Users/dream/.ssh/id_rsa.pub. The key fingerprint is: SHA256:+IzVbBCP8mX0f+KVkgwxHjgUFiboXrn7dqutv3F1CCk dream@dreamdeMacBook-Air.local The key's randomart image is: +---[RSA 2048]----+ | ..o*=+ | | . +B..+. | | . . + Eo+ | | . * * .oo...| | . o S + ++o+| | . * . .o+.| | . + . .. | | . ...o | | o+==o | +----[SHA256]-----+
接着可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对
Your identification has been saved in /Users/mesut/.ssh/id_rsa.
Your public key has been saved in /Users/mesut/.ssh/id_rsa.pub.
dreamdeMacBook-Air:~ dream$ cd .ssh dreamdeMacBook-Air:.ssh dream$ ls id_rsa id_rsa.pub
如何查看公钥?命令cat ~/.ssh/id_rsa.pub
dreamdeMacBook-Air:.ssh dream$ cat ~/.ssh/id_rsa.pub ssh-rsa 。。。。。。。。。。。。。。。。。。。 dreamdeMacBook-Air:.ssh dream$
将SSH key添加到GitHub。登录到GitHub页面,Account Settings->SSH Public Keys->Add another key
将生成的key(id_rsa.pub文件)内容copy到输入框中,save。
commd+shift+g进入/Users/chen/.ssh/就可以看得见私钥和公钥
私钥的名字是 id_rsa,是服务器确定你身份的唯一凭证。
公钥的名字是id_rsa.pub。把这个文件发给仓库管理员,仓库管理员会把这个公钥放到服务器上,以后git就通过上面的私钥跟服务器交互了。如果使用github就是自己把公钥内容添加上去