现在使用Docker部署了Jenkins,想要通过密钥登录远程服务器,而不是账号和密码,该如何处理呢?
1. 进入Jenkins容器,生成密钥
docker exec -it jenkins bin/bash
# 当前是jenkins用户
jenkins@4e3e91f7c0d0:/$ cd ~
# 生成ssh key,直接全部回车
jenkins@4e3e91f7c0d0:/$ ssh-keygen -t rsa
# 获取当前密钥
jenkins@4e3e91f7c0d0:/$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8bzHXXZ1JE/g18zBoTndVh3OzG7JOnopsBhQ++OgX9D1Qin7hro/wMFcDGvxm9C5/ybjuGRSfOTQJRcDesMHsn61mTeNOtVXDSD6EITqiYWBYhM3cQzwAhjCRGHcE73H40aQD77efiTPzV8u+VKFlLUrurPlhSZUDUgMd3E0AvuniCAun34mn/ZYnwECItUy7DWMAbfd9ridhV9upaizVzE9NruoJHVG032N/kco0fpMjoPL9uu8lqxtWpKGij1aY4DQyNo4onuAi7Lov3nxejofs+lioEPB5y66777hFHQUUQbPZDnM3zM4OxpSyL0b9E71WydvQxS0VIBZL jenkins@4e3e91f7c0d0
2. 登录想要部署的服务器,添加ssh-rsa
修改```~/.ssh/authorized_keys``文件,添加刚刚那一串密钥
cd ~/.ssh
# 添加 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8bzHXXZ1JE/g18zBoTndVh3OzG7JOnopsBhQ++OgX9D1Qin7hro/wMFcDGvxm9C5/ybjuGRSfOTQJRcDesMHsn61mTeNOtVXDSD6EITqiYWBYhM3cQzwAhjCRGHcE73H40aQD77efiTPzV8u+VKFlLUrurPlhSZUDUgMd3E0AvuniCAun34mn/ZYnwECItUy7DWMAbfd9ridhV9upaizVzE9NruoJHVG032N/kco0fpMjoPL9uu8lqxtWpKGij1aY4DQyNo4onuAi7Lov3nxejofs+lioEPB5y66777hFHQUUQbPZDnM3zM4OxpSyL0b9E71WydvQxS0VIBZL jenkins@4e3e91f7c0d0
vi authorized_keys
3. 配置Jenkins
如下图,path to key
需要配置好容器里面生成的key的路径,配置好后使用Test Configuration
测试看看