Web集群之SSH批量管理

、什么是SSH批量管理

在管理机产生公钥和私钥,然后把自己的公钥推送给需要被管理的服务器,然后就可以通过scp和ssh命令,无需输入密码即可管理

锁=公钥,钥匙=私钥

企业里实现ssh方案:

1)直接root ssh key。

条件:系统允许root使用ssh

2)sudo提权来实现没有权限用户拷贝

实验环境:

hostnameip描述

m01172.16.1.61管理机

web01172.16.1.7被管理

nfs172.16.1.31被管理

backup172.16.1.41被管理

所有机器系统环境统一

[root@m01 /]# cat /etc/redhat-release CentOS Linux release7.5.1804(Core) [root@m01 /]# uname -r3.10.0-862.el7.x86_64

1.1 所有的服务器创建普通用户及密码

useradd xiaoliecho"123456"|passwd --stdin xiaoliid xiaolisu - xiaoli#<==统一切换到xiaoli用户

1.2 m01产生密钥

#使用xiaoli用户来创建私钥,并且分发公钥

[xiaoli@m01 ~]$ ssh-keygen -t dsa#<==生成私钥(一路回车)Generatingpublic/privatedsa key pair.Enter fileinwhich to save thekey(/home/xiaoli/.ssh/id_dsa): Created directory '/home/xiaoli/.ssh'. #<==私钥存放的目录Enterpassphrase(emptyforno passphrase): Enter same passphrase again: Your identification has been savedin/home/xiaoli/.ssh/id_dsa.Yourpublickey has been savedin/home/xiaoli/.ssh/id_dsa.pub.The key fingerprintis:SHA256:/UtUhhM++KSQH9OgJyP+MCRz+LhdYfRt/r6384aVLzU xiaoli@m01The key's randomart imageis:+---[DSA 1024]----+|        . . .    ||    . . + * o  ||    + + O * X o  ||    O o O O=  ||    . = S + +  .||    o =  o . Eo||    . . .  o .+o||          . oo.+||            . o*=|+----[SHA256]-----+[xiaoli@m01 ~]$ pwd/home/xiaoli[xiaoli@m01 ~]$ ls .ssh/id_dsa  id_dsa.pub[xiaoli@m01 ~]$ ll .ssh/total8-rw-------1xiaoli xiaoli672Nov520:57id_dsa#<==私钥-rw-r--r--1xiaoli xiaoli600Nov520:57id_dsa.pub#<==公钥

1.3 管理机分发公钥给客户端

管理机推送公钥给backup

[xiaoli@m01 ~]$ ssh-copy-id -i .ssh/id_dsa.pub xiaoli@172.16.1.41/bin/ssh-copy-id: INFO: Sourceofkey(s) to be installed:".ssh/id_dsa.pub"The authenticityofhost'172.16.1.41 (172.16.1.41)'can't be established.

ECDSA key fingerprint is SHA256:9mwPu7qxdn4iuw1GFz5nXmBdpXKRoj0D8dhDo6sp9XQ.

ECDSA key fingerprint is MD5:d2:35:47:86:60:b5:97:16:3f:26:4c:91:78:3a:02:2a.

Are you sure you want to continue connecting (yes/no)? yes

/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

xiaoli@172.16.1.41's password: Numberofkey(s) added:1Nowtrylogging into the machine, with:"ssh 'xiaoli@172.16.1.41'"andcheck to make sure that only the key(s) you wanted were added.#backup上查看是否收到公钥[xiaoli@backup ~]$ ls .ssh/authorized_keys .ssh/authorized_key#配置文件默认就是.ssh/authorized_key这个文件名,是由/etc/ssh/sshd_config这个配置文件所定义[root@backup backup]$ grep authorized_keys /etc/ssh/sshd_config |egrep -v"^#"AuthorizedKeysFile      .ssh/authorized_keys

管理机推送公钥给nfs

[xiaoli@m01 ~]$ ssh-copy-id -i .ssh/id_dsa.pub xiaoli@172.16.1.31/bin/ssh-copy-id: INFO: Sourceofkey(s) to be installed:".ssh/id_dsa.pub"The authenticityofhost'172.16.1.31 (172.16.1.31)'can't be established.

