博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux下实现免密登录
阅读量:5037 次
发布时间:2019-06-12

本文共 2232 字,大约阅读时间需要 7 分钟。

过程如下:

1.Linux下生成密钥

通过命令”ssh-keygen -t rsa“

 2.1 通过ssh-copy-id的方式

    命令: ssh-copy-id -i ~/.ssh/id_rsa.put <romte_ip>

[root@
test
.
ssh
]
# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135
root@192.168.91.135's password:
Now try logging into the machine, with
"ssh '192.168.91.135'"
, and check
in
:
 
.
ssh
/authorized_keys
 
to
make
sure we haven
't added extra keys that you weren'
t expecting.
 
[root@
test
.
ssh
]
# ssh root@192.168.91.135
Last login: Mon Oct 10 01:25:49 2016 from 192.168.91.133
[root@localhost ~]
#
 
2.2 通过scp将内容写到对方的文件中
命令:scp -p ~/.ssh/id_rsa.pub root@<remote_ip>:/root/.ssh/authorized_keys
[root@
test
.
ssh
]
# scp -p ~/.ssh/id_rsa.pub root@192.168.91.135:/root/.ssh/authorized_keys
root@192.168.91.135's password:
id_rsa.pub 100% 408 0.4KB
/s
00:00
[root@
test
.
ssh
]
#
[root@
test
.
ssh
]
#
[root@
test
.
ssh
]
#
[root@
test
.
ssh
]
# ssh root@192.168.91.135
Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133
[root@localhost ~]
#
 

2.3 通过Ansible实现批量免密

2.3.1 将需要做免密操作的机器hosts添加到/etc/ansible/hosts下:

  [Avoid close]

  192.168.91.132
  192.168.91.133
  192.168.91.134

2.3.2 执行命令进行免密操作

  ansible <groupname> -m authorized_key -a "user=root key='{

{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@
test
sshpass-1.05]
# ansible test -m authorized_key -a "user=root key='{
{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
  SSH password: ----->输入密码
  192.168.91.135 | success >> {
  
"changed"
:
true
,
  
"key"
:
"ssh-rsa    AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SM  WQmpKUltj2nltuv3w== root@localhost.localdomain"
,
  
"key_options"
: null,
  
"keyfile"
:
"/root/.ssh/authorized_keys"
,
  
"manage_dir"
:
true
,
  
"path"
: null,
  
"state"
:
"present"
,
  
"unique"
:
false
,
  
"user"
:
"root"
  }
  [root@
test
sshpass-1.05]
#
 

2.4 手工复制粘贴的方式

  将本地id_rsa.pub文件的内容拷贝至远程服务器的~/.ssh/authorized_keys文件中

 

转载于:https://www.cnblogs.com/linwenbin/p/10368646.html

你可能感兴趣的文章
LR_问题_如何将场景中的用户设置为百分比形式
查看>>
OpenShift-OKD3.10基础环境部署
查看>>
工程师淘金:开发Android主攻四大方向
查看>>
ASP.NET MVC——Controller的激活
查看>>
javascript中Array对象
查看>>
SQLSERVER中查看谁占用了Buffer Pool
查看>>
lamp环境安装shell脚本
查看>>
ASP.NET MVC使用jQuery实现Autocomplete
查看>>
model中字段格式验证
查看>>
host路径
查看>>
查看linux 内存
查看>>
HTTP 状态码
查看>>
Ubuntu 14.10 下卸载MySQL
查看>>
练习题 求字符串是否为回文
查看>>
为了兼容性问题,本人一律淘汰不兼容如下三种浏览器的js
查看>>
RowFilter 对于已获取到的dataset进行过滤
查看>>
451. Sort Characters By Frequency
查看>>
第十五周总结
查看>>
java学习笔记-hibernate基础(1)
查看>>
jQuery属性操作
查看>>