使用多个git服务器每次都需要输入帐号密码,这个是不能忍受的。
可以使用ssh配置公钥快速登录,不需要再输入密码了,很快捷。
生成/添加SSH公钥
ssh-keygen -t rsa -C "xxxxx@xxxxx.com" -f ~/.ssh/xxx_id_rsa
注意:这里的 xxxxx@xxxxx.com 只是生成的 sshkey 的名称,并不约束或要求具体命名为某个邮箱。
现网的大部分教程均讲解的使用邮箱生成,其一开始的初衷仅仅是为了便于辨识所以使用了邮箱。
-f 指定生成目录
按照提示完成三次回车,即可生成 ssh key。通过查看 ~/.ssh/id_rsa.pub 文件内容,获取到你的 public key
cat ~/.ssh/xxx_id_rsa.pub
多个公钥需要写个配置文件
vim ~/.ssh/config
配置文件如下
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
Host xxx.com
HostName xxx.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/xxx_id_rsa
把公钥添加到服务器上。
gitee、github可以在个人配置页面添加。
自己的服务器,把公钥添加到 ~/.ssh/authorized_keys 文件。
测试
ssh -T git@gitee.com
Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.
有遇到如下问题
xxx@xxx.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)
用root用户修改配置
vim /etc/ssh/sshd_config
把下面这些设置为no
改成
PermitRootLogin no
UsePAM no
PasswordAuthentication no
RSAAuthentication yes
PubkeyAuthentication yes
重启sshd服务
systemctl restart sshd.service
使用普通用户登录
ssh xxx@xxx.com
Last login: Wed Apr 21 07:45:07 2021 from xxx
很方便。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)