|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!实行背景
在一个局域网,在个中指定一台主机做为办理机,别的主机做为被办理机,为今后保护的便当性,请求完成办理机无需暗码,间接登录被办理机.
利用VmwareWorkstation搭建一个摹拟局域网
该局域网内有四台主机,经由过程假造互换机Vnet1,完成互连互通
四台主机都装置了CentOS6,并封闭了iptables和SELinux
指定CentOS1(192.168.10.2)为办理机
指定CentOS2(192.168.10.3~5)为被办理机
复杂的收集拓朴以下图所示
实行目标
经由过程shell剧本,完成一次实行,批量设置办理机与被办理机的信托干系,完成办理机免暗码登录被办理机
剧本构成
为了便于剧本保护与扩大,经由过程以下一组剧本来完成
1
2
3
4
5
6
7
#ll
total20
-rw-r--r--1rootroot657Nov822:49ClientAuthorize.sh
-rw-r--r--1rootroot338Nov822:24distribute.sh
-rw-r--r--1rootroot279Nov822:34excuse.sh
-rw-r--r--1rootroot39Nov822:15hostip.out
-rw-r--r--1rootroot210Nov820:30ServerAuthorize.sh.example
上面分离先容一下各个剧本
ClientAuthorize.sh设置主剧本,在办理机上实行,经由过程它挪用别的几个子剧本,完成批量设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#vimClientAuthorize.sh
#!/bin/bash
#声明情况变量
exportPATH="/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
exportLANG="en_US.UTF-8"
#天生办理机的公私钥
ssh-keygen
#设置办理机的相干目次权限
chmodgo-w/root
chmod700/root/.ssh
chmod600/root/.ssh/*
#将天生的公钥信息导进ServerAuthorize.sh剧本中
rsapub_var=$(cat/root/.ssh/id_rsa.pub)
cp/tmp/authorize/ServerAuthorize.sh.example/tmp/authorize/ServerAuthorize.sh
echo"">>/tmp/authorize/ServerAuthorize.sh
echo"#setauthorized_keys">>/tmp/authorize/ServerAuthorize.sh
echo"echo""${rsapub_var}"">>/root/.ssh/authorized_keys">>/tmp/authorize/ServerAuthorize.sh
chmodu+x/tmp/authorize/ServerAuthorize.sh
#挪用批量分发剧本,假如实行乐成,就持续挪用批量实行剧本
sh/tmp/authorize/distribute.sh&&
sh/tmp/authorize/excuse.sh
ServerAuthorize.sh.example一个模板文件,经ClientAuthorize.sh处置后,天生在被办理机上实行的剧本,用来设置两边的信托干系,上面显现的是一个已设置完成的ServerAuthorize剧本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#vimServerAuthorize.sh
#!/bin/bash
#声明情况变量
exportPATH="/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
exportLANG="en_US.UTF-8"
#反省所需目次及文件,假如没有就创立一个
if[!-d/root/.ssh];then
mkdir/root/.ssh
fi
if[!-f/root/.ssh/authorized_keys];then
touch/root/.ssh/authorizedzz_keys
fi
#设置被办理机的相干目次文件权限
chmodgo-w/root
chmod700/root/.ssh
chmod600/root/.ssh/*
#设置信托干系
#setauthorized_keys
echo"ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEA6YUgG2kpxJDfqeoSIEOzQk/2tj1xTpMtb6e618rm6XYnjjdP5/FdwMKnBXRc6a/fp3h2AupsM7Pzc1AxzTZWNUUxEJoI0mZxxoy0B5UITTA8bAwiBfhIsTkcHqSS3CADdaAlFYol+9JO3sZ6U8dlD1KQtZLpc9FMPX87kowEJbtuq+XNZ7xe59KV0Adt3YI+ICqVU8WHu9yO7XkP313FZFPIYISqmY9kmhKUHT8znIHDqYQVC9MOMsNxQ4HlPLHNESnBvbSlR0wdz0q1VjVqF2qxyRZAQiIWi3nkYk6oKK61UYHQ62ueLpPQ4yWZfcKLaYJZQFeVo/uQdauYYVEQww==root@CentOS1">>/root/.ssh/authorized_keys
distribute.sh分发剧本,将ServerAuthorize.sh从办理机分发到各被办理机
1
2
3
4
5
6
7
8
9
10
11
12
13
#vimdistribute.sh
#!/bin/bash
#声明情况变量
exportPATH="/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
exportLANG="en_US.UTF-8"
#指定近程分发的来历与方针
from_var="/tmp/authorize/ServerAuthorize.sh"
to_var="/tmp"
#经由过程for轮回将剧本分发到各个被办理机
forhost_ipin$(cat/tmp/authorize/hostip.out)
do
scp-oStrictHostKeyChecking=no-rp"${from_var}""${host_ip}":"${to_var}"
done
excuse.sh批量实行剧本,在办理机上实行,使被办理机批量实行ServerAuthorize.sh
1
2
3
4
5
6
7
8
9
10
11
#vimexcuse.sh
#!/bin/bash
#声明情况变量
exportPATH="/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
exportLANG="en_US.UTF-8"
#经由过程for轮回,批量实行被办理机上的设置剧本
command_var="sh/tmp/ServerAuthorize.sh"
forhost_ipin$(cat/tmp/authorize/hostip.out)
do
ssh-f${host_ip}"${command_var}"
done
hostip.out供应被办理机的ip列表
1
2
3
4
#vimhostip.out
192.168.10.3
192.168.10.4
192.168.10.5
剧本实行
上面我在办理机下面实行ClientAuthorize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@CentOS1authorize]#shClientAuthorize.sh
Generatingpublic/privatersakeypair.
#确认重生成的公钥保留地位,默许在以后用户家目次的.ssh目次下,此处间接回车便可
Enterfileinwhichtosavethekey(/root/.ssh/id_rsa):
Createddirectory/root/.ssh.
#输出暗码,间接回车,使暗码为空
Enterpassphrase(emptyfornopassphrase):
Entersamepassphraseagain:
Youridentificationhasbeensavedin/root/.ssh/id_rsa.
Yourpublickeyhasbeensavedin/root/.ssh/id_rsa.pub.
Thekeyfingerprintis:
6d:bc:5c:f8:32:bf:ee:4a:fe:bf:be:76:8d:29:38:aaroot@CentOS1
Thekeysrandomartimageis:
||
||
||
|o.|
|S=.|
|o+|
|*..o.|
|oo+.+o|
|E...o***=+|
#入手下手实行分发义务,由于在剧本中增加了StrictHostKeyChecking=no参数,以是会呈现上面的warning
Warning:Permanentlyadded192.168.10.3(RSA)tothelistofknownhosts.
#由于信托干系还没有创建,以是仍是必要暗码
root@192.168.10.3spassword:
ServerAuthorize.sh100%6640.7KB/s00:00
Warning:Permanentlyadded192.168.10.4(RSA)tothelistofknownhosts.
root@192.168.10.4spassword:
ServerAuthorize.sh100%6640.7KB/s00:00
Warning:Permanentlyadded192.168.10.5(RSA)tothelistofknownhosts.
root@192.168.10.5spassword:
ServerAuthorize.sh100%6640.7KB/s00:00
#分发完成后,入手下手在各个被办理机上实行设置剧本
root@192.168.10.3spassword:
root@192.168.10.4spassword:
root@192.168.10.5spassword:
实行了局查验
我从办理机,分离登录三台被办理机,能够看到,都已不必要输出暗码了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[root@CentOS1authorize]#ssh192.168.10.3
Lastlogin:ThuNov712:25:142013from192.168.10.1
[root@CentOS2~]#ifconfigeth0
eth0Linkencap:EthernetHWaddr00:0C:29:CA:DC:71
inetaddr:192.168.10.3Bcast:192.168.10.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:feca:dc71/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:150errors:0dropped:0overruns:0frame:0
TXpackets:103errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:25556(24.9KiB)TXbytes:16569(16.1KiB)
Interrupt:19Baseaddress:0x2000
[root@CentOS2~]#exit
logout
Connectionto192.168.10.3closed.
[root@CentOS1authorize]#ssh192.168.10.4
Lastlogin:ThuNov714:13:232013from192.168.10.1
[root@CentOS3~]#ifconfigeth0
eth0Linkencap:EthernetHWaddr00:0C:29:28:1C:51
inetaddr:192.168.10.4Bcast:192.168.10.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fe28:1c51/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:149errors:0dropped:0overruns:0frame:0
TXpackets:112errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:23653(23.0KiB)TXbytes:17595(17.1KiB)
Interrupt:19Baseaddress:0x2000
[root@CentOS3~]#exit
logout
Connectionto192.168.10.4closed.
[root@CentOS1authorize]#ssh192.168.10.5
Lastlogin:ThuNov715:16:272013from192.168.10.1
[root@CentOS4~]#ifconfigeth0
eth0Linkencap:EthernetHWaddr00:0C:29:51:D4:1F
inetaddr:192.168.10.5Bcast:192.168.10.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fe51:d41f/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:124errors:0dropped:0overruns:0frame:0
TXpackets:108errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:19536(19.0KiB)TXbytes:17163(16.7KiB)
Interrupt:19Baseaddress:0x2000
[root@CentOS4~]#exit
logout
Connectionto192.168.10.5closed.
[root@CentOS1authorize]#
本文出自“月白白”博客,请务必保存此出处http://yuebaibai222.blog.51cto.com/2535988/1322683
欢迎大家来到仓酷云论坛! |
|