CentOS下用OpenSSH构建SSH服务器

上一篇 / 下一篇  2008-11-10 21:54:37 / 个人分类:Lunix 点滴

  • 文件版本: V1.0
  • 开发商: 本站原创
  • 文件来源: 本地
  • 界面语言: 简体中文
  • 授权方式: 免费
  • 运行平台: Win9X/Win2000/WinXP

(^z4K'nn6o&t^0前  言

,Y;SA%I%[(hI?$q C0 51Testing软件测试网4SlMV!O+i:C n5V

  SSH服务和Telnet服务一样,通过远程登录登录到系统,在远程操控系统。但它与Telnet的不同点就是:Telnet在传输的过程中是平文传输,而SSH是将传输内容加密,在传送的过程中保证了传送内容的保密性,从而提高了系统的安全性。
a~B&jzA051Testing软件测试网\B!x:{M9m9@
  在这里,我们不准备将SSH服务作为用户上传下载文件的工具。我们只用SSH服务的开通为远程管理系统提供方便。另外在用户认证方式上,为了服务器和用户的安全,禁止用户密码的认证方式,而基于“钥匙”的方式。

vt~,? J0

51Testing软件测试网,[9ot*z[]4a V

_xqJLT0

SSH相关配置文件的修改

1e.z Jbr xi+l"Nd aN0

3F:p/\bfhE0
f!j"`)nc0  首先修改SSH的配置文件。如下:51Testing软件测试网bb{(F@ Suk6G7Z,z

4q+Y.b0nR,S;b GC ^'Z$O051Testing软件测试网9z%Btc Yu&G ]6p

5Eud2f3x+k}0

[root@sample ~]# vi /etc/ssh/sshd_config  ← 用vi打开SSH的配置文件51Testing软件测试网"F:sE!N`KZ N
51Testing软件测试网 o,p0r9pl7s
#Protocol 2,1 ← 找到此行将行头“#”删除,再将行末的“,1”删除,只允许SSH2方式的连接51Testing软件测试网0\ o5R&W4lGO\.C
 ↓
tFS"l8RpWwHm0Protocol 2 ← 修改后变为此状态,仅使用SSH251Testing软件测试网$|7i#SkE"kZ5c5s

4r:?S7r"q%p;J0#ServerKeyBits 768 ← 找到这一行,将行首的“#”去掉,并将768改为1024
&sXC~%N*WsIF x0 ↓51Testing软件测试网5_ ls$\lG ^3k*T1N'l;Q
ServerKeyBits 1024 
← 修改后变为此状态,将ServerKey强度改为1024比特
:s3Xn3p*}2l|3][051Testing软件测试网 xcWg a&t2pc h
#PermitRootLogin yes  ← 找到这一行,将行首的“#”去掉,并将yes改为no51Testing软件测试网?'{"Vmn
 ↓
Fg"Y{t0PermitRootLogin no  
← 修改后变为此状态,不允许用root进行登录51Testing软件测试网/A C cY Ye

Ih2pkU.C0#PasswordAuthentication yes ← 找到这一行,将yes改为no51Testing软件测试网;o o{d`L5Q
 ↓
*b4sQ#g(c.K;uU0PasswordAuthentication no ← 修改后变为此状态,不允许密码方式的登录51Testing软件测试网;fE*I;u;oWjg

W$d%D L(b2w0#PermitEmptyPasswords no  ← 找到此行将行头的“#”删除,不允许空密码登录
BiEja0 ↓
wt u/P'MmY}0PermitEmptyPasswords no  ← 修改后变为此状态,禁止空密码进行登录
51Testing软件测试网 a k O/~szle.E"_

;l!Q7S7e(\WOLl051Testing软件测试网qG S}"zT
  然后保存并退出。(vi保存退出的命令为ZZ)
Rp)h;j.L;U| T'a0
51Testing软件测试网 \ER?/{u t _
  因为我们只想让SSH服务为管理系统提供方便,所以在不通过外网远程管理系统的情况下,只允许内网客户端通过SSH登录到服务器,以最大限度减少不安全因素。设置方法如下:51Testing软件测试网 n _y~U
51Testing软件测试网"J ` m _6\Ot5G {,r
51Testing软件测试网.Fc [&fo3CQ5],K

51Testing软件测试网XX6FRoI"F*y

[root@sample ~]# vi /etc/hosts.deny  ← 修改屏蔽规则,在文尾添加相应行51Testing软件测试网:f V0ncyQp^,T

m)dWG z9}0

#
f(TI,{j)| u3I{0# hosts.deny This file describes the names of the hosts which are51Testing软件测试网S)v:D.ut;E
# *not* allowed to use the local INET services, as decided51Testing软件测试网] Xp6l6I
# by the '/usr/sbin/tcpd' server.
&E @;nnQNN0#51Testing软件测试网d/\fv*tt9B
# The portmap line is redundant, but it is left to remind you that51Testing软件测试网'hf~%R'zQ-Gs
# the new secure portmap uses hosts.deny and hosts.allow. In particular51Testing软件测试网!h$G9X5C!h Wh+CR M
# you should know that NFS uses portmap!

[.re5^#~6y R0

sshd: ALL  ← 添加这一行,屏蔽来自所有的SSH连接请求51Testing软件测试网*M.Yb!sU.`zw
51Testing软件测试网x#kS@)Fk$Gy'wI
[root@sample ~]# vi /etc/hosts.allow  ← 修改允许规则,在文尾添加相应行
51Testing软件测试网r7Iyl!l O&h!n]

#
^pSd6y0# hosts.allow This file describes the names of the hosts which are
| J |-~V w&b A0# allowed to use the local INET services, as decided
#Sk[3e;\/Y)u r2P0# by the '/usr/sbin/tcpd' server.51Testing软件测试网rkH*pA
#
51Testing软件测试网;RBs0P(cv ds S

sshd: 192.168.0.  ← 添加这一行,只允许来自内网的SSH连接请求

1wy"ZJ;fz|+F0

U(|:F#b$DN@[ipaH051Testing软件测试网:|)[$_en1@c'_4p%a

g(sxn:Erg'[gn0 51Testing软件测试网6e;F)BRh;b&h%p|m

重新启动SSH服务
51Testing软件测试网'_2_| Q7UR

&[S;u|B3R#f0
;i:Gh4c _ rr0  在修改完SSH的配置文件后,需要重新启动SSH服务才能使新的设置生效。51Testing软件测试网q dw*CN)| S

ADRF D i0

1s^0SJ+e:H Z{0

%xmJZ,fN i WGJ0

[root@sample ~]# /etc/rc.d/init.d/sshd restart  ← 重新启动SSH服务器
LR9N/W#AB,on051Testing软件测试网8v#Zp;B8Ny l
Stopping sshd:             [ OK
]51Testing软件测试网*Z0L*m2_,PV4F'A
Starting sshd:             [ OK ]  ← SSH服务器重新启动成功

0y7[`0a|x0

#Uz8s_x:e0
u7[xj%J0  这时,在远程终端(自用PC等等)上,用SSH客户端软件以正常的密码的方式是无法登录服务器的。为了在客户能够登录到服务器,我们接下来建立SSH用的公钥与私钥,以用于客户端以“钥匙”的方式登录SSH服务器。51Testing软件测试网?__"d:ny)m*Rip
51Testing软件测试网?j.RZ,XqC+j
51Testing软件测试网4R Fdj3E'T\

51Testing软件测试网+bQ$[ U.N7V:s2?tE

SSH2的公钥与私钥的建立
51Testing软件测试网N#u0hY8p?E

nTXPp*h"B051Testing软件测试网+pq9ZD D1k
  登录为一个一般用户,基于这个用户建立公钥与私钥。(这里以centospub用户为例)51Testing软件测试网%`c o!g$Gf)a

G+o4ebB3j[9j051Testing软件测试网5V BthrR j

51Testing软件测试网5x l;f]P7k!S t

[root@sample ~]# su - centospub ← 登录为一般用户centospub51Testing软件测试网r x[#mhm/t,Z
51Testing软件测试网-E@v {'k1y
[centospub@sample ~]$ ssh-keygen -t rsa  ← 建立公钥与私钥
QB\Eo!@0
Generating public/private rsa key pair.51Testing软件测试网 eV8bA/|"SX
Enter file in which to save the key (/home/kaz/.ssh/id_rsa):  ← 钥匙的文件名,这里保持默认直接回车
C!z#i^9h(Zz$z0
Created directory '/home/kaz/.ssh'
;hoiYuS},d V0Enter passphrase (empty for no passphrase):  ← 输入口令
v(wV$} [i0Enter same passphrase again:   ← 再次输入口令
_~D nD rO0
Your identification has been saved in /home/kaz/.ssh/id_rsa.51Testing软件测试网)z7|"i&yo
Your public key has been saved in /home/kaz/.ssh/id_rsa.pub.51Testing软件测试网`[AQW _ {N
The key fingerprint is:51Testing软件测试网?sEn.r:d#Z/m~'V
tf:rs:e3:7s:28:59:5s:93:fe:33:84:01:cj:65:3b:8e centospub@sample.centospub.com
51Testing软件测试网6]@5Y4i @6T

51Testing软件测试网 sK:?-GDUW3~5ET

51Testing软件测试网4PF6S2~F.j/`W O{
  然后确认一下公钥与密钥的建立,以及对应于客户端的一些处理。
A#`` iNz,^,YBW0
s Ya Ur*QcV0

i7q}.c)P0

#I } F6nA0

[centospub@sample ~]$ cd ~/.ssh  ← 进入用户SSH配置文件的目录
$LXrJW$rv051Testing软件测试网-nK/G"xD
[centospub@sample .ssh]$ ls -l  ← 列出文件
&yO Z/X|0
total 16
(N{)` Ia~0-rw------- 1 centospub centospub 951 Sep 4 19:22 id_rsa  ← 确认私钥已被建立
)jL P*t@0-rw-r--r-- 1 centospub centospub 241 Sep 4 19:22 id_rsa.pub  ← 确认公钥已被建立
"y~!G F6d)Li051Testing软件测试网 w6}(O}G.D6g
[centospub@sample .ssh]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys  ← 公钥内容输出到相应文件中
a;aT+CIa[g051Testing软件测试网jV l0Pe;w!@
[centospub@sample .ssh]$ rm -f ~/.ssh/id_rsa.pub  ← 删除原来的公钥文件51Testing软件测试网(oQgf2kI"`x
51Testing软件测试网:r+XG!p mJ A
[centospub@sample .ssh]$ chmod 400 ~/.ssh/authorized_keys  ← 将新建立的公钥文件属性设置为400
51Testing软件测试网l1Z1}(dpz P A

51Testing软件测试网+|h^;}#x,fj7p'U

51Testing软件测试网-u0@ntM1BAX
  然后,将私钥通过安全的方式转移到欲通过SSH连接到服务器的PC上。这里,以通过3.5寸磁盘为介质为例:51Testing软件测试网C*G)^;l;X*O P

W4I,b7M\;|6X)b051Testing软件测试网/P6W1JY{ M%[PZ/k

c1E.JO]N-a2@0

centospub@sample .ssh]$ exit   ← 退出一般用户的登录(返回root的登录)
sO\.L v051Testing软件测试网|9sgGX
[root@sample ~]# mount /mnt/floppy/  ← 加载软盘驱动器51Testing软件测试网 { h(Y!r5y2~*nGNlv;by

(P{%cIm0[root@sample ~]# mv /home/centospub/.ssh/id_rsa /mnt/floppy/  ← 将刚刚建立的私钥移动到软盘
2_NE%T F-HU051Testing软件测试网 Aln9su!f
[root@sample ~]# umount /mnt/floppy/  ← 卸载软盘驱动器

;c9KX2RiaR0

Y9{2p|"f7l051Testing软件测试网cF;|.is8N K7\*S/mx
  51Testing软件测试网OE(P|wzDh]4Bm

TAG:

 

评分:0

我来说两句

Open Toolbar