实验环境
[alvin@Client-1 ~]$ cat /etc/redhat-release
CentOS release 6.5 (Final)
[alvin@Client-1 ~]$ uname -r
2.6.32-431.el6.i686
三台服务器分别是:
alvin@Client-1 192.168.8.6 (中心服务器,向2,3 分发数据)
alvin@Client-2 192.168.8.7
alvin@Client-3 192.168.8.8
开始前关闭三台服务器的防火墙
实验步骤:
1.
三台机器上都新建同一个用户 alvin, 以下以第3台为例:
[root@Client-3 ~]# useradd alvin
给三台机器都设置密码
echo 123456 |passwd --stdin alvin
2.
三台机器都切换到alvin下
3.
在中心服务器上创建秘钥操作
[alvin@Client-1 ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/alvin/.ssh/id_dsa):
Created directory '/home/alvin/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/alvin/.ssh/id_dsa.
Your public key has been saved in /home/alvin/.ssh/id_dsa.pub.
The key fingerprint is:
56:93:fd:aa:1d:a9:1a:d0:39:57:2b:78:5e:04:2e:b3 alvin@Client-1
The key's randomart p_w_picpath is:
+--[ DSA 1024]----+
| . |
| . + |
| o = + |
| . B + o |
| . E + o . |
| o = o o |
| . . + |
| . + . |
| ..o . |
+-----------------+
[alvin@Client-1 ~]$ ls -l .ssh/
总用量 8
-rw------- 1 alvin alvin 668 5月 2 18:19 id_dsa #私钥
-rw-r--r-- 1 alvin alvin 604 5月 2 18:19 id_dsa.pub #公钥
[alvin@Client-1 ~]$ ls -ld .ssh/
drwx------ 2 alvin alvin 4096 5月 2 18:19 .ssh/ #查看目录权限
4.
把公钥发给其他的2台服务器
[alvin@Client-1 ~]$ ssh-copy-id -i .ssh/id_dsa.pub alvin@192.168.8.7
如果客户端不是22端口 (比如ssh 远程连接端口被修改为52113) 则写成下面方式
[alvin@Client-1 ~]$ ssh-copy-id -i .ssh/id_dsa.pub "-p 52113 alvin@192.168.8.7"
alvin@192.168.8.7's password:
Now try logging into the machine, with "ssh '-p 52113 alvin@192.168.8.7'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[alvin@Client-1 ~]$
另外一台操作同上,注意修改一下IP
5.
查看接受的客户端公钥情况,发现名称改变了。
[alvin@Client-2 ~]$ ll .ssh/
总用量 4
-rw-------. 1 alvin alvin 604 7月 2 21:18 authorized_keys
注意:名称改变是因为 /etc/ssh/sshd_config 里面定义的。
#AuthorizedKeysFile .ssh/authorized_keys
6.
测试发现,连接到Client-3上不要密码就可以直接连
[alvin@Client-1 ~]$ ssh -p52113 alvin@192.168.8.8
[alvin@Client-3 ~]$
7.
测试文件的分发功能
[alvin@Client-1 ~]$ echo 123 >a.txt
[alvin@Client-1 ~]$ cat a.txt
123
[alvin@Client-1 ~]$ scp -P 52113 a.txt alvin@192.168.8.7:~
a.txt 100% 4 0.0KB/s 00:00
[alvin@Client-1 ~]$ scp -P 52113 a.txt alvin@192.168.8.8:~
a.txt 100% 4 0.0KB/s 00:00
8.
验证
[alvin@Client-2 ~]$ ll
总用量 4
-rw-rw-r--. 1 alvin alvin 4 7月 2 21:40 a.txt
[alvin@Client-3 ~]$ ll
总用量 4
-rw-rw-r-- 1 alvin alvin 4 7月 2 05:27 a.txt
[alvin@Client-3 ~]$ cat a.txt
123