一、实验环境

本次实验需至少两台机器,本文采用三台机器做讲解。

       服务器
      ip
    是否为分发机
    nfs-server
10.0.0.11
         是
  web-client01
10.0.0.12
         否
  web-client02
10.0.0.13
         否

二、实施步骤

1.添加系统账号(即普通用户)
分别使用了三台机器,一台为分发机,另两台为被分发机。
在企业环境中,很多用户直接用root来操作,这样好很不规范,权限非常大,很不安全。我们本次实验使用普通用户来操作。

1)给每台机器添加一个普通用户
useradd chen
id chen
echo "chen"|passwd --stdin chen  
2)切换到普通用户下
su - chen
3)在分发机上生成秘钥(此步骤是在普通用户下执行,直接回车到底)  
ssh-keygen -t dsa(在工作中使用较多) 或使用:
ssh-keygen -t rsa
 ~]$ ssh-keygen  -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/chen/.ssh/id_dsa):
Created directory '/home/chen/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/chen/.ssh/id_dsa.
Your public key has been saved in /home/chen/.ssh/id_dsa.pub.
The key fingerprint is:
de:11:d3:01:12:78:43:52:5e:34:cf:7c:96:c1:e9:ec chen@nfs-server
The key's randomart p_w_picpath is:
+--[ DSA 1024]----+
|      .+=o=..... |
|      .ooo * .oo |
|       ...o =o+  |
|           o oo  |
|        S .  .   |
|       . . .  E  |
|        . .      |
|                 |
|                 |
+-----------------+
[chen@nfs-server ~]$
~]$ ll .ssh/
-rw-------. 1 chen chen 668 6月   9 00:02 id_dsa(私钥)
-rw-r--r--. 1 chen chen 605 6月   9 00:02 id_dsa.pub(公钥)

4)分发公钥到其他的机器  
ssh-copy-id -i .ssh/id_dsa.pub  chen@10.0.0.12

[chen@web-client01 ~]$ ll -la .ssh/
总用量 12
drwx------. 2 chen chen 4096 6月   8 18:58 .
drwx------. 4 chen chen 4096 6月   8 18:58 ..
-rw-------. 1 chen chen  605 6月   8 18:58 authorized_keys(如发现切换到其他的用户能发现生成这个文件时,表示分发成功)
[chen@web-client01 ~]$

ssh-copy-id的特殊应用:如果已经修改了端口则需加上-p参数,且需用引号引起来。如:ssh-copy-id -i id_dsa.pub "-p 520520 chen@10.0.0.12"


免密码登录小结:
    1)免密码登录时单向的。
    2)基于用户,最好不要跨用户
    3)ssh连接慢的问题,修改/ect/ssh/sshd_config
    4)批量分发1000台初始都需要输入一次密码,并确定一次。
如下为一个简单的脚本实现对所需文件分发:
#!/bin/bash
file="$1"
remotefile="$2"
. /etc/init.d/functions
if [ $# -ne 2 ];then
        echo "at least input two argus:"
        exit 1
fi
for i in 12 13
do
        scp -P22 -rp $1 chen@10.0.0.$i:~ >/dev/null 2>&1
        if [ $? -eq 0 ];then
                action "scp $file to $remotefile is ok" /bin/true
        else
                action "scp $file to $remotefile is failed" /bin/false
        fi
done

      通过sudo提权即可实现普通用户能向没有权限的目录下拷贝文件。脚本如下:前提:visudo 修改98行内容。脚本如下:
#!/bin/bash
file="$1"
#!/bin/bash
file="$1"
remotefile="$2"
. /etc/init.d/functions

if [ $# -ne 2 ];then
        echo "at least input two argus:"
        exit 1
fi
for i in 12 13
do
        scp -P22 -rp $1 chen@10.0.0.$i:~ >/dev/null 2>&1
        ssh -p22 -t chen@10.0.0.$i sudo /bin/cp ~/$file $remotefile &>/dev/null
        if [ $? -eq 0 ];then
                action "scp $file to $remotefile is ok" /bin/true
        else
                action "scp $file to $remotefile is failed" /bin/false
        fi
done                                                                                                                                                                                                                                                                                              ssh批量分发与权限管理方案小结:
    1.利用root做ssh-key验证
        优点:简单、方便
        缺点:不安全、同时无法禁止root远程连接
    2.利用普通用户来做,把要分发的文件拷贝到服务器普通用户的家目录下,然后通过sudo提权将文件拷贝到所需的相应目录中。  
        优点:安全
        缺点:配置复杂
    3.设置suid对固定命令提权   

企业中生产场景批量管理,自动化管理方案:
1.ssh-key:最简单常用工具
2.puppt :门户级别比较流行,复杂笨重
3.saltstack :特点简单强大但配置复杂