ECDSA key fingerprint is SHA256:9mwPu7qxdn4iuw1GFz5nXmBdpXKRoj0D8dhDo6sp9XQ.

ECDSA key fingerprint is MD5:d2:35:47:86:60:b5:97:16:3f:26:4c:91:78:3a:02:2a.

Are you sure you want to continue connecting (yes/no)? yes

/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

xiaoli@172.16.1.31's password: Numberofkey(s) added:1Nowtrylogging into the machine, with:"ssh 'xiaoli@172.16.1.31'"andcheck to make sure that only the key(s) you wanted were added.#nfs上查看是否收到公钥[xiaoli@nfs ~]$ ls -l .ssh/total4-rw-------1xiaoli xiaoli600Nov521:16authorized_keys

管理机推送公钥给web01

[xiaoli@m01 ~]$ ssh-copy-id -i .ssh/id_dsa.pub xiaoli@172.16.1.7/bin/ssh-copy-id: INFO: Sourceofkey(s) to be installed:".ssh/id_dsa.pub"The authenticityofhost'172.16.1.7 (172.16.1.7)'can't be established.

ECDSA key fingerprint is SHA256:9mwPu7qxdn4iuw1GFz5nXmBdpXKRoj0D8dhDo6sp9XQ.

ECDSA key fingerprint is MD5:d2:35:47:86:60:b5:97:16:3f:26:4c:91:78:3a:02:2a.

Are you sure you want to continue connecting (yes/no)? yes

/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

xiaoli@172.16.1.7's password: Numberofkey(s) added:1Nowtrylogging into the machine, with:"ssh 'xiaoli@172.16.1.7'"andcheck to make sure that only the key(s) you wanted were added.#web01查看是否收到公钥[xiaoli@web01 ~]$ ls -l .ssh/total4-rw-------1xiaoli xiaoli600Nov521:20authorized_keys

1.4 管理机实现批量获取参数

单独查看某一台客户端IP地址,如果端口号为22,就不需要加-p

[xiaoli@m01 ~]$ ssh xiaoli@172.16.1.31/sbin/ifconfig ens33ens33:flags=4163  mtu1500inet10.0.0.31netmask255.255.255.0broadcast10.0.0.255inet6 fe80::7ef6:6b6b:fba4:c66c  prefixlen64scopeid0x20        inet6 fe80::f15a:916:1ee7:65e9  prefixlen64scopeid0x20        ether00:50:56:20:de:ectxqueuelen1000(Ethernet)        RX packets68059bytes50182137(47.8MiB)        RX errors0dropped0overruns0frame0TX packets32722bytes6712416(6.4MiB)        TX errors0dropped0overruns0carrier0collisions0#我们可以发现这时执行ssh就不需要输入密码

创建脚本查看三台客户端的IP地址

[xiaoli@m01 ~]$ mkdir seripts[xiaoli@m01 ~]$ cd seripts[xiaoli@m01 seripts]$ cat view_ip.sh#!/bin/shUser=xiaoliIp=(172.16.1.7

172.16.1.31

172.16.1.41

)

