ssh免密登录Linux服务器,GitLab公钥免密管理Git源码
目标:在开发机或jenkins服务器,ssh免密连接服务器,或免密拉取Gitlab代码。
1、在“开发机”或“jenkins服务器”生成密钥,打开终端命令行输入:
1 2 3 |
ssh-keygen -t rsa -C "your_email@example.com" |
1 2 3 4 5 6 |
#Generating public/private rsa key pair. Enter file in which to save the key (/Users/barryng/.ssh/id_rsa): // 一般保持默认既可(也可以修改文件名,比如:/Users/barryng/.ssh/id_rsa_server001) Enter passphrase (empty for no passphrase): Enter same passphrase again: // 点击回车既可 |
2、在“目标服务器”进行授权,有2个方法,二选一既可:
方法一:把id_rsa.pub的内容append到“目标服务器”的~/.ssh/authorized_keys里
方法二:终端输入命令 ssh-copy-id -i id_rsa.pub root@192.168.0.110(目标服务器:username@ip)
使用ssh登录一次服务器:ssh root@192.168.0.110
如果不需要输入密码则成功。
3、如果有一台机需要对应多个服务器免密登录:
可以创建多套密钥文件,注意需要不同的文件名,比如:ids_rsa、ids_rsa_server_001、ids_rsa_server_002。
创建~/.ssh/config文件进行指定登录什么服务器采用什么密钥,内容格式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# coding.net Host git.coding.net User your_email@example.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # dev-tencent Host git.dev.tencent.com User your_email@example.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # project001 Host 192.168.0.110 User your_email@example.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_server_001 # project002 Host 192.168.0.112 User your_email@example.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_server_002 |
邓子乐
2019年3月26日 - 下午11:01
这个教程是教如何生成key,免密码登陆吗?如果我没有 ssh-keygen 哪里找呢?http://dev.tencent.com 怎样配置呢?
barryng
2019年3月27日 - 上午3:28
1、linux和MacOS的终端都自带ssh-keygen的,如果是windows则需要安装其他软件,比如powershell(据说Win10 自带的 OpenSSH 客户端了,可以试试)
2、dev.tencent.com可以参考这个教程:https://dev.tencent.com/help/doc/faq/bbe781aee786/ssh