Linux之间配置SSH免密码登

假定有A、B两台Linux服务器,要配置两台服务器的免密码登录,简单执行如下操作即可。文章源自靠谱运维-https://www.ixdba.net/archives/1785

1、在A服务器生成公私钥

[root@iZ23sl33esbZ A]# ssh-keygen -t rsa

一路默认回车,会在系统的/root/.ssh下生成id_rsa、id_rsa.pub文件,其中,id_rsa是私钥,id_rsa.pub是公钥。文章源自靠谱运维-https://www.ixdba.net/archives/1785

要实现从A机器无密码登录到B机器上,只需要讲A机器上的id_rsa.pub拷贝到B机器对应的路径即可。文章源自靠谱运维-https://www.ixdba.net/archives/1785

2、将公钥传输到B服务器

将公钥从A机器传输到B机器,执行如下命令:文章源自靠谱运维-https://www.ixdba.net/archives/1785

[root@iZ23sl33esbZ A]# ssh-copy-id -i /root/.ssh/id_rsa.pub B机器的IP地址

例如:文章源自靠谱运维-https://www.ixdba.net/archives/1785

[root@iZ23sl33esbZ A]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.213.235
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
root@172.16.213.235's password: #输入172.16.213.235的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.213.235'"
and check to make sure that only the key(s) you wanted were added.

然后登录B服务器查看:文章源自靠谱运维-https://www.ixdba.net/archives/1785

[root@xenon1 B]# cd /root/.ssh/
[root@xenon1 B]# ls authorized_keys 

通过这种方式,可以看到,已经成功将公钥发送到了B服务器。authorized_keys就是存放A服务器公钥的文件。文章源自靠谱运维-https://www.ixdba.net/archives/1785

3、测试从A到B服务器的无密码登录

在服务器A执行如下操作,假定B服务器的IP是172.16.213.235:文章源自靠谱运维-https://www.ixdba.net/archives/1785

[root@iZ23sl33esbZ A]# ssh 172.16.213.235 date
2021年 11月 22日 星期一 16:36:23 CST

可以看的,现在从A登录到B服务器,可以无密码登录了。文章源自靠谱运维-https://www.ixdba.net/archives/1785

4、设置从B到A的无密码登录

要实现从B到A的无密码登录,方法就是上面的1-3三个步骤,在B服务器上执行上面三个步骤,即可实现从B到A的无密码登录,这个过程不再介绍。文章源自靠谱运维-https://www.ixdba.net/archives/1785

  • 本文由 发表于 2021年11月22日14:43:06
  • 转载请务必保留本文链接:https://www.ixdba.net/archives/1785
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: