ssh 免登录
场景:
jenkins 需要自动登陆到远程服务器
1. 生成 ssh 公钥和私钥(在 jenkins 服务器上执行)
xiluhua@vm-xiluhua ~ $ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/xiluhua/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/xiluhua/.ssh/id_rsa. Your public key has been saved in /home/xiluhua/.ssh/id_rsa.pub. The key fingerprint is: 4f:59:70:07:ac:5d:78:ed:b2:98:de:8d:9f:eb:73:36 xiluhua@vm-xiluhua The key's randomart image is: +--[ RSA 2048]----+ | ..oo.. | | oo.o .| | o.o . | | .o. . .| | S o o o | | o o . | | .. . o | | . oE=| | .**| +-----------------+
2. 查看生成结果
xiluhua@vm-xiluhua ~ $ cd /home/xiluhua/.ssh/ xiluhua@vm-xiluhua ~/.ssh $ ll 总用量 12 -rw-------. 1 xiluhua xiluhua 1675 2017/07/26 12:57:57 id_rsa -rw-r--r--. 1 xiluhua xiluhua 400 2017/07/26 12:57:57 id_rsa.pub -rw-r--r--. 1 xiluhua xiluhua 971 2017/01/30 21:09:14 known_hosts
3. 将公钥部署到目标客户端(一)
xiluhua@vm-xiluhua ~/.ssh $ ssh-copy-id -i id_rsa.pub loginuser@10.1.118.45 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys xiluhua@192.168.178.132's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'loginuser@10.1.118.45'" and check to make sure that only the key(s) you wanted were added.
3. 将公钥部署到目标客户端(二)
scp -p ~/.ssh/id_rsa.pub loginuser@10.1.118.45:/home/weblogic/.ssh/authorized_keys
实在不行,手动复制到目标机器上也可以
4. 验证
xiluhua@vm-xiluhua ~/.ssh $ ssh loginuser@10.1.118.45 Last login: Tue Jul 25 21:27:55 2017 from 192.168.178.1 ****************************** *** welcome! xiluhua *** ****************************** xiluhua@localhost ~ $
return "SUCCESS";