Linux下如何配置ssh免密登录(linux,ssh,开发技术)

时间:2024-05-04 23:27:09 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

SSH 是 Linux 系统的登录工具,现在广泛用于服务器登录和各种加密通信。

Linux下如何配置ssh免密登录

1.Linux下生成密钥

ssh-keygen的命令手册,通过”man ssh-keygen“命令: Linux下如何配置ssh免密登录

通过命令”ssh-keygen -t rsa“ Linux下如何配置ssh免密登录

生成之后会在用户的根目录生成一个 “.ssh”的文件夹 Linux下如何配置ssh免密登录

进入“.ssh”会生成以下几个文件 Linux下如何配置ssh免密登录

authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥id_rsa:生成的私钥文件id_rsa.pub:生成的公钥文件know_hosts:已知的主机公钥清单如果希望ssh公钥生效需满足至少下面两个条件:

\1) .ssh目录的权限必须是700 2) .ssh/authorized_keys文件权限必须是600

2.远程免密登录

原理图:

Linux下如何配置ssh免密登录
Linux下ssh高级使用–免密登录Linux下ssh高级使用–免密登录

常用以下几种方法:

2.1 通过ssh-copy-id的方式

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

举例:

[root@test.ssh]#ssh-copy-id-i~/.ssh/id_rsa.pub192.168.91.135root@192.168.91.135'spassword:Nowtryloggingintothemachine,with"ssh'192.168.91.135'",andcheckin:.ssh/authorized_keystomakesurewehaven'taddedextrakeysthatyouweren'texpecting.[root@test.ssh]#sshroot@192.168.91.135Lastlogin:MonOct1001:25:492016from192.168.91.133[root@localhost~]#

常见错误:

[root@test~]#ssh-copy-id-i~/.ssh/id_rsa.pub192.168.91.135-bash:ssh-copy-id:commandnotfound//提示命令不存在

解决办法:yum -y install openssh-clients

2.2 通过scp将内容写到对方的文件中

命令:scp -p ~/.ssh/id_rsa.pub root@:/root/.ssh/authorized_keys

举例:

[root@test.ssh]#scp-p~/.ssh/id_rsa.pubroot@192.168.91.135:/root/.ssh/authorized_keysroot@192.168.91.135'spassword:id_rsa.pub100%4080.4KB/s00:00[root@test.ssh]#[root@test.ssh]#sshroot@192.168.91.135Lastlogin:MonOct1001:27:022016from192.168.91.133[root@localhost~]#

2.3 通过Ansible实现批量免密

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

[Avoidclose]192.168.91.132192.168.91.133192.168.91.134

2.3.2 执行命令进行免密操作

ansible -m authorized_key -a “user=root key='{{ lookup(‘file’,’/root/.ssh/id_rsa.pub’) }}'” -k

示例:

[root@testsshpass-1.05]#ansibletest-mauthorized_key-a"user=rootkey='{{lookup('file','/root/.ssh/id_rsa.pub')}}'"-kSSHpassword:----->输入密码192.168.91.135|success>>{"changed":true,"key":"ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVPPejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SMWQmpKUltj2nltuv3w==root@localhost.localdomain","key_options":null,"keyfile":"/root/.ssh/authorized_keys","manage_dir":true,"path":null,"state":"present","unique":false,"user":"root"}[root@testsshpass-1.05]#

2.4 手工复制粘贴的方式

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


 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Linux下如何配置ssh免密登录的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Linux系统文件类型有哪些下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18