荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: georgehill (人生不定式), 信区: Linux
标  题: Linux系统中OpenSSH的安装和配置(转寄)
发信站: BBS 荔园晨风站 (Fri Sep  8 13:24:43 2000), 站内信件

【 以下文字转载自 georgehill 的信箱 】
【 原文由 georgehill.bbs@smth.org 所发表 】
发信人: starw (虫二·抛愚引砖), 信区: Linux
标  题: Linux系统中OpenSSH的安装和配置(转寄)
发信站: BBS 水木清华站 (Fri Sep  8 04:08:10 2000)

http://www.linuxaid.com.cn/solution/posts/OpenSSh.html

Linux系统中OpenSSH的安装和配置
by brimmer


概述
许多网络程序,如telnet、rsh、rlogin或rexec,用明文(plain text)传送口
令和秘密的信息,所以就可利用任何连接到网络上的计算机监听这些程序和服务
器之间的通信并获取口令和秘密信息。现在,telnet程序对于日常的管理工作是
必不可少的,但是它又是不安全的,那么用什么来替代它呢?OpenSSH就是那些过
时的、不安全的远程登录程序,如:telnet、rlogin、rsh、rdist或rcp的替代品。

在OpenSSH的README文件中提到:ssh(Secure Shell)程序可以通过网络登录到
远程主机并执行命令。它提供了很强的安全验证可以在不安全的网络中进行安全
的通信。

我们把OpenSSH配置成支持TCP-Wrappers(inetd超级服务器),这样能够进一步
地提高安全性而且也没有必要把OpenSSH作为守护进程(daemon)在后台运行。当
客户端的程序提出连接请求的时候,TCP-Wrappers守护进程就会在把连接重定向
到OpenSSH之前,对连接请求进行验证和授权。OpenSSH是自由软件而且使用不受
专利保护的加密算法。因此,我建议你使用OpenSSH(免费而且修正了一些bug),
而不使用SSH1(免费但是有bug)和SSH2(现在使用商用的许可协议)。

注意事项
下面所有的命令都是Unix兼容的命令。

源路径都为“/var/tmp”(当然在实际情况中也可以用其它路径)。

安装在RedHat Linux 6.1和6.2下测试通过。

要用“root”用户进行安装。

OpenSSH的版本是1.2.3。

软件包的来源
OpenSSH的主页:http://violet.ibs.com.au/openssh/。

下载:openssh-1.2.3.tar.gz。

准备工作
编译OpenSSH需要zlib-devel软件包,这个软件包包括头文件和函数库。编译使用
zlib的压缩和解压函数的程序,就要事先安装这个软件包。可以用RedHat 6.1或
6.2的光盘安装。

l        用下面的命令验证一下在系统中是否已经安装了zlib-devel软件包:

[root@deep /]# rpm -qi zlib-devel

l        用下面命令在系统中安装zlib-devel软件包:

[root@deep /]# mount /dev/cdrom /mnt/cdrom/
[root@deep /]# cd /mnt/cdrom/RedHat/RPMS/
[root@deep RPMS]# rpm -Uvh zlib-devel-version.i386.rpm
gd ##################################################
[root@deep RPMS]# rpm -Uvh gd-devel-version.i386.rpm
zlib-devel ##################################################
[root@deep RPMS]# cd /; umount /mnt/cdrom/

在使用OpenSSH之前,还必须安装OpenSSL。因为就算你不使用OpenSSL创建或保存
加密文件,OpenSSH需要用OpenSSL的库文件才能正常运行。

