Web集群之SSH批量管理

降瞳、什么是SSH批量管理

在管理機產(chǎn)生公鑰和私鑰茵瀑,然后把自己的公鑰推送給需要被管理的服務器撮躁,然后就可以通過scp和ssh命令金踪,無需輸入密碼即可管理

鎖=公鑰,鑰匙=私鑰

企業(yè)里實現(xiàn)ssh方案:

1)直接root ssh key季春。

條件:系統(tǒng)允許root使用ssh

2)sudo提權來實現(xiàn)沒有權限用戶拷貝

實驗環(huán)境:

hostnameip描述

m01172.16.1.61管理機

web01172.16.1.7被管理

nfs172.16.1.31被管理

backup172.16.1.41被管理

所有機器系統(tǒng)環(huán)境統(tǒng)一

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

1.1 所有的服務器創(chuàng)建普通用戶及密碼

useradd xiaoliecho"123456"|passwd --stdin xiaoliid xiaolisu - xiaoli#<==統(tǒng)一切換到xiaoli用戶

1.2 m01產(chǎn)生密鑰

#使用xiaoli用戶來創(chuàng)建私鑰洗搂,并且分發(fā)公鑰

[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 管理機分發(fā)公鑰給客戶端

管理機推送公鑰給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 管理機實現(xiàn)批量獲取參數(shù)

單獨查看某一臺客戶端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#我們可以發(fā)現(xiàn)這時執(zhí)行ssh就不需要輸入密碼

創(chuàng)建腳本查看三臺客戶端的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

#執(zhí)行腳本?貨運APP找上海捌躍網(wǎng)絡科技有限公司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實現(xiàn)批量下發(fā)文件

每臺服務器root權限下實施sudo#切換到root用戶,給xiaoli用戶賦予有rsync的命令執(zhí)行權限echo"xiaoli ALL=(ALL) NOPASSWD:/usr/bin/rsync ">>/etc/sudoersvisudo -c

將/etc/hosts文件拷貝到家目錄(xiaoli)倡勇,并修改hosts文件內(nèi)容

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

使用腳本批量分發(fā)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#運行批量分發(fā)腳本[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通道模式逞刷,實現(xiàn)增量、加密

[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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末妻熊,一起剝皮案震驚了整個濱河市夸浅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌扔役,老刑警劉巖帆喇,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異亿胸,居然都是意外死亡坯钦,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門侈玄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來婉刀,“玉大人,你說我怎么就攤上這事序仙⊥患眨” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長律秃。 經(jīng)常有香客問我呈昔,道長,這世上最難降的妖魔是什么友绝? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任堤尾,我火速辦了婚禮,結果婚禮上迁客,老公的妹妹穿的比我還像新娘郭宝。我一直安慰自己,他們只是感情好掷漱,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布粘室。 她就那樣靜靜地躺著,像睡著了一般卜范。 火紅的嫁衣襯著肌膚如雪衔统。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天海雪,我揣著相機與錄音锦爵,去河邊找鬼。 笑死奥裸,一個胖子當著我的面吹牛险掀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播湾宙,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼樟氢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了侠鳄?” 一聲冷哼從身側響起埠啃,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎伟恶,沒想到半個月后碴开,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡知押,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年叹螟,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片台盯。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡罢绽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出静盅,到底是詐尸還是另有隱情良价,我是刑警寧澤寝殴,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站明垢,受9級特大地震影響蚣常,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜痊银,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一抵蚊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧溯革,春花似錦贞绳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至抖单,卻和暖如春萎攒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背矛绘。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工耍休, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蔑歌。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓羹应,卻偏偏與公主長得像,于是被迫代替她去往敵國和親次屠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 加密算法 對稱加密算法 加密和解密使用同一個密鑰 DES雳刺、3DES劫灶、AES、Blowfish掖桦、Twofish本昏、ID...
    毛利卷卷發(fā)閱讀 1,590評論 0 4
  • SSH全稱Secure SHell,顧名思義就是非常安全的shell的意思枪汪,SSH協(xié)議是IETF(Internet...
    StarShift閱讀 2,528評論 0 7
  • 被炙熱的火舌吞噬 被炸裂的痛苦掩埋 留下一小片 溫柔笑著的誓言 多少次午夜夢回不可抑制地想起他眉眼的溫柔 半哭半笑...
    之幕閱讀 215評論 0 0
  • 因為生活壓力太大雀久,年輕人整天唉聲嘆氣宿稀。某天,他去山中尋找一位大哲人赖捌,希望對方給他一個解脫之法祝沸。 哲人聽完他的訴說后...
    應果閱讀 667評論 0 0
  • 從上周開始濰坊的天氣就一直被烏云滿布的天空所籠罩;直到昨晚的一場狂風暴雨算是這些天的陰沉、烏蒙罩锐、和每天不定時的大雨...
    Sunny仔仔閱讀 652評論 1 2