SSH批量部署服务
SSH批量部署服務
1.1在NFS上作為中心分發服務器將私鑰分發到其他服務器上
1.1.1NFS部署
1 [root@nfs-server ~]# useradd zhurui2 [root@nfs-server ~]# echo 123456|passwd --stdin zhurui3 Changing password for user zhurui.4 passwd: all authentication tokens updated successfully.5 創建密碼對:6 [root@nfs-server ~]# su - zhurui ##切換到zhurui用戶下,以后批量分發都在當前用戶下,安全考慮7 [zhurui@nfs-server ~]$ ssh-keygen -t dsa ##ssh-keygen是生成秘鑰的工具,-t參數指建立秘鑰的類型,這里建立dsa類型秘鑰(還有一種類型的秘鑰為RSA,兩者加密算法有區別)8 Generating public/private dsa key pair.9 Enter file in which to save the key (/home/zhurui/.ssh/id_dsa): 10 Created directory '/home/zhurui/.ssh'. 11 Enter passphrase (empty for no passphrase): 12 Enter same passphrase again: 13 Your identification has been saved in /home/zhurui/.ssh/id_dsa. 14 Your public key has been saved in /home/zhurui/.ssh/id_dsa.pub. 15 The key fingerprint is: 16 6f:65:c4:a6:fb:32:45:0c:85:c3:bc:87:8f:a4:ae:bc zhurui@nfs-server 17 The key's randomart image is: 18 +--[ DSA 1024]----+ 19 | o o. | 20 | *. | 21 | *+ | 22 | +++ | 23 | So.=o | 24 | ...+o | 25 | . +. | 26 | . ..o. | 27 | Eo o. | 28 +-----------------+ 29 [zhurui@nfs-server ~]$ 30 [zhurui@nfs-server ~]$ ls -l .ssh/ 31 total 8 32 -rw-------. 1 zhurui zhurui 672 Mar 5 04:23 id_dsa ##私鑰 33 -rw-r--r--. 1 zhurui zhurui 607 Mar 5 04:23 id_dsa.pub ##公鑰 34 將公鑰分發給web-lamp01服務器 35 [zhurui@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub zhurui@192.168.1.12 ##將公鑰分發給1.12服務器 36 The authenticity of host '192.168.1.12 (192.168.1.12)' can't be established. 37 RSA key fingerprint is d6:e6:e6:2a:c7:df:99:51:bb:f4:90:29:16:df:c4:a5. 38 Are you sure you want to continue connecting (yes/no)? yes 39 Warning: Permanently added '192.168.1.12' (RSA) to the list of known hosts. 40 Nasty PTR record "192.168.1.12" is set up for 192.168.1.12, ignoring 41 zhurui@192.168.1.12's password: 42 Permission denied, please try again. 43 zhurui@192.168.1.12's password: 44 Now try logging into the machine, with "ssh 'zhurui@192.168.1.12'", and check in: 45 46 .ssh/authorized_keys 47 48 to make sure we haven't added extra keys that you weren't expecting. 49 50 [zhurui@nfs-server ~]$1.1.2 web-lnmp02客戶端分發部署
-------------------------------------------------------------------- 注:如果你對python感興趣,我這有個學習Python基地,里面有很多學習資料,感興趣的+Q群:895817687 --------------------------------------------------------------------1 [zhurui@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub zhurui@192.168.1.13 ##將公鑰分發到1.13服務器2 The authenticity of host '192.168.1.13 (192.168.1.13)' can't be established.3 RSA key fingerprint is d6:e6:e6:2a:c7:df:99:51:bb:f4:90:29:16:df:c4:a5.4 Are you sure you want to continue connecting (yes/no)? yes5 Warning: Permanently added '192.168.1.13' (RSA) to the list of known hosts.6 Nasty PTR record "192.168.1.13" is set up for 192.168.1.13, ignoring7 zhurui@192.168.1.13's password: 8 Now try logging into the machine, with "ssh 'zhurui@192.168.1.13'", and check in:9 10 .ssh/authorized_keys 11 12 to make sure we haven't added extra keys that you weren't expecting. 13 14 [zhurui@nfs-server ~]$1.1.3 rsync-backup客戶端分發部署
1 [zhurui@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub zhurui@192.168.1.172 The authenticity of host '192.168.1.17 (192.168.1.17)' can't be established.3 RSA key fingerprint is d6:e6:e6:2a:c7:df:99:51:bb:f4:90:29:16:df:c4:a5.4 Are you sure you want to continue connecting (yes/no)? yes5 Warning: Permanently added '192.168.1.17' (RSA) to the list of known hosts.6 zhurui@192.168.1.17's password: 7 Now try logging into the machine, with "ssh 'zhurui@192.168.1.17'", and check in:8 9 .ssh/authorized_keys 10 11 to make sure we haven't added extra keys that you weren't expecting. 12 13 [zhurui@nfs-server ~]$2.1在NFS上測試
2.1.1 通過ssh命令在當前機器上查看web-lamp01的IP地址
1 [zhurui@nfs-server ~]$ ssh -P22 zhurui@192.168.1.12 /sbin/ifconfig eth02 eth0 Link encap:Ethernet HWaddr 00:0C:29:49:CE:B3 3 inet addr:192.168.1.12 Bcast:192.168.1.255 Mask:255.255.255.04 inet6 addr: fe80::20c:29ff:fe49:ceb3/64 Scope:Link5 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:16 RX packets:7701 errors:0 dropped:0 overruns:0 frame:07 TX packets:4795 errors:0 dropped:0 overruns:0 carrier:08 collisions:0 txqueuelen:1000 9 RX bytes:4806676 (4.5 MiB) TX bytes:484902 (473.5 KiB) 10 注:在命令執行過程中,跳過輸入密碼的步驟 11 接著分發文件測試: 12 [zhurui@nfs-server ~]$ cp /etc/hosts 13 hosts hosts.allow hosts.deny 14 [zhurui@nfs-server ~]$ cp /etc/hosts . 15 [zhurui@nfs-server ~]$ ll 16 [zhurui@nfs-server ~]$ scp -P22 hosts zhurui@192.168.1.12:~ ##將當前目錄下hosts文件分發到1.12家目錄下 17 hosts 100% 243 0.2KB/s 00:00 18 [zhurui@nfs-server ~]$ 19 20 檢查1.12上zhurui家目錄下有無hosts文件 21 [root@lamp01 zhurui]# cat /home/zhurui/hosts 22 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 23 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 24 192.168.1.11 nfs-server 25 192.168.1.17 backup 26 192.168.1.12 lamp01 27 192.168.1.13 lnmp02 28 [root@lamp01 zhurui]# 29 通過腳本分發: 30 [zhurui@nfs-server ~]$ sh fenfa.sh 31 hosts 100% 257 0.3KB/s 00:00 32 hosts 100% 257 0.3KB/s 00:00 33 hosts 100% 257 0.3KB/s 00:00 34批量管理腳本:
1 #!/bin/sh2 . /etc/init.d/functions3 if [ $# -ne 1 ]4 then5 echo "USAGE:$0 USAGE|COMMAND"6 exit 17 fi8 for n in 12 13 179 do 10 ssh -p22 zhurui@192.168.1.$n $1 11 done 12 13 ~批量分發腳本:
1 #!/bin/sh2 . /etc/init.d/functions3 4 for n in 12 13 175 do6 scp -P22 $1 zhurui@192.168.1.$n:~ &>/dev/null7 if [ $? -eq 0 ]8 then9 action "fenfa $1 ok" /bin/true 10 else 11 action "fenfa $1 ok" /bin/false 12 fi 13 done 14 15 ~利用分發腳本分發hosts文件
1 [zhurui@nfs-server ~]$ sh fenfa.sh hosts 2 fenfa hosts ok [ OK ] 3 fenfa hosts ok [ OK ] 4 fenfa hosts ok [ OK ] 5 [zhurui@nfs-server ~]$優化后的分發腳本
1 #!/bin/sh2 . /etc/init.d/functions3 if [ $# -ne 1 ]4 then5 echo "USAGE:$0 {FILENAME|DIRNAME}"6 exit 17 fi8 for n in 12 13 179 do 10 scp -P22 -r $1 zhurui@192.168.1.$n:~ &>/dev/null 11 if [ $? -eq 0 ] 12 then 13 action "fenfa $1 ok" /bin/true 14 else 15 action "fenfa $1 ok" /bin/false 16 fi 17 done總結
- 上一篇: inotify+rsync实现实时同步部
- 下一篇: Kafka系列2-producer和co