openssh 安装配置
1.1 什么是SSH ? 传统的网络服务程序,如FTP、Pop和Telnet在传输机制和实现原理上是没有考虑安全机制的,其本质上都是不安全的;因为它们在网络上用明文 传送数据、用户帐号和用户口令,别有用心的人通过窃听等网络攻击手段非常容易地就可以截获这些数据、用户帐号和用户口令。而且,这些网络服务程序的简单安 全验证方式也有其弱点,那就是很容易受到“中间人“(man-in-the-middle)这种攻击方式的攻击。所谓“中间人“的攻击方式,就是“中间人 “冒充真正的服务器接收你的传给服务器的数据,然后再冒充你把数据传给真正的服务器。服务器和你之间的数据传送被“中间人“一转手做了手脚之后,就会出现 很严重的问题。
SSH是英文Secure Shell的简写形式。通过使用SSH,你可以把所有传输的数据进行加密,这样“中间人“这种攻击方式就不可能实现了,而且也能够防止DNS欺骗和IP欺 骗。使用SSH,还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的速度。SSH有很多功能,它既可以代替Telnet,又可以为 FTP、Pop、甚至为PPP提供一个安全的 “通道“。
最初的SSH是由芬兰的一家公司开发的。但是因为受版权和加密算法的限制,现在很多人都转而使用OpenSSH。OpenSSH是SSH的替代软件包,而且是免费的,可以预计将来会有越来越多的人使用它而不是SSH。
SSH是由客户端和服务端的软件组成的,有两个不兼容的版本分别是:1.x和2.x。 用SSH 2.x的客户程序是不能连接到SSH 1.x的服务程序上去的。OpenSSH 2.x同时支持SSH 1.x和2.x。 1.2 SSH协议的内容
SSH协议是建立在应用层和传输层基础上的安全协议,它主要由以下三部分组成,共同实现SSH的安全保密机制。
传输层协议,它提供诸如认证、信任和完整性检验等安全措施,此外它还可以任意地提供数据压缩功能。通常情况下,这些传输层协议都建立在面向连接的TCP数据流之上。
用户认证协议层,用来实现服务器的跟客户端用户之间的身份认证,它运行在传输层协议之上。
连接协议层,分配多个加密通道至一些逻辑通道上,它运行在用户认证层协议之上。
当安全的传输层连接建立之后,客户端将发送一个服务请求。当用户认证层连接建立之后将发送第二个服务请求。这就允许新定义的协议可以和以前的协议共存。连接协议提供可用作多种目的通道,为设置安全交互Shell会话和传输任意的TCP/IP端口和X11连接提供标准方法。1.3 SSH的安全验证
从客户端来看,SSH提供两种级别的安全验证。
第一种级别(基于口令的安全验证),只要你知道自己的帐号和口令,就可以登录到远程主机,并且所有传输的数据都会被加密。但是,这种验证方 式不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人“这种攻击方式的攻击。
第二种级别(基于密匙的安全验证),需要依靠密匙,也就是你必须为自己创建一对密匙,并把公有密匙放在需要访问的服务器上。如果你要连接 到SSH服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在你在该服务器的用户根目录下寻找你的公有密 匙,然后把它和你发送过来的公有密匙进行比较。如果两个密匙一致,服务器就用公有密匙加密“质询“(challenge)并把它发送给客户端软件。客户端 软件收到“质询“之后就可以用你的私人密匙解密再把它发送给服务器。
与第一种级别相比,第二种级别不需要在网络上传送用户口令。但是整个登录的过程可能慢一些。1.4 SSH的应用
首先,SSH最常见的应用就是,用它来取代传统的Telnet、FTP等网络应用程序,通过SSH登录到远方机器执行你想进行的工作与命 令。在不安全的网路通讯环境中,它提供了很强的验证(authentication)机制与非常安全的通讯环境。实际上,SSH开发者的原意是设计它来取 代原UNIX系统上的rcp、rlogin、rsh等指令程序的;但经过适当包装后,发现它在功能上完全可以取代传统的Telnet、FTP等应用程序。
传统 BSD 风格的 r 系列指令(如 rcp,rsh,rlogin)往往都被视为不安全的,很容易就被各种网络攻击手段所破解,几乎所有找得到有关 UNIX安全的书或文件,都会一而再、再而三地警告系统管理者,留心r系列指令的设定,甚至要求系统管理者将r系列指令通通关闭。
而用来替代r系列指令的SSH,则在安全方面做了极大的强化,不但对通讯内容可以进行极为安全的加密保护,同时也强化了对身份验证的安全 机制,它应用了在密码学(Cryptography)中已发展出来的数种安全加密机制,如 Symmetric Key Cryptography, Asymmetric Key Cryptography, One-way Hash Function,Random- number Generation等,来加强对于身份验证与通讯内容的安全保护。通讯时资料的加密有IDEA,three- key triple DES,DES,RC4-128,TSS,Blowfish 等数种多种安全加密算法可供选择,加密的key则是通过 RSA 进行交换的。资料的加密可以对抗IP spoofing,RSA这种非对称性的加密机制则可用来对抗DNS spoofing与 IP routing spoofing,同时RSA也可以进行对主机身份的验证。
其次,通过使用用SSH可以在本地主机和远程服务器之间设置“加密通道“,并且这样设置的“加密通道“可以跟常见的Pop应用程序、X应 用程序、Linuxconf应用程序相结合,提供安全保障。 SSH的“加密通道“是通过“端口转发“来实现的。你可以在本地端口(没有用到的)和在远程服务器上运行的某个服务的端口之间建立“加密通道“。然后只要 连接到本地端口。所有对本地端口的请求都被SSH加密并且转发到远程服务器的端口。当然只有远程服务器上运行SSH服务器软件的时候“加密通道“才能工 作。
2.1 openssh的安装和启动
tony@tonybox:~$ sudo aptitude updatetony@tonybox:~$ sudo aptitude install openssh-server Reading package lists... Done Building dependency tree... Done Reading extended state information Initializing package states... Done Reading task descriptions... Done Building tag database... Done The following NEW packages will be automatically installed: libedit2 openssh-client The following NEW packages will be installed: libedit2 openssh-client openssh-server 0 packages upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 55.4kB/879kB of archives. After unpacking 2130kB will be used. Do you want to continue? [Y/n/?]y ... Setting up openssh-server (4.3p2-5) ... Creating SSb RSA key; this may take some time ... Creating SSb DSA key; this may take some time ... Restarting OpenBSD Secure Shell server: sshd. //创建服务器RSA/DSA密钥对 tony@tonybox:~$
安装完毕后默认生成以下配制文件:
tony@tonybox:~$ ls /etc/ssh -l total 160 -rw-r--r-- 1 root root 132839 2006-10-07 23:10 moduli //ssh服务器的Diffie-Hellman密钥文件 -rw-r--r-- 1 root root 1423 2006-10-07 23:10 ssh_config //ssh客户端配置文件 -rw-r--r-- 1 root root 1874 2006-10-20 17:45 sshd_config //ssh服务器配置文件 -rw------- 1 root root 668 2006-10-20 17:46 ssh_host_dsa_key //ssh服务器dsa算法私钥 -rw-r--r-- 1 root root 602 2006-10-20 17:46 ssh_host_dsa_key.pub //ssh服务器dsa算法公钥 -rw------- 1 root root 1679 2006-10-20 17:46 ssh_host_rsa_key //ssh服务器rsa算法私钥 -rw-r--r-- 1 root root 394 2006-10-20 17:46 ssh_host_rsa_key.pub //ssh服务器rsa算法公钥 tony@tonybox:~$
如果没有特别要求使用默认设置即可满足使用要求.
启动ssh服务
$ sudo /etc/init.d/ssh start
停止ssh服务
$ sudo /etc/init.d/ssh stop
重启ssh服务
$ sudo /etc/init.d/ssh restart
使用ssh首次连接一个远程ssh服务器时,会出现类似下边的信息。这是因为ssh不能识别这台主机,键入yes将会把这台服务器的信息写入 /.ssh/known_hosts文件,下次连入这台远程服务器时就不会出现这类信息。
tony@tonybox:~$ ssh root@192.168.102.50 The authenticity of host '192.168.102.50 (192.168.102.50)' can't be established. RSA key fingerprint is c6:d4:e7:23:03:ce:15:2c:08:ec:39:7e:52:29:a5:a6. Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.102.50' (RSA) to the list of known hosts. root@192.168.102.50's password:****** Last login: Thu Oct 26 10:38:41 2006 Linux vmdebian 2.6.16-2-486 #1 Fri Aug 18 18:39:04 UTC 2006 i686 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. vmdebian:~#
2.2 默认配置
默认sshd_config文件
# Package generated configuration file # See the sshd(8) manpage for details# What ports, IPs and protocols we listen for Port 22 #默认使用22端口 # Use these options to restrict which interfaces/protocols sshd will bind to #ListenAddress :: #ListenAddress 0.0.0.0 Protocol 2 #使用ssb协议 # HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key #主机密钥存储在此 #Privilege Separation is turned on for security UsePrivilegeSeparation yes #需要sshd用户启动ssh服务 # Lifetime and size of ephemeral version 1 server key KeyRegenerationInterval 3600 ServerKeyBits 768 服务器在启动时生成这个密钥。并以固定的周期重新生成。这里指定长度是768位,最小为512,周期为3600。 # Logging SyslogFacility AUTH #设置syslog的facility(KERN,DAEMON,USER,AUTH,MAIL等) LogLevel INFO #指定记录日志级别为INFO,该值从低到高顺序是:QUIET,FATAL,ERROR,INFO,VERBOSE,DEBUG,使用DEBUG会侵犯用 户的隐私权,这个级别只能用于诊断,而不能用于普通操作。 # Authentication: LoginGraceTime 120 #设置如果用户不能成功登录,在切断连接之前服务器需要等待的时间(以秒为单位)。 PermitRootLogin yes #允许root登录 StrictModes yes #设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。新手会把自己的目录和文件设成任何人都有写权限。 RSAAuthentication yes PubkeyAuthentication yes #AuthorizedKeysFile %h/.ssh/authorized_keys # Don't read the user's ~/.rhosts and ~/.shosts files IgnoreRhosts yes #完全禁止SSHD使用.rhosts文件 # For this to work you will also need host keys in /etc/ssh_known_hosts RhostsRSAAuthentication no #设置是否使用用RSA算法的基于rhosts的安全验证。 # similar for protocol version 2 HostbasedAuthentication no # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication #IgnoreUserKnownHosts yes #设置ssh daemon是否在进行RhostsRSAAuthentication安全验证的时候忽略用户的“$HOME/.ssh/known_hosts” # To enable empty passwords, change to yes (NOT RECOMMENDED) PermitEmptyPasswords no #设置不允许使用空密码 # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no #关闭挑战响应 # Change to no to disable tunnelled clear text passwords #PasswordAuthentication yes #设置是否使用明文密码认证 # Kerberos options #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #有关Kerberos的相关选项 # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes #有关GSSAPI的相关选项 X11Forwarding yes #允许X转发 X11DisplayOffset 10 PrintMotd no PrintLastLog yes TCPKeepAlive yes #UseLogin no #MaxStartups 10:30:60 #Banner /etc/issue.net # Allow client to pass locale environment variables AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server UsePAM yes
如果没有特别要求使用默认设置即可满足使用要求.2.3 基于口令的认证
缺省情况下,ssh仍然使用传统的口令验证,在使用这种认证方式时,我们不需要进行任何配置。你可以使用自己帐号和口令登录到远程主机。所 有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的 攻击。
使用以下方法登录服务器:
$ ssh tony@192.168.102.50 tony@192.168.102.50's password: Linux vmdebian 2.6.16-2-486 #1 Fri Aug 18 18:39:04 UTC 2006 i686The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Fri Dec 8 12:31:58 2006 from 192.168.102.249 $
2.4 基于密钥的认证
密匙认证需要依靠密匙,可以使用ssh-keygen 命令生成密钥对,将会把生成的私钥存储在 /.ssh/id_rsa文件中,公钥存储在/.ssh/id_rsa.pub文件中,需要将其复制到远程服务 器上, 这样当登录远程服务器时,客户端软件就会向服务器发出请求,请求用你的密匙进行认证,服务器收到请求之后,先在你在该服务器的宿主目录下寻找你的公匙,进 行身份认证. ssh-keygen 默认使用rsa算法生成密钥,如果要使用dsa算法,则需要使用-t 指定比如($ ssh-keygen -t dsa)
tony@tonybox:~$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/tony/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/tony/.ssh/id_rsa. Your public key has been saved in /home/tony/.ssh/id_rsa.pub. The key fingerprint is: 5e:25:fe:32:af:96:e5:e2:c9:55:ad:f9:d2:f1:67:5d tony@tonybox tony@tonybox:~$
然后使用scp 命令将公钥上传到远程SSH服务器的对应用户的.ssh目录下,并更名为authorized_keys并确保权限为644
tony@tonybox:~/.ssh$ scp id_rsa.pub tony@192.168.102.50:.ssh/authorized_keys tony@192.168.102.50's password: id_rsa.pub 100% 394 0.4KB/s 00:00 tony@tonybox:~/.ssh$
这样,以后登录这台SSH服务器的时候,就会使用您上传的公钥进行身份认证。
tony@tonybox:~$ ssh tony@192.168.102.50 Linux vmdebian 2.6.16-2-486 #1 Fri Aug 18 18:39:04 UTC 2006 i686The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Oct 26 12:59:12 2006 from 192.168.102.43 tony@vmdebian:~$
如果为密钥设置了 passphrase, 则登录过程如下:
tony@tonybox:~$ ssh tony@192.168.102.50 Enter passphrase for key '/home/tony/.ssh/id_rsa': Linux vmdebian 2.6.16-2-486 #1 Fri Aug 18 18:39:04 UTC 2006 i686The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Oct 26 14:27:58 2006 from 192.168.102.43 tony@vmdebian:~$
如果将客户端私钥 id_rsa 更名, 并在服务器端sshd_config文件中做如下设置:
PasswordAuthentication no
tony@tonybox:$ mv /home/tony/.ssh/id_rsa /home/tony/.ssh/id_rsa.bak tony@tonybox:$ ssh tony@192.168.102.50 Permission denied (publickey). tony@tonybox:~$
如果在服务器端sshd_config文件中做如下设置:
PasswordAuthentication yes
则当密钥口令输入错误, 或密钥不存在是,就会使用口令认证
tony@tonybox:~$ ssh tony@192.168.102.50 Enter passphrase for key '/home/tony/.ssh/id_rsa': tony@192.168.102.50's password: Linux vmdebian 2.6.16-2-486 #1 Fri Aug 18 18:39:04 UTC 2006 i686The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Oct 26 17:30:43 2006 from 192.168.102.43 tony@vmdebian:~$3. 使用windows客户端
在windows客户端下也有很多ssh的客户端,比如 putty,它同时兼容ssh1和ssb,在它的网站上提供了很多客户端软 件下载, 我们通常使用到的是putty和puttygen, 其中puttygen 用于生成密钥对, 操作非常简单,值得注意的是, 如果您使用putty以密钥认证方式连接ssh服务器, putty并不能很好的识别ssh-keygen 生成的私钥, 需要使用puttygen工具进行密钥转换, 操作如下:
载入公钥
输入公钥的passphrase
然后使用Save private key按钮, 将转换后的私钥导出即可得到putty可以识别的私钥. 就我个个人而言我更倾向于使用pietty, 它是putty的改进, 对文字编码支持更为丰富, 使用起来更具人性化, 下图为以pietty连接ssh服务器, 我们可以通过选项的SSH->Auth来指定私钥的位置: