centos passwordless remote copy scp

foreword

为什么有这个需要,由于服务器是负载均衡的,具有多个节点,由于现在是自动部署的,总不能在每个节点上都部署一次吧?那如果在一台服务器上部署了,

直接远程复制到其余的服务器就比较方便了,但是由于scp是要输入密码的,使用脚本的话比较麻烦,所以就必须在两个机器之间建立相互信任的连接。就有

了这篇文章

Prepare

虚拟机:192.168.0.201  作为A

      192.168.0.204  作为B
	  
系  统:centos 6.5 64 位

成  果:A无需密码SCP文件到B

Start

1、用ssh登录A,输入 ssh-keygen -b 4096 -t rsa  ,一直按回车就行。

   显示大概如下:
   
	Generating public/private rsa key pair.
	Enter file in which to save the key (/root/.ssh/id_rsa): 
	Created directory '/root/.ssh'.
	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:
	e5:81:a9:33:14:87:28:ae:37:4b:17:f3:e4:4a:4d:e1 root@waiyin1
	The key's randomart image is:
	+--[ RSA 2048]----+
	|     ....        |
	|  . . oo o       |
	| . . ...o o      |
	|  . o.E. o .     |
	| .   O+ S .      |
	|. + o +o         |
	| o = .           |
	|  . .            |
	|                 |
	+-----------------+

	执行完毕后,在目录 /root/.ssh/ 下会产生一对秘钥,其中`id_rsa.pub`是公钥,`id_rsa`是私钥;
	
	
2、把id_rsa.pub 复制到B服务器的./ssh目录下,并改名字为authorized_keys, 输入 :

  scp -p /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
  
  出现:
  
  [email protected]'s password:        ---输入B服务器的密码
  scp: /root/.ssh/authorized_keys: No such file or directory,提示没有这个目录
  
  SSH进入**B服务器**运行:ssh-keygen -b 4096 -t rsa,然后再运行:scp -p /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
  
  [email protected]'s password:  ---输入B服务器的密码
  id_rsa.pub  
  
  说明已经建立了联系,此时在A服务器 可以试用scp 不输入密码复制文件去B服务器!

Summarize

总结一下,如果想要A服务器 无密码远程服务制到服务器B 则需要把A生成的公钥放到B服务器,想要scp到C服务器也一样。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259926&siteId=291194637