阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

SSH批量部署服务

450次阅读
没有评论

共计 5750 个字符,预计需要花费 15 分钟才能阅读完成。

SSH 批量部署服务

1.1 在 NFS 上作为中心分发服务器将私钥分发到其他服务器上

1.1.1NFS 部署

[root@nfs-server ~]# useradd linuxidc
[root@nfs-server ~]# echo 123456|passwd –stdin linuxidc
Changing password for user linuxidc.
passwd: all authentication tokens updated successfully.
创建密码对:
[root@nfs-server ~]# su – linuxidc    ## 切换到 linuxidc 用户下,以后批量分发都在当前用户下,安全考虑
[linuxidc@nfs-server ~]$ ssh-keygen -t dsa ##ssh-keygen 是生成秘钥的工具,- t 参数指建立秘钥的类型,这里建立 dsa 类型秘钥(还有一种类型的秘钥为 RSA,两者加密算法有区别)
Generating public/private dsa key pair.
Enter file in which to save the key (/home/linuxidc/.ssh/id_dsa):
Created directory ‘/home/linuxidc/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/linuxidc/.ssh/id_dsa.
Your public key has been saved in /home/linuxidc/.ssh/id_dsa.pub.
The key fingerprint is:
6f:65:c4:a6:fb:32:45:0c:85:c3:bc:87:8f:a4:ae:bc linuxidc@nfs-server
The key’s randomart image is:
+–[DSA 1024]—-+
|        o o.    |
|          *.    |
|          *+    |
|          +++    |
|        So.=o    |
|        …+o    |
|      .  +.    |
|    .  ..o.    |
|      Eo  o.    |
+—————–+
[linuxidc@nfs-server ~]$
[linuxidc@nfs-server ~]$ ls -l .ssh/
total 8
-rw——-. 1 linuxidc zhurui 672 Mar  5 04:23 id_dsa  ## 私钥
-rw-r–r–. 1 linuxidc zhurui 607 Mar  5 04:23 id_dsa.pub  ## 公钥
将公钥分发给 web-lamp01 服务器
[linuxidc@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub linuxidc@192.168.1.12  ## 将公钥分发给 1.12 服务器
The authenticity of host ‘192.168.1.12 (192.168.1.12)’ can’t be established.
RSA key fingerprint is d6:e6:e6:2a:c7:df:99:51:bb:f4:90:29:16:df:c4:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.1.12’ (RSA) to the list of known hosts.
Nasty PTR record “192.168.1.12” is set up for 192.168.1.12, ignoring
linuxidc@192.168.1.12’s password:
Permission denied, please try again.
linuxidc@192.168.1.12’s password:
Now try logging into the machine, with “ssh ‘linuxidc@192.168.1.12′”, and check in:

  .ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

[linuxidc@nfs-server ~]$

1.1.2 web-lnmp02 客户端分发部署

[linuxidc@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub linuxidc@192.168.1.13  ## 将公钥分发到 1.13 服务器
The authenticity of host ‘192.168.1.13 (192.168.1.13)’ can’t be established.
RSA key fingerprint is d6:e6:e6:2a:c7:df:99:51:bb:f4:90:29:16:df:c4:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.1.13’ (RSA) to the list of known hosts.
Nasty PTR record “192.168.1.13” is set up for 192.168.1.13, ignoring
linuxidc@192.168.1.13’s password:
Now try logging into the machine, with “ssh ‘linuxidc@192.168.1.13′”, and check in:

  .ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

[linuxidc@nfs-server ~]$

1.1.3 rsync-backup 客户端分发部署

[linuxidc@nfs-server ~]$ ssh-copy-id -i .ssh/id_dsa.pub linuxidc@192.168.1.17
The authenticity of host ‘192.168.1.17 (192.168.1.17)’ can’t be established.
RSA key fingerprint is d6:e6:e6:2a:c7:df:99:51:bb:f4:90:29:16:df:c4:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.1.17’ (RSA) to the list of known hosts.
linuxidc@192.168.1.17’s password:
Now try logging into the machine, with “ssh ‘linuxidc@192.168.1.17′”, and check in:

  .ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

[linuxidc@nfs-server ~]$

2.1 在 NFS 上测试

2.1.1 通过 ssh 命令在当前机器上查看 web-lamp01 的 IP 地址

[linuxidc@nfs-server ~]$ ssh -P22 linuxidc@192.168.1.12 /sbin/ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:49:CE:B3 
          inet addr:192.168.1.12  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe49:ceb3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7701 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4795 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4806676 (4.5 MiB)  TX bytes:484902 (473.5 KiB)
注:在命令执行过程中,跳过输入密码的步骤
接着分发文件测试:
[linuxidc@nfs-server ~]$ cp /etc/hosts
hosts        hosts.allow  hosts.deny 
[linuxidc@nfs-server ~]$ cp /etc/hosts .
[linuxidc@nfs-server ~]$ ll
[linuxidc@nfs-server ~]$ scp -P22 hosts linuxidc@192.168.1.12:~ ## 将当前目录下 hosts 文件分发到 1.12 家目录下
hosts                                          100%  243    0.2KB/s  00:00   
[linuxidc@nfs-server ~]$

检查 1.12 上 linuxidc 家目录下有无 hosts 文件
[root@lamp01 linuxidc]# cat /home/linuxidc/hosts
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.11 nfs-server
192.168.1.17 backup
192.168.1.12 lamp01
192.168.1.13 lnmp02
[root@lamp01 linuxidc]#
通过脚本分发:
[linuxidc@nfs-server ~]$ sh fenfa.sh
hosts                                          100%  257    0.3KB/s  00:00   
hosts                                          100%  257    0.3KB/s  00:00   
hosts                                          100%  257    0.3KB/s  00:00

批量管理脚本:

#!/bin/sh
. /etc/init.d/functions
if [$# -ne 1]
  then
    echo “USAGE:$0 USAGE|COMMAND”
    exit 1
fi
for n in 12 13 17
do
  ssh -p22 linuxidc@192.168.1.$n $1
done

~

批量分发脚本:

#!/bin/sh
. /etc/init.d/functions

for n in 12 13 17
do
  scp -P22 $1 linuxidc@192.168.1.$n:~ &>/dev/null
  if [$? -eq 0]
    then
      action “fenfa $1 ok” /bin/true
    else
      action “fenfa $1 ok” /bin/false
  fi
done

~

利用分发脚本分发 hosts 文件

1 [linuxidc@nfs-server ~]$ sh fenfa.sh hosts
2 fenfa hosts ok                                            [OK]
3 fenfa hosts ok                                            [OK]
4 fenfa hosts ok                                            [OK]
5 [linuxidc@nfs-server ~]$

优化后的分发脚本

#!/bin/sh
. /etc/init.d/functions
if [$# -ne 1]
  then
    echo “USAGE:$0 {FILENAME|DIRNAME}”
    exit 1
fi
for n in 12 13 17
do
  scp -P22 -r $1 linuxidc@192.168.1.$n:~ &>/dev/null
  if [$? -eq 0]
    then
      action “fenfa $1 ok” /bin/true
    else
      action “fenfa $1 ok” /bin/false
  fi
done

下面关于 SSH 相关的文章您也可能喜欢,不妨参考下:

Ubuntu 下配置 SSH 服务全过程及问题解决 http://www.linuxidc.com/Linux/2011-09/42775.htm

Ubuntu 14.04 下安装 Samba 及 SSH 服务端的方法 http://www.linuxidc.com/Linux/2015-01/111971.htm

SSH 服务远程访问 Linux 服务器登陆慢 http://www.linuxidc.com/Linux/2011-08/39742.htm

提高 Ubuntu 的 SSH 登陆认证速度的办法 http://www.linuxidc.com/Linux/2014-09/106810.htm

开启 SSH 服务让 Android 手机远程访问 Ubuntu 14.04  http://www.linuxidc.com/Linux/2014-09/106809.htm

如何为 Linux 系统中的 SSH 添加双重认证 http://www.linuxidc.com/Linux/2014-08/105998.htm

在 Linux 中为非 SSH 用户配置 SFTP 环境 http://www.linuxidc.com/Linux/2014-08/105865.htm

Linux 上 SSH 服务的配置和管理 http://www.linuxidc.com/Linux/2014-06/103627.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-03/128978.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计5750字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19348
评论数
4
阅读量
7804673
文章搜索
热门文章
开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南 大家好,我是星哥。之前介绍了腾讯云的 Code...
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板 1Panel:小白轻松构建 Web 服务与面板加固指南 哈喽,我是星哥,经常有人问我不...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级 大家好,我是星哥,之前写了一篇文章 自己手撸一...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...
安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装 Black 群晖 DSM7.2 系统安装教程(在 Vmware 虚拟机中、实体机均可)! 前言 大家好,...
还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手! 前言 对于个人开发者、建站新手或是想搭建测试站点的从业者...
免费领取huggingface的2核16G云服务器,超简单教程

免费领取huggingface的2核16G云服务器,超简单教程

免费领取 huggingface 的 2 核 16G 云服务器,超简单教程 前言 HuggingFace.co...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级 大家好,我是星哥,之前写了一篇文章 自己手撸一...
每天一个好玩的网站-手机博物馆-CHAZ 3D Experience

每天一个好玩的网站-手机博物馆-CHAZ 3D Experience

每天一个好玩的网站 - 手机博物馆 -CHAZ 3D Experience 一句话介绍:一个用 3D 方式重温...
150元打造低成本NAS小钢炮,捡一块3865U工控板

150元打造低成本NAS小钢炮,捡一块3865U工控板

150 元打造低成本 NAS 小钢炮,捡一块 3865U 工控板 一块二手的熊猫 B3 工控板 3865U,搭...
如何免费使用强大的Nano Banana Pro?附赠邪修的用法

如何免费使用强大的Nano Banana Pro?附赠邪修的用法

如何免费使用强大的 Nano Banana Pro?附赠邪修的用法 前言 大家好,我是星哥,今天来介绍谷歌的 ...
支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare 也瘫了连监控都挂,根因藏在哪? 最近两天的互联网堪称“故障...