中小企業服務器ssh管理
今天經理讓規劃一下公司的ssh管理,已前都是用密碼,現在想用密鑰,並且要易於管理,安全性高,於是有了以下規劃: www.2cto.com
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 ,其它用戶操作相同 www.2cto.com
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測試能否正常連接
ssh 192.168.12.83
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測試能否連接成功
ssh 192.168.11.78
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
[email protected]
# 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
[email protected]
# 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