IT技术博客大学习 共学习 共进步

linux 建立两台机器的信任关系

Zhang Jiuan' Notes 2009-12-07 16:11:39 浏览 11,344 次

在写脚本中,肯定会遇到跨机器访问和scp的操作,每次都输入用户名和密码当然不方便,也不是写脚本的根本目的。如何在两台机器之间建立信任关系,那样每次访问都不用输入用户名和密码,这样脚本自动化起来就做到无人值守了。 那么下来就来讲下如何在两台机器之间建立信任关系。

机器1im1.com

机器2im2.com

假如把机器2作为主机器

1、登陆机器2

根目录为:/home/wanping

2、进入.ssh目录(如果没有,手动创建mkdir .ssh)执行命令:ssh-keygen -trsa

 [wanping@im2 .ssh]$ ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/home/wanping/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/wanping/.ssh/id_rsa.

Your public key has been saved in /home/wanping/.ssh/id_rsa.pub.

The key fingerprint is:

00:b7:30:96:2d:09:25:cb:64:fe:88:5d:32:57:72:65 wanping@im2.com

生成两个文件id_rsa  id_rsa.pub

其中id_rsa是密钥文件,id_rsa.pub是公钥文件。

3、执行拷贝,把id_rsa.pub文件拷贝到机器1

scp id_rsa.pub im1.com:/home/wanping/.ssh/ scp id_rsa.pub 192.168.1.201:/home/user2/.ssh/10.62.244.126

为什么要把机器2ip作为文件名呢,这是为了避免以后和更多的机器建立信任关系不发生混淆。

4、登陆机器1,进入.ssh目录下(如果没有,手动建立)

5cat 10.62.244.126>authorized_keys

完成上面的步骤后,从imtest2登陆imtest1就无需输入用户名和密码,直接就可以ssh过去,scp也无需密码。但是ssh和拷贝的时候,需要把用户名写上,具体格式如下:ssh wanping@im1.com

scp a.file wanping@im1.com:/home/wanping/a.file

建议继续学习

  1. 加速scp传输速度 (阅读 5,281)
  2. 使用scp在windows和Linux之间互传文件 (阅读 5,222)
  3. 使用scp命令在两台linux上对拷文件或者文件夹 (阅读 3,762)
  4. 如何在 Linux 上复制文件/文件夹到远程系统? (阅读 2,422)
  5. 产品的信任感 (阅读 1,820)