资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SSH批量分发与管理

一、SSH服务介绍

成都创新互联公司专注于太仆寺网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供太仆寺营销型网站建设,太仆寺网站制作、太仆寺网页设计、太仆寺网站官网定制、微信小程序开发服务,打造太仆寺网络公司原创品牌,更为您提供太仆寺网站排名全网营销落地服务。

SSH是SecureShellProtocol的简写,由IETF网络工作小组制定;在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后再进行数据传输,确保了传递的数据安全。

SSH是专为远程登录会话和其他网络服务提供的安全性协议。利用SSH协议可以有效的防止远程管理过程中的信息泄露问题,在当前的生产环境当中,绝大多数企业普遍采用SSH协议服务来代替传统的不安全的远程联机服务软件。如telnet等。

SSH服务结构:

SSH服务是由服务端软件OpenSSH和客户端(常见的有SSH,SecureCRT,Xshell,Putty)组成,SSH服务默认使用22端口提供服务,它有两个不兼容的SSH协议版本,分别是1.x和2.x。

二、SSH服务认证类型

从SSH客户端来看,SSH服务主要有两种级别安全验证,具体级别如下:

1.基于口令的安全认证

2.基于密钥对的安全认证:基于密钥的安全认证也有windows客户端和linux客户端的区别。

三、SSH服务优化

修改sshd.conf

Port52113#为了提高安全级别,建议改掉SSH服务默认连接端口 PermitRootLoginno#root超级用户***都知道,建议禁止它(root)远程登陆 PermitEmptyPasswordsno#禁止空密码登录 Usednsno#不使用DNS GSSAPIAuthenticationno#加快SSH连接速度

四、SSH批量分发管理实战

批量分发数据或者文件

(1)添加系统账号,并修改密码

[root@A~]#useraddfenfa [root@A~]#idfenfa uid=503(fenfa)gid=503(fenfa)groups=503(fenfa) [root@A~]#echo123456|passwd--stdinfenfa

注意:所有被管理主机都要创建该用户

(2)创建密钥对

[fenfa@A~]$ssh-keygen-tdsa Generatingpublic/privatedsakeypair. Enterfileinwhichtosavethekey(/home/fenfa/.ssh/id_dsa): Createddirectory\'/home/fenfa/.ssh\'. Enterpassphrase(emptyfornopassphrase): Entersamepassphraseagain:#此处回车 Youridentificationhasbeensavedin/home/fenfa/.ssh/id_dsa. Yourpublickeyhasbeensavedin/home/fenfa/.ssh/id_dsa.pub. Thekeyfingerprintis:#此处回车 0e:99:ef:7f:2d:5c:36:88:79:09:7a:89:e0:d1:f7:fcfenfa@A Thekey\'srandomartp_w_picpathis:#此处回车 +--[DSA1024]----+ || || |.| |oo.o| |.+oS+Bo| |.+o=*+| |o.o=.| |.+E| |.....| +-----------------+

默认会在fenfa用户的家目录/home/fenfa/.ssh下生成两个文件:

id_dsa.pub#公钥,权限644,分发给需要管理的主机

id_dsa#私钥,权限600,保留在本地

(3)推送公钥到管理主机

实例为推送到192.168.0.111主机,192.168.0.112同样的方法推送

[fenfa@A~]$ssh-copy-id-i.ssh/id_dsa.pub"-p22fenfa@192.168.0.111" Theauthenticityofhost\'192.168.0.111(192.168.0.111)\'can\'tbeestablished. RSAkeyfingerprintis85:83:52:21:20:dd:4a:7c:3c:df:ec:5a:de:a0:b4:82. Areyousureyouwanttocontinueconnecting(yes/no)?yes Warning:Permanentlyadded\'192.168.0.111\'(RSA)tothelistofknownhosts. fenfa@192.168.0.111\'spassword: Nowtryloggingintothemachine,with"ssh\'-p22fenfa@192.168.0.111\'",andcheckin: .ssh/authorized_keys#出现这个表示推送公钥成功 tomakesurewehaven\'taddedextrakeysthatyouweren\'texpecting.

注意:ssh-copy-id的特殊应用

如果SSH修改成了特殊端口,如52113,那么,用上面的ssh-copy-id命令就无法进行分发公钥匙了。如果仍要使用ssh-copy-id的话,那么可能的解决方法有两个:

1.命令为:ssh-copy-id-i.ssh/id_dsa.pub"-p52113fenfa@192.168.0.111"#特殊端口分发,要适当加引号

2.编辑vi/usr/bin/ssh-copy-id在第41行做如下修改,见加粗部分

41{eval"$GET_ID";}|ssh-p22$1"umask077;test-d~/.ssh||

