多台Linux服务器SSH相互访问无需密码

简介:

一、环境配置

1、系统:CentOS release 5.6   IP:192.168.4.200   主机名:JW01

2、系统:CentOS release 5.9   IP:192.168.4.244   主机名:JW02

3、系统:CentOS release 5.3   IP:192.168.4.232   主机名:KT01

4、系统:Red Hat  release 4   IP:192.168.4.201   主机名:KT02

二、服务器配置

1、在每台服务器上都执行ssh-keygen -t rsa 生成密钥

[root@jw01 .ssh]# ssh-keygen -t rsa
[root@jw02 .ssh]# ssh-keygen -r rsa
[root@kt01  .ssh]# ssh-keygen -t rsa
[root@kt02  .ssh]# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #回车代表无需密码登陆
Enter passphrase (empty for no passphrase): #回车
Enter same passphrase again: #回车
Your identification has been saved in /root/.ssh/id_rsa. #代表私钥
Your public key has been saved in /root/.ssh/id_rsa.pub. #代表公钥
The key fingerprint is:
04:45:0b:47:10:92:0c:b2:b9:d7:11:5b:49:05:e4:d9 
root@jw01

2、在每台服务器上将公钥复制到无需登录的服务器上,如192.168.4.200 /244/232/2014台做相互无需密码登录,在每台服务器生成密钥后,在每一台服务器上执行ssh-copy-id的命令;以192.168.4.200为例;其他三台服务器也是同样的操作;

例:192.168.4.200
[root@jw01 ~]# cd ~/.ssh
[root@jw01 .ssh]# ssh-copy-id -i  id_rsa.pub "-p 22 
root@192.168.4.244"
[root@jw01 .ssh]# ssh-copy-id -i  id_rsa.pub "-p 22 root@192.168.4.232"
[root@jw01 .ssh]# ssh-copy-id -i  id_rsa.pub "-p 22 root@192.168.4.201"

root@192.168.4.244's password:              #输入密码
Now try logging into the machine, with "ssh '-p 22 
root@192.168.4.244'", and check in:
 .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
以上信息出现就代表复制成功!

以上,可以自动将公钥添加到authorized_keys的文件中,在每台服务器都执行完以上的步骤就可以实现多台服务器无需密码相互访问了。

三、故障处理

1、如果ssh-copy-id 函数在远程服务器不存在;如下

[root@kt02 .ssh]# ssh-copy-id -i  id_rsa.pub "-p 22
root@192.168.4.244"
-bash: ssh-copy-id: command not found

     可以尝试用一下命令解决,直接复制本地的pubkey内容到远程服务器;

[root@kt02 .ssh]# cat ~/.ssh/id_*.pub | ssh  root@192.168.4.244 'cat > .ssh/authorized_keys'

四、登录SSH

1、登录远程服务器就不需要密码:

[root@kt02 .ssh]# ssh root@192.168.4.244

2、登录远程服务器有端口的需要添加端口-P

[root@jw02 .ssh]# ssh ken@223.73.125.198 -p 511190

3、你依然被要求输入远程服务器密码, 那说明你的用户在远程服务器上面没有足够的权限;请查看一下你的远程登录的用户以及authorized_keys文件在哪个用户的目录下。



本文转自 kuangling 51CTO博客,原文链接:http://blog.51cto.com/kling/1132800

相关文章
|
6天前
|
消息中间件 安全 Unix
SSH配置多台服务器之间的免密登陆以及登陆别名
SSH配置多台服务器之间的免密登陆以及登陆别名
18 1
|
16天前
|
Linux
关于Linux目录访问函数总结
关于Linux目录访问函数总结
11 1
|
1月前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
161 0
|
27天前
|
安全 Shell Linux
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
【Shell 命令集合 文件管理】Linux ssh 远程主机之间复制文件 scp 命令使用教程
34 0
|
8天前
|
Linux 网络安全
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
linux免密登录报错 Bad owner or permissions on /etc/ssh/ssh_config.d/05-redhat.conf
17 1
|
1月前
|
弹性计算 缓存 测试技术
阿里云ECS云服务器2核4G能支持多少人同时访问?2核4G5M并发量评测
阿里云ECS云服务器2核4G能支持多少人同时访问?2核4G5M并发量评测,2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
1月前
|
弹性计算 缓存 数据库
2核4g服务器能支持多少人访问?
2核4g服务器能支持多少人访问?阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
27天前
|
JavaScript 前端开发 应用服务中间件
通过域名的方式访问服务器里的资源
通过域名的方式访问服务器里的资源
20 0
|
1月前
|
存储 网络协议 安全
如何搭建外网可访问的Serv-U FTP服务器,轻松远程共享文件!
如何搭建外网可访问的Serv-U FTP服务器,轻松远程共享文件!
|
1月前
|
安全 Shell 网络安全
ssh配置无密码验证
ssh配置无密码验证要在SSH中配置无密码验证,您需要使用公钥验证【2月更文挑战第18天】
36 1