中小企业最基本的实用的ssh-key秘钥方案之ssh免密码登陆实战  

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中小企业最基本的实用的ssh-key秘钥方案之ssh免密码登陆实战  相关的知识,希望对你有一定的参考价值。


一、实验环境

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

       服务器
      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 che[email protected]
The key‘s randomart image is:
+--[ DSA 1024]----+
|      .+=o=..... |
|      .ooo * .oo |
|       ...o =o+  |
|           o oo  |
|        S .  .   |
|       . . .  E  |
|        . .      |
|                 |
|                 |
+-----------------+
[[email protected] ~]$
~]$ 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  [email protected]

[[email protected] ~]$ 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(如发现切换到其他的用户能发现生成这个文件时,表示分发成功)
[[email protected] ~]$

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


免密码登录小结:
    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 [email protected]$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 [email protected]$i:~ >/dev/null 2>&1
        ssh -p22 -t [email protected]$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 :特点简单强大但配置复杂     

本文出自 “叫醒你的不是闹钟而是梦想” 博客,请务必保留此出处http://purify.blog.51cto.com/10572011/1787574

以上是关于中小企业最基本的实用的ssh-key秘钥方案之ssh免密码登陆实战  的主要内容,如果未能解决你的问题,请参考以下文章

中小企业最基本的实用的ssh-key秘钥方案之ssh免密码登陆实战  

非常强悍并实用的双机热备+负载均衡线上方案

Ansible ssh-key密钥认证配置

企业网络运维非常实用 之 时间同步与Syslog配置

多个github账号时,本地配置ssh-key

linux下数据推送(同步)方案