安装软件包需要注意的问题
最好在编译前和编译后都做一张系统中所有文件的列表,然后用“diff”命令去
比较它们,找出其中的差别并知道到底把软件安装在哪里。只要简单地在编译之
前运行一下命令“find /* >OpenSSH1”,在编译和安装完软件之后运行命令
“find /* > OpenSSH2”,最后用命令“diff OpenSSH1 OpenSSH2 >
OpenSSH-Installed”找出变化。

编译和安装
把软件包(tar.gz)解压:

[root@deep /]# cp openssh-version.tar.gz /var/tmp
[root@deep /]# cd /var/tmp
[root@deep tmp]# tar xzpf openssh-version.tar.gz

编译和优化
第一步

转到OpenSSH的新目录下,先设置编译器的编译参数:

CC="egcs" \
CFLAGS="-O9 -funroll-loops -ffast-math -malign-double -mcpu=pentiumpro
 -march=pentiumpro -fomit-frame-
pointer -fno-exceptions" \
./configure \
--prefix=/usr \
--sysconfdir=/etc/ssh \
--with-tcp-wrappers \
--with-ipv4-default \
--with-ssl-dir=/usr/include/openssl

这些设置告诉编译器如何编译OpenSSH:

l        链接上libwrap函数库并且加上对TCP Wrappers的支持

l        禁止Linux/glibc-2.1.2中域名解析的延时,缩短建立连接的时间

l        设置OpenSSL函数库的路径,这样OpenSSH才能正常运行

第二步

现在,编译和安装OpenSSH:

[root@deep openssh-1.2.3]# make
[root@deep openssh-1.2.3]# make install
[root@deep openssh-1.2.3]# make host-key
[root@deep openssh-1.2.3]# install -m644 contrib/redhat/sshd.pam
/etc/pam.d/sshd

“make”命令把源文件编译成可执行的二进制文件,“make install”把二进制
文件和配置文件安装在合适的目录下。“make host-key”生成主机密匙,
“install”命令在RedHat Linux上安装对OpenSSH的PAM支持。

清除不必要的文件
用下面的命令删除不必要的文件:

[root@deep /]# cd /var/tmp
[root@deep tmp]# rm -rf openssh-version/ openssh-version.tar.gz

“rm”命令删除所有编译和安装OpenSSH所需要的源程序,并且把OpenSSH软件的
压缩包删除掉。

配置
可以到这去下载“floppy.tgz”文件:http://www.openna.com/books/floppy.tgz。
把“floppy.tgz”文件解开之后,可以在相应的目录下发现我们在这本书中
介绍的所有软件的配置文件。这样就没有必要手工重新生成这些文件,或者用拷
贝粘贴的方法把它们粘贴到配置文件中去。不管是打算自己动手生成配置文件还
是拷贝现成的,你都要学会自己修改配置文件并且把配置文件拷贝到正确的目录
下。下面将具体说明。

为了运行OpenSSH,必须创建或者把下面的文件拷贝到相应的目录下:

l        把“sshd_config”文件拷贝到“/etc/ssh”目录下

l        把“ssh_config”文件拷贝到“/etc/ssh”目录下

l        把“ssh”文件拷贝到“/etc/pam.d/”目录下

可以把“floppy.tgz”解压之后,找到上面列出来的文件,并拷贝到合适的目录
下,或者用拷贝粘贴的方法从本书中直接粘贴出。

配置“/etc/ssh/ssh_config”文件
“/etc/ssh/ssh_config”文件是OpenSSH系统范围的配置文件,允许你通过设置
不同的选项来改变客户端程序的运行方式。这个文件的每一行包含“关键词-值”
的匹配,其中“关键词”是忽略大小写的。下面列出来的是最重要的关键词,用
man命令查看帮助页(ssh (1))可以得到详细的列表。

编辑“ssh_config”文件(vi /etc/ssh/ssh_config),添加或改变下面的参数:

# Site-wide defaults for various options
Host *
ForwardAgent no
ForwardX11 no
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
FallBackToRsh no
UseRsh no
BatchMode no
CheckHostIP yes
StrictHostKeyChecking no
IdentityFile ~/.ssh/identity
Port 22
Cipher blowfish
EscapeChar ~

下面逐行说明上面的选项设置:

Host *
选项“Host”只对能够匹配后面字串的计算机有效。“*”表示所有的计算机。

ForwardAgent no
“ForwardAgent”设置连接是否经过验证代理(如果存在)转发给远程计算机。

ForwardX11 no
“ForwardX11”设置X11连接是否被自动重定向到安全的通道和显示集(DISPLAY
 set)。

RhostsAuthentication no
“RhostsAuthentication”设置是否使用基于rhosts的安全验证。

RhostsRSAAuthentication no
“RhostsRSAAuthentication”设置是否使用用RSA算法的基于rhosts的安全验证。

RSAAuthentication yes
“RSAAuthentication”设置是否使用RSA算法进行安全验证。

PasswordAuthentication yes
“PasswordAuthentication”设置是否使用口令验证。

FallBackToRsh no
“FallBackToRsh”设置如果用ssh连接出现错误是否自动使用rsh。

UseRsh no
“UseRsh”设置是否在这台计算机上使用“rlogin/rsh”。

BatchMode no
“BatchMode”如果设为“yes”,passphrase/password(交互式输入口令)的提
示将被禁止。当不能交互式输入口令的时候,这个选项对脚本文件和批处理任务
十分有用。

CheckHostIP yes
“CheckHostIP”设置ssh是否查看连接到服务器的主机的IP地址以防止DNS欺骗。
建议设置为“yes”。

StrictHostKeyChecking no
“StrictHostKeyChecking”如果设置成“yes”,ssh就不会自动把计算机的密匙
加入“$HOME/.ssh/known_hosts”文件,并且一旦计算机的密匙发生了变化,就
拒绝连接。

IdentityFile ~/.ssh/identity
“IdentityFile”设置从哪个文件读取用户的RSA安全验证标识。

Port 22
“Port”设置连接到远程主机的端口。

Cipher blowfish
“Cipher”设置加密用的密码。

EscapeChar ~
“EscapeChar”设置escape字符。

配置“/etc/ssh/sshd_config”文件
“/etc/ssh/sshd_config”是OpenSSH的配置文件,允许设置选项改变这个daemon
的运行。这个文件的每一行包含“关键词-值”的匹配,其中“关键词”是忽略
大小写的。下面列出来的是最重要的关键词,用man命令查看帮助页(sshd (8))
可以得到详细的列表。

编辑“sshd_config”文件(vi /etc/ssh/sshd_config),加入或改变下面的参
数:

# This is ssh server systemwide configuration file.
Port 22
ListenAddress 192.168.1.1
HostKey /etc/ssh/ssh_host_key
ServerKeyBits 1024
LoginGraceTime 600
KeyRegenerationInterval 3600
PermitRootLogin no
IgnoreRhosts yes
IgnoreUserKnownHosts yes
StrictModes yes
X11Forwarding no
PrintMotd yes
SyslogFacility AUTH
LogLevel INFO
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
PermitEmptyPasswords no
AllowUsers admin

下面逐行说明上面的选项设置:

Port 22
“Port”设置sshd监听的端口号。

ListenAddress 192.168.1.1
“ListenAddress”设置sshd服务器绑定的IP地址。

HostKey /etc/ssh/ssh_host_key

“HostKey”设置包含计算机私人密匙的文件。

ServerKeyBits 1024
“ServerKeyBits”定义服务器密匙的位数。

LoginGraceTime 600
“LoginGraceTime”设置如果用户不能成功登录,在切断连接之前服务器需要等
待的时间(以秒为单位)。

KeyRegenerationInterval 3600
“KeyRegenerationInterval”设置在多少秒之后自动重新生成服务器的密匙(如
果使用密匙)。重新生成密匙是为了防止用盗用的密匙解密被截获的信息。

PermitRootLogin no
“PermitRootLogin”设置root能不能用ssh登录。这个选项一定不要设成“yes”。

IgnoreRhosts yes
“IgnoreRhosts”设置验证的时候是否使用“rhosts”和“shosts”文件。

IgnoreUserKnownHosts yes
“IgnoreUserKnownHosts”设置ssh daemon是否在进行RhostsRSAAuthentication
安全验证的时候忽略用户的“$HOME/.ssh/known_hosts”

StrictModes yes
“StrictModes”设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件
的权限和所有权。这通常是必要的,因为新手经常会把自己的目录和文件设成任
何人都有写权限。

X11Forwarding no
“X11Forwarding”设置是否允许X11转发。

PrintMotd yes
“PrintMotd”设置sshd是否在用户登录的时候显示“/etc/motd”中的信息。

SyslogFacility AUTH
“SyslogFacility”设置在记录来自sshd的消息的时候,是否给出“facility
code”。

LogLevel INFO
“LogLevel”设置记录sshd日志消息的层次。INFO是一个好的选择。查看sshd的
man帮助页,已获取更多的信息。

RhostsAuthentication no
“RhostsAuthentication”设置只用rhosts或“/etc/hosts.equiv”进行安全验
证是否已经足够了。

RhostsRSAAuthentication no
“RhostsRSA”设置是否允许用rhosts或“/etc/hosts.equiv”加上RSA进行安全
验证。

RSAAuthentication yes
“RSAAuthentication”设置是否允许只有RSA安全验证。

PasswordAuthentication yes
“PasswordAuthentication”设置是否允许口令验证。

PermitEmptyPasswords no
“PermitEmptyPasswords”设置是否允许用口令为空的帐号登录。

AllowUsers admin
“AllowUsers”的后面可以跟着任意的数量的用户名的匹配串(patterns)或
user@host这样的匹配串,这些字符串用空格隔开。主机名可以是DNS名或IP地址。

配置OpenSSH使其使用TCP-Wrappers inetd超级服务器
TCP-WRAPPERS用来启动和停止sshd1服务。当inetd运行的时候,它会从配置文件
(默认为“/etc/inetd.conf”)中读入配置信息。在配置文件中每一行的不同项
是用TAB或空格分开。

第一步

编辑“inetd.conf”文件(vi /etc/inetd.conf)并加入这一行:

ssh stream tcp nowait root /usr/sbin/tcpd sshd –i

注意:“-i”参数很重要,它说明sshd是被inetd运行的。在加入这一行后,通过
发送一个SIGHUP信号(killall –HUP inetd)来更新“inetd.conf”文件。

[root@deep /root]# killall -HUP inetd

第二步

编辑“hosts.allow”文件(vi /etc/hosts.allow)并加入这一行:

sshd: 192.168.1.4 win.openarch.com

这一行表示IP地址为“192.168.1.4”,主机名为“win.openarch.com”的计算机
允许用ssh访问服务器。

下面这些“daemon”字符串(用于TCP-WRAPPERS)被sshd1使用:

sshdfwd-X11 (允许/禁止X11转发).
sshdfwd-<port-number> (TCP转发).
sshdfwd-<port-name> (port-name在/etc/services中定义。用于TCP转发).

注意:如果准备使用ssh,一定要用在所有的服务器上。如果十台安全的服务器和
一台不安全的服务器配在一起,也谈不上什么安全性。

更多的资料
如果想查找详细的资料可以用man命令查帮助页,读取相关信息:

$ man ssh (1) - OpenSSH secure shell client (remote login program)
$ man ssh [slogin] (1) - OpenSSH secure shell client (remote login program)
$ man ssh-add (1) - adds identities for the authentication agent
$ man ssh-agent (1) - authentication agent
$ man ssh-keygen (1) - authentication key generation
$ man sshd (8) - secure shell daemon

SSH1每用户配置
第一步

为本地服务器创建私有和公用密匙,执行下面的命令:

[root@deep]# su username
[username@deep]$ ssh-keygen1

举个例子,显示出来的结果可能是:

Initializing random number generator...
Generating p: ............................++ (distance 430)
Generating q: ......................++ (distance 456)
Computing the keys...
Testing the keys...
Key generation complete.
Enter file in which to save the key (/home/username/.ssh/identity):
【按下回车键】
Enter passphrase:
Enter the same passphrase again:
Your identification has been saved in /home/username/.ssh/identity.
Your public key is:
1024 37
1493775751125195553369112031847729386229004939471513651114580610887000
1764378494676831
2975778431585322723612061006231460440536487184367748423324091941848098
8907860997175244
4697758964712775703072877997370856999301704314156353633306888894403817
8461608592483844
590202154102756903055846534063365635584899765402181 username@deep.open
arch.com
Your public key has been saved in /home/username/.ssh/identity.pub

注意:如果有多个帐号需要为每个帐号创建一个密匙。

你可能要为下面的服务器创建密匙:

l        Mail服务器

l        Web服务器

l        网关服务器

这允许对这些服务器进行有限的访问,例如,不允许用Mail服务器的帐号访问We
b服务器或网关服务器。这样可以增加整体的安全性,即使因为某种原因有一个密
匙被泄密了,也不会影响到其它的服务器。

第二步

把本机的公用密匙(identity.pub)拷贝到远程主机的“/home/username/.ssh”
目录下,例如,使用“authorized_keys”这个名字。

注意:拷贝文件的一个方法使用ftp命令,另一个办法是把公用密匙用email(包
含“~/.ssh/identity.pub”文件的内容)发给系统管理员。

改变pass-phrase

用加上“-p”参数的“ssh-keygen”命令,在任何时候都可以改变pass-phrase。
用下面的命令,改变pass-phrase:

[root@deep]# su username
[username@deep]$ ssh-keygen1 –p

Enter file key is in (/home/username/.ssh/identity): [按下回车键]
Enter old passphrase:
Key has comment 'username@deep.openarch.com'
Enter new passphrase:
Enter the same passphrase again:
Your identification has been saved with the new passphrase.

OpenSSH用户工具
下面列出的是一些我们经常要用到的命令,当然还有很多其它的命令,更详细的
信息可以查看man帮助页或其它文档。

ssh
ssh(Secure Shell)是用来登录远程计算机和在远程计算机上执行命令的程序。
它是用来替代rlogin和rsh,以及在不安全的网络环境下在两台计算机之间提供安
全和加密的信息交流。X11连接和TCP/IP端口可以被转发到一个安全的通道里。

用下面的命令,登录远程计算机:

[root@deep]# ssh <login_name> <hostname>

例如:

[root@deep]# ssh username www.openarch.com
username@deep.openarch.com’s password:
Last login: Tue Oct 19 1999 18:13:00 -0400 from gate.openarch.com
Welcome to www.openarch.com on Deepforest.

<login_name>是用来登录ssh服务器的用户名,<hostname>是ssh服务器主机的地
址。

scp
可以用这个命令把文件从本地计算机拷贝到远程计算机,或者反之,甚至可以在
两台远程计算机之间用“scp”命令拷贝文件。把远程主机上的文件拷贝到当前目
录的一个简单的方法如下。

用下面的命令把文件从远程主机拷贝到本地主机上:

[root@deep /]# su admin
[admin@deep /]$ scp -p <login_name@hostname>:/dir/for/file localdir/to
/filelocation

例如:

[username@deep]$ scp -p username@mail:/etc/test1 /tmp
Enter passphrase for RSA key 'username@mail.openarch.com':
test1 | 2 KB | 2.0 kB/s | ETA: 00:00:00 | 100%

用下面的命令把文件从本地主机拷贝到远程主机上:

[root@deep /]# su admin
[admin@deep /]$ scp -p localdir/to/filelocation <username@hostname>:
/dir/for/file

例如:

[username@deep]$ scp -p /usr/bin/test2 username@mail:/var/tmp
username@mail's password:
test2 | 7 KB | 7.9 kB/s | ETA: 00:00:00 | 100%

注意:“-p”选项表示文件的改变和访问时间属性以及权限,在拷贝过程中被保
留。通常是需要这样的。

安装到系统中的文件
> /etc/ssh
> /etc/ssh/ssh_config
> /etc/ssh/sshd_config
> /etc/ssh_host_key
> /etc/ssh_host_key.pub
> /usr/bin/ssh
> /usr/bin/slogin
> /usr/man/man1/ssh.1
> /usr/man/man1/scp.1
> /usr/man/man1/ssh-add.1
> /usr/man/man1/ssh-agent.1
> /usr/man/man1/ssh-keygen.1
> /usr/bin/scp
> /usr/bin/ssh-add
> /usr/bin/ssh-agent
> /usr/bin/ssh-keygen
> /usr/man/man1/slogin.1
> /usr/man/man8/sshd.8
> /usr/sbin/sshd

Windows平台上免费的SSH客户软件
Putty
Putty的主页:http://www.chiark.greenend.org.uk/~sgtatham/putty.html

Tera Term Pro and TTSSH
Tera Term Pro的主页:http://hp.vector.co.jp/authors/VA002416
/teraterm.html

TTSSH Homepage:http://www.zip.com.au/~roca/download.html

版权说明
这篇文章翻译和改编自Gerhard Mourani的《Securing and Optimizing Linux:
RedHat Edition》,原文及其版权协议请参考:http://www.openna.com/。

中文版的版权属于作者brimmer和http://www.linuxaid.com.cn/。


--

        寒山惟白云,寂寂绝埃尘。草座山家有,孤灯明月轮。
        石床临碧沼,鹿虎每为邻。自羡幽居乐,长为世外人。


※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.112.11.199]
--
※ 转载:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 192.168.1.115]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店