原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://laoguang.blog.51cto.com/6013350/1109021
今天经理让规划一下公司的ssh管理,已前都是用密码,现在想用密钥,并且要易于管理,安全性高,于是有了以下规划:
1.将一台服务器Pb1的ssh服务暴露到公网作为跳板机,更改ssh的端口为28888
2.Pb1认证方式改为密钥方式,关闭密码认证
3.对于其它服务器如server1来说,除了Pb1服务器ip可以ssh外,其它一律禁止,关闭密码认证,端口用默认22即可
4.Pb1上生成ssh密钥对,将用户公钥传到其它服务器,以后连接其它服务器须先连接到Pb1,然后再跳转过去,见下图
操作步骤:以用户root为例,假设Pb1: 192.168.12.83 server1:192.168.11.78 ,其它用户操作相同
1.在xshell中生成密钥对
在tools中生成,步骤不再详述,将公钥复制到Pb1 root家目录的.ssh下的authorized_keys中
- mkdir ~/.ssh
- vi ~/.ssh/authorized_keys
- ssh-dss AAAAB3NzaC1kc3MAAACBAJzgf3UtrQo8mrP7okdbuOUAojVhD8KAYnzvDkl7b4HcMXmdFnnAjD3J92gBrvh
- mZgsz7YeVlL8/SafTyWZgH41gP9U/sFqNSG0n4XrdOHnDa0cGzO/0y0OLP+BoG/g3XfaeBYUrhcoInhJFomIla2bvDP
- E+9c8Q0AbuwPmQ04kXAAAAFQDjtjgZn6Lmjh3/IHKjNLSXh9WPtQAAAIAfAAkGfGNJbVXwp/h4lzV4q9pqN3FkyS/
- QEWonMlVBfWo0p6q72Z0UwScZ4PPpwVjDjTnGjGyhV/dxU7USUqPLqTwMbFHZvWpnVYwkqopijFjYaC1I4ofsGH9aO5
- hOrcbb4qAKCXTf2ljD+iEfw5qawYDG8H1XD2/mEWsDp8SuQAAAIBIUMwx7mkMNSBy+DlQHEv2K2CB51Ziqelrswhx13
- NeMcn17xwW5z6So4o2m01omAUcYrqodq+xR9H6WfauqqHbKDJGaZ1JzYWEl9au4an8F04zMKnGhQoXM7NARo82YROwR
- fnax8gRkG8Y+3r7+IU3Yvvya0P24TZPNhuiwIlU/w== ##这是xshell生成的,复制过来的
2.编辑Pb1中的/etc/ssh/sshd_config 更改端口为 28888 关闭密在码认证
- vi /etc/ssh/sshd_config
- Port 28888
- PasswordAuthentication no
- Service sshd restart
3.连接Pb1测试能否正常连接
4.Pb1上成生密钥对,并复制到server1中
- ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
- ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.11.78
- ssh-copy-id -i ~/.ssh/id_rsa.pub 其它server IP
5.在Pb1上连接server1测试能否连接成功
6.关闭server1 .. serverN中的密码认证,添加iptables,再次测试
- vi /etc/ssh/sshd_config
- PasswordAuthentication no
- service sshd restart
- iptables -A INPUT -p tcp -s ! 192.168.12.83 --dport 22 –j DROP
或
- vi /etc/hosts.allow
- sshd:192.168.1.83
- vi /etc/hosts.deny
- sshd:ALL
为了防止pb1出现故障后不能正常连接其它服务器,应设置另外一台最好不在同一网络或地区中的服务器做备用。
Pb1脚本:
- #!/bin/bash
- # Author: LaoGuang
- # Script Name: Pb.sh
- # 2013/01/05 ibuler@qq.com
- # Description:
- # Set ssh Middle Server
- serverip="192.168.11.78" ## one or some ip
- port=28888
- #Modify port and Authentication
- sed -i 's/#\?Port .*/Port 28888/' /etc/ssh/sshd_config
- sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
- service sshd restart &>/dev/null
- #Transfer key
- if ! [ -e ~/.ssh/id_rsa.pub ] && [ -e ~/.ssh/id_rsa ];then
- ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa &>/dev/null
- fi
- for i in $serverip
- do
- ssh-copy-id -i ~/.ssh/id_rsa.pub $i &>/dev/null ##这里其实写的不好,还有与用户互动
- ssh $i "echo : $i success "
- done
Server端脚本:
- #!/bin/bash
- # Author: LaoGuang
- # Script Name: server.sh
- # 2013/01/05 ibuler@qq.com
- # Description:
- # Set ssh Servers
- sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
- service sshd restart &>/dev/null
- iptables -A INPUT -p tcp -s ! 192.168.12.83 --dport 22 -j DROP
你有更好的管理方法吗?欢迎交流!
本文出自 “Free Linux,Share Linux” 博客,请务必保留此出处http://laoguang.blog.51cto.com/6013350/1109021