mkdir~/.ssh;cat>>~/.ssh/authorized_keys&&(test-x/sbin/

restorecon&&/sbin/restorecon~/.ssh~/.ssh/authorized_keys>/d

ev/null2>&1||true)"||exit1#在41行中的开头ssh后面和$1前面加入自定义的ssh端口

说明:ssh-copy-id的原理(ssh-copy-id-i.ssh/id_dsa.pub"-p52113fenfa@192.168.0.111")

就是把.ssh/id_dsa.pub复制到192.168.0.111的fenfa用户家目录下面的.ssh目录(提前创建,权限为700)下,并做了更改名字的操作,名字改为authorized_keys,权限变为600.

(4)检查被管理主机上的公钥

[fenfa@B~]$ll.ssh/ total4 -rw-------1fenfafenfa598Jul2522:59authorized_keys [fenfa@C~]$ll.ssh/ total4 -rw-------1fenfafenfa598Jul2522:47authorized_keys

(5)批量分发测试

[fenfa@A~]$whoami fenfa [fenfa@A~]$echo123>a.txt [fenfa@A~]$ll total4 -rw-rw-r--1fenfafenfa4Jul2600:00a.txt [fenfa@A~]$cata.txt 123 [fenfa@A~]$scp-P22a.txtfenfa@192.168.0.111:~ a.txt100%40.0KB/s00:00 [fenfa@A~]$scp-P22a.txtfenfa@192.168.0.112:~ a.txt100%40.0KB/s00:00

批量分发脚本:

①建立被管理主机地址库

②创建批量分发脚本

实例:将分发主机(192.168.1.114)家目录的text.txt文件分发到被管理主机(192.168.1.113,192.168.1.115)

[fenfa@server_04~]$catip.txt 192.168.1.113 192.168.1.115 [fenfa@server_04~]$catplfenfa.sh #!/bin/bash ./etc/profile ./etc/init.d/functions File_name=test.txt File_dir=/home/fenfa Fenfa_user=fenfa foripin`cat/home/fenfa/ip.txt` do rsync-avz-e"ssh-p52113"${File_dir}/$File_name${Fenfa_user}@$ip:~ action"${Fenfa_user}@$ip${File_dir}/$File_namecopy"/bin/true done [fenfa@server_04~]$ [fenfa@server_04~]$shplfenfa.sh sendingincrementalfilelist test.txt  sent91bytesreceived37bytes256.00bytes/sec totalsizeis9speedupis0.07 fenfa@192.168.1.113/home/fenfa/test.txtcopy[OK] sendingincrementalfilelist test.txt  sent91bytesreceived31bytes244.00bytes/sec totalsizeis9speedupis0.07 fenfa@192.168.1.115/home/fenfa/test.txtcopy[OK]

小结:

1)免密码登陆验证是单向的(管理主机--->被管理主机)

2)基于用户的,最好不要跨不同的用户

3)批量分发初始都需要输入一次密码,并且第一次连接要确认

(6)sudo对分发用户fenfa提权

root用户,visudo命令修改

在文件结尾加入一下内容:

fenfaALL=(ALL)NOPASSWD:/bin/cp

说明:

fenfa

ALL

(ALL)

NOPASSWD

/bin/cp

使用sudo的用户

允许使用sudo的主机

使用sudo免密码

使用sudo可以执行的命令,如果是允许所有命令填NOPASSWD:ALL

root用户,vim/etc/sudoers直接修改sudoers文件,同理添加同上面的内容

五、利用expect脚本实现免交互ssh密钥分发

1)安装expect执行环境

2)expect批量分发,免交互脚本示例

主要有两部分:fenfa_sshkey.exp和fenfa.sh ,使用时直接执行fenfa.sh即可

[root@server_05scripts]#catfenfa_sshkey.exp #!/usr/bin/expect if{$argc!=2}{ send_user"usage:expectfenfa_sshkey.expfilehost\\n" exit } #definevar setfile[lindex$argv0] sethost[lindex$argv1] setpassword"123456"##分发帐号的密码 setuser"fenfa"##分发帐号 setport"52113"##分发主机的ssh端口 spawnssh-copy-id-i$file"-p$port$user@$host" expect{ "yes/no"{send"yes\\r";exp_continue} "*password"{send"$password\\r"} } expecteof

[root@server_05scripts]#catfenfa.sh #!/bin/bash Ipaddr_head=192.168.1 User=fenfa Port=52113 Commond_dir=/usr/bin if[$UID-ne0] then echo"Error:Pleaseuserootaccounttoexecthisscript!" else fornin`seq5` do ${Commond_dir}/ssh-copy-id-i"-p$Port${User}@${Ipaddr_head}.$n"&2>/dev/null if[$?-eq0] then action"${Ipaddr_head}$ncopyssh_key..."/bin/ture else action"${Ipaddr_head}$ncopyssh_key..."/bin/false fi done fi

新闻标题:SSH批量分发与管理
本文网址:http://cdkjz.cn/article/cpodds.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220