for ((i=0;i<${#Ip[*]};i++))

do

        ssh ${User}@${Ip[$i]} /sbin/ifconfig ens33

done

#执行脚本 货运APP找上海捌跃网络科技有限公司QQ3343874032

[xiaoli@m01 seripts]$ sh view_ip.sh

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu1500inet10.0.0.7netmask255.255.255.0broadcast10.0.0.255inet6 fe80::7ef6:6b6b:fba4:c66c  prefixlen64scopeid0x20        inet6 fe80::b85a:6444:fdc7:90ef  prefixlen64scopeid0x20        inet6 fe80::f15a:916:1ee7:65e9  prefixlen64scopeid0x20        ether00:50:56:32:88:betxqueuelen1000(Ethernet)        RX packets11633bytes2805754(2.6MiB)        RX errors0dropped0overruns0frame0TX packets6003bytes1047269(1022.7KiB)        TX errors0dropped0overruns0carrier0collisions0ens33:flags=4163  mtu1500inet10.0.0.31netmask255.255.255.0broadcast10.0.0.255inet6 fe80::7ef6:6b6b:fba4:c66c  prefixlen64scopeid0x20        inet6 fe80::f15a:916:1ee7:65e9  prefixlen64scopeid0x20        ether00:50:56:20:de:ectxqueuelen1000(Ethernet)        RX packets68065bytes50182545(47.8MiB)        RX errors0dropped0overruns0frame0TX packets32726bytes6712704(6.4MiB)        TX errors0dropped0overruns0carrier0collisions0ens33:flags=4163  mtu1500inet10.0.0.41netmask255.255.255.0broadcast10.0.0.255inet6 fe80::7ef6:6b6b:fba4:c66c  prefixlen64scopeid0x20        inet6 fe80::b85a:6444:fdc7:90ef  prefixlen64scopeid0x20        inet6 fe80::f15a:916:1ee7:65e9  prefixlen64scopeid0x20        ether00:50:56:21:a4:2a  txqueuelen1000(Ethernet)        RX packets123357bytes15582283(14.8MiB)        RX errors0dropped0overruns0frame0TX packets130534bytes11862139(11.3MiB)        TX errors0dropped0overruns0carrier0collisions0上面结果为成功标志!连接所有机器,不提示密码直接可以操作

1.5 scp实现批量下发文件

每台服务器root权限下实施sudo#切换到root用户,给xiaoli用户赋予有rsync的命令执行权限echo"xiaoli ALL=(ALL) NOPASSWD:/usr/bin/rsync ">>/etc/sudoersvisudo -c

将/etc/hosts文件拷贝到家目录(xiaoli),并修改hosts文件内容

[xiaoli@m01 ~]$ cp /etc/hosts .[xiaoli@m01 ~]$ tail-5hosts172.16.1.7web01172.16.1.41backup172.16.1.31nfs172.16.1.51m01################2018-11-5################

使用脚本批量分发hosts文件

[xiaoli@m01 ~]$  cat seripts/fenfa_file.sh#!/bin/shUser=xiaoliIp=(172.16.1.7172.16.1.31172.16.1.41)for((i=0;i<${#Ip[*]};i++))doscp ~/hosts${User}@${Ip[$i]}:~ ssh -t${User}@${Ip[$i]}sudo rsync ~/hosts /etc/hostsdone#运行批量分发脚本[xiaoli@m01 seripts]$ sh  fenfa_file.shhosts                                                                    100%  268  245.5KB/s  00:00    Connection to 172.16.1.7 closed.hosts                                                                    100%  268    47.6KB/s  00:00    Connection to 172.16.1.31 closed.hosts                                                                    100%  268  295.1KB/s  00:00    Connection to 172.16.1.41 closed.

客户端查看结果

#以backup客户端为例展示结果:[xiaoli@backup ~]$ tail-5/etc/hosts172.16.1.7web01172.16.1.41backup172.16.1.31nfs172.16.1.51m01################2018-11-5################

扩展:使用rsync通道模式,实现增量、加密

[xiaoli@m01 ~]$ rsync -avz hosts -e'ssh -p 22'xiaoli@172.16.1.41sending incremental file listhostssent214bytes  received35bytes498.00bytes/sectotal sizeis268speedupis1.08


转自:http://blog.51cto.com/12643266/2314340

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • 加密算法 对称加密算法 加密和解密使用同一个密钥 DES、3DES、AES、Blowfish、Twofish、ID...
    毛利卷卷发阅读 1,533评论 0 4
  • SSH全称Secure SHell,顾名思义就是非常安全的shell的意思,SSH协议是IETF(Internet...
    StarShift阅读 2,482评论 0 7
  • 被炙热的火舌吞噬 被炸裂的痛苦掩埋 留下一小片 温柔笑着的誓言 多少次午夜梦回不可抑制地想起他眉眼的温柔 半哭半笑...
    之幕阅读 202评论 0 0
  • 因为生活压力太大,年轻人整天唉声叹气。某天,他去山中寻找一位大哲人,希望对方给他一个解脱之法。 哲人听完他的诉说后...
    应果阅读 665评论 0 0
  • 从上周开始潍坊的天气就一直被乌云满布的天空所笼罩;直到昨晚的一场狂风暴雨算是这些天的阴沉、乌蒙、和每天不定时的大雨...
    Sunny仔仔阅读 643评论 1 2