我们一般用http的形式拉取代码。
ssh的好处就是不用每次输入密码,而且貌似会快丢丢,不知道是不是错觉。
大概需要三个步骤:
一、本地生成密钥对;
二、设置github上的公钥;
三、修改git的remote url为git协议。
一、生成密钥对。
SSH 公钥默认储存在账户的主目录下的 ~/.ssh 目录。
进入服务器的对应目录:
$ cd ~/.ssh $ ls authorized_keys2 id_dsa known_hosts config id_dsa.pub
生成:
$ ssh-keygen -t rsa -C " your_email@youremail.com"
执行下:
[root@iZbp16x1iba3qb7a1cuu20Z bin]# ssh-keygen -t rsa -C "test" Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:xtPk6o3L...CfcOK7UCo test The key's randomart image is: +---[RSA 2048]----+ | ..o ..o . | | . o o.+ | | o . ++. | | +.o+ ..=.. | | . ++o.o o | | ..=o+o . | | o oE=o . | | . ooo=+. . | | .oo.o*oo=o | +----[SHA256]-----+
二、添加公钥到你的git帐户
[root@iZbp16x1iba3qb7a1cuu20Z bin]# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAA...HqATKQ7MQPkuBDU1+sdUEv7 test
直接粘贴进去就好了
三、修改你本地的ssh remote url. 不用https协议,改用git 协议
可以用git remote -v 查看你当前的remote url
$ git remote -v origin <https://github.com/test1/test1.git> (fetch) origin <https://github.com/test1/test1.git> (push)
使用 git remote set-url 来调整你的url。
git remote set-url origin git@github.com:test1/test1.git
完了之后,你便可以再用 git remote -v 查看一下。
测试一下:
/usr/bin/git ls-remote -h git地址
查看私钥:
[root@iZbp16x1iba3qb7a1cuu20Z bin]# cat ~/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAzNZXYdsjoPIPUhOyFaco/HDZ9kAXeQgwf/X9apZVB7NsMBKk ... ... ... TbIuzX1zvSud4AB+RslD2TB0Tn2Ck2lCBAN61ted1jc2ZIPMVk9HLA== -----END RSA PRIVATE KEY-----
可以填入Jenkins等工具中。
本文介绍了如何通过SSH生成密钥对,将公钥添加到GitHub账户,以及如何将远程URL从HTTPS改为Git协议,以便更便捷地使用git进行代码管理,包括在Jenkins等工具中的应用。
991

被折叠的 条评论
为什么被折叠?



