最初的SSH是由芬兰的一家公司开发的。但是因为受版权和加密算法的,现在很多人都转而使用OpenSSH。OpenSSH是SSH的替代软件包,而且是免费的,可以预计将来会有越来越多的人使用它而不是SSH。
SSH是由客户端和服务端的软件组成的,有两个不兼容的版本分别是:和。 用SSH 的客户程序是不能连接到SSH 的服务程序上去的。OpenSSH 同时支持SSH 和。 SSH协议的内容
SSH协议是建立在应用层和传输层基础上的安全协议,它主要由以下三部分组成,共同实现SSH的安全保密机制。
传输层协议,它提供诸如认证、信任和完整性检验等安全措施,此外它还可以任意地提供数据压缩功能。通常情况下,这些传输层协议都建立在面向连接的TCP数据流之上。
用户认证协议层,用来实现服务器的跟客户端用户之间的身份认证,它运行在传输层协议之上。
连接协议层,分配多个加密通道至一些逻辑通道上,它运行在用户认证层协议之上。
当安全的传输层连接建立之后,客户端将发送一个服务请求。当用户认证层连接建立之后将发送第二个服务请求。这就允许新定义的协议可以和以前的协议共存。连接协议提供可用作多种目的通道,为设置安全交互Shell会话和传输任意的TCP/IP端口和X11连接提供标准方法。 SSH的安全验证 从客户端来看,SSH提供两种级别的安全验证。
第一种级别(基于口令的安全验证),只要你知道自己的帐号和口令,就可以登录到远程主机,并且所有传输的数据都会被加密。但是,这种验证方 式不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人“这种攻击方式的攻击。
第二种级别(基于密匙的安全验证),需要依靠密匙,也就是你必须为自己创建一对密匙,并把公有密匙放在需要访问的服务器上。如果你要连接 到SSH服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在你在该服务器的用户根目录下寻找你的公有密 匙,然后把它和你发送过来的公有密匙进行比较。如果两个密匙一致,服务器就用公有密
匙加密“质询“(challenge)并把它发送给客户端软件。客户端 软件收到“质询“之后就可以用你的私人密匙解密再把它发送给服务器。
与第一种级别相比,第二种级别不需要在网络上传送用户口令。但是整个登录的过程可能慢一些。 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服务器软件的时候“加密通道“才能工 作。 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 879kB of archives. After unpacking 2130kB will be used.
Do you want to continue? [Y/n/?]y ...
Setting up openssh-server ...
Creating SSb RSA key; this may take some time ...
Creating SSb DSA key; this may take some time ...
Restarting OpenBSD Secure Shell server: sshd.
启动ssh服务
$ sudo /etc/ssh start 停止ssh服务
$ sudo /etc/ssh stop 重启ssh服务
$ sudo /etc/ssh restart
使用ssh首次连接一个远程ssh服务器时,会出现类似下边的信息。这是因为ssh不能识别这台主机,键入yes将会把这台服务器的信息写入
/.ssh/known_hosts文件,下次连入这台远程服务器时就不会出现这类信息。 tony@tonybox:~$ ssh
The authenticity of host ' ' 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 '' (RSA) to the list of known'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:~# 默认配置
默认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/
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes
如果没有特别要求使用默认设置即可满足使用要求. 基于口令的认证
缺省情况下,ssh仍然使用传统的口令验证,在使用这种认证方式时,我们不需要进行任何配置。你可以使用自己帐号和口令登录到远程主机。所 有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的 攻击。 使用以下方法登录服务器:
$ ssh'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 基于密钥的认证
密匙认证需要依靠密匙,可以使用ssh-keygen 命令生成密钥对,将会把生成的私钥存储在 /.ssh/id_rsa文件中,公钥存储在/.ssh/文件中,需要将其复制到远程服务 器上, 这样当登录远程服务器时,客户端软件就会向服务器发出请求,请求用你的密匙进行认证,服务器收到请求之后,先在你在该服务器的宿主目录下寻找你的公匙,进 行身份认证. 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/.
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并确保权限为4
tony@tonybox:~/.ssh$ scp 's password:
100% 394 s 00:00
tony@tonybox:~/.ssh$
这样,以后登录这台SSH服务器的时候,就会使用您上传的公钥进行身份认证。 tony@tonybox:~$ ssh
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 如果为密钥设置了 passphrase, 则登录过程如下:
tony@tonybox:~$ ssh
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 如果将客户端私钥 id_rsa 更名, 并在服务器端sshd_config文件中做如下设置:
PasswordAuthentication no
tony@tonybox:$ mv /home/tony/.ssh/id_rsa /home/tony/.ssh/
tony@tonybox:$ ssh
Permission denied (publickey).
tony@tonybox:~$
如果在服务器端sshd_config文件中做如下设置:
PasswordAuthentication yes
则当密钥口令输入错误, 或密钥不存在是,就会使用口令认证
tony@tonybox:~$ ssh
Enter passphrase for key '/home/tony/.ssh/id_rsa''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 使用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来指定私钥的位置:
如果您仍然使用 telnet, 而不是 ssh, 则需要改变对本手册的阅读方式. 应当用 ssh 来取代所有的 telnet 远程登录。任何时候通过嗅探互联网通讯来获取明文密码都是相当简单的, 您应该采用使用加密算法的协议. 那么, 现在在你的系统上执行 apt-get install ssh。
鼓励您系统上的所有用户使用 ssh 取代 telnet, 或者更进一步, 卸载 telnet/telnetd. 另外您应该避免使用 ssh 以 root 身份登录, 其替代的方法是使用 su
或 sudo 转换成 root 用户。最后, /etc/ssh 目录下的 sshd_config 文件, 应当作如下修改, 以增强安全性:
ListenAddress 使得 ssh 只监听一个指定的接口, 如果你有多个(并不想在其上边获得 ssh 服务)接口, 或者将来会增加一块新网卡(但并不想通过它连接ssh服务).
PermitRootLogin no
尝试任何情况先都不允许 Root 登录. 如果有人想通过 ssh 成为 root, 需要两次登录, 并且root的密码现在仍不可能通过SSH暴力破解.
Listen 666
改变监听端口, 这样入侵者不能完全确定是否运行了sshd守护进程(事先警告,这是模糊安全的).
PermitEmptyPasswords no
空密码是对系统安全的嘲弄.
AllowUsers alex ref me@somewhere
只允许某些用户通过 ssh 访问主机. user@host 也可用于指定用户通过指定主机访问.
AllowGroups wheel admin
仅允许某个组的成员通过 ssh 访问主机. AllowGroups 和 AllowUsers 对于拒绝访问主机有同样的效果. 当称它们为 \"DenyUsers\" 和 \"DenyGroups\" 时不要觉得奇怪.
PasswordAuthentication yes
这完全取决于您的选择. 仅仅允许用户使用置于 ~/.ssh/authorized_keys 文件中的 ssh-keys 登录主机将更加安全. 如果要达到这种效果,将其设为 \"no\".
禁用所有的您不需要的认证方式, 如果您用不到, 例如 RhostsRSAAuthentication, HostbasedAuthentication, KerberosAuthentication 或 RhostsAuthentication(例如), 您应该将其禁用, 即使它们是缺省设置(参阅联机帮助 sshd_config(5)).
Protocol 2
禁用版本1协议, 因为其设计缺陷, 很容易使密码被黑掉. 更多信息, 参阅 ssh协议问题报告 或 Xforce 通告.
Banner /etc/some_file
为用户连接到 ssh 服务器增加一个标题(它将从文件读取), 在一些国家, 登入给定系统前, 给出未经授权或者用户监视警告信息, 将会受到法律的保护. 利用Public Key实现ssh的认证
使用许多的ssh工具,最后发现只有putty可以成功的实现ssh认证,其他的都没有搞定.(winxp+, linux +
Publick Key认证简介 Publick Key认证的主要魅力在于认证时承诺不必提供密码就能够同远程系统建立连接。Publick Key认证的基础在于一对密钥,public key 和private key,public key对数据进行加密而且只能用于加密,private key 只能对所匹配的public key加密过的数据进行解密。我们把public key放在远程系统合适的位置,然后从本地开始进行ssh连接。此时,远程的sshd会产生一个随机数并用我们产生的public key进行加密后发给本地,本地会用private key进行解密并把这个随机数发回给远程系统。最后,远程系统的sshd会得出结论我们拥有匹配的private key允许我们登录。 制造密钥用putty套装的puttygen来产生密钥。密钥的类型建议选择SSH2 RSA,因为用的最为广泛,大多数server支持。密钥的位数选择1024对大多数用户已经足够,除非你觉得还是不安全。然后点 击\"Generator\此时密钥生成当然也有几个选项出现。如果你有不止一个key,你可以在“Key comment”作注释用于区别其他的key。 “Key passphrase“和“Confirm passphrase“用于对硬盘上的key进行加密,如过你自己一个人用机器觉得安全有保障的话可以让它们为空。然后保存两个key。不要关掉 puttygen,它还有用呢。 输送public key到远程系统在远程系统自己的目录下建立目录\".ssh\然后进入目录建立一个新文件authorized_keys ,然后把puttygen中\"Public key for pasting into authorized_keys file\"粘贴于authorized_keys这个文件中。然后执行以下命令: chmod 700 $HOME/.ssh chmod 600 $HOME/.ssh/authorized_keys chmod g-w $HOME $HOME/.ssh $HOME/.ssh/authorized_keys 测试执行putty,建立一个session命名为ssh,设定ip,协议设为ssh,Connection设定ssh的 auth中,private key的文件选择为puttygen产生的密钥。连接开始后,要求输入username,远程系统开始public key认证,如果密钥有passphrase 则需要输入,否则直接就登录了。 如果你有是linux,也可以用openssh自带的密钥产生工具:ssh-keygen
ssh-keygen -t rsa
Permission denied (publickey) 问题的解决
这个问题一般是由于没有正确的配置publickey认证方式引起的.问题发生的Openssh-server端 一般来说可以尝试如下的解决办法.
如果你不需要用ssh的方式, 可以简单的禁用它. 修改 /etc/ssh/sshd_config (注意是sshd_config,不是ssh_config, 这个让我郁闷了两个小时) RSAAuthentication yes PubkeyAuthentication yes
为
RSAAuthentication no PubkeyAuthentication no
即可. 然后可以通过如下选项打开传统的秘密认证方式: PasswordAuthentication yes
如果你确实需要publickey的认证方式那么你应该看看用户在server端的主目录的.ssh目录及其下面的文件的权限了. 目录的权限是700, 文件的权限应该是600. 如果不是, chmod吧. (这个不用我多说了吧) 还可能有Pam认证的问题. (Pam认证是一种密码认证, 如果你不想使用, 可以关掉. 如果你只想使用publickey的认证方式, 那么必须关掉).在sshd_config中可以通过改 UsePAM yes 为
UserPAM no 关闭Pam认证.
如果你不想关闭Pam认证. 那么你就该检查检查检查你的/etc/ssh文件的配置. 下面是openssh的标准配置, 实在不行可以用这个试试.
# PAM configuration for the Secure Shell service
# Read environment variables from /etc/environment and # /etc/security/.
auth required # [1]
# Standard Un*x authentication. @include common-auth
# Standard Un*x authorization. @include common-account
# Standard Un*x session setup and teardown. @include common-session
# Print the message of the day upon successful login. session optional # [1]
# Print the status of the user's mailbox upon successful login. session optional standard noenv # [1]
# Set up user limits from /etc/security/. session required
# Set up SELinux capabilities (need modified pam) # session required multiple
# Standard Un*x password updating. @include common-password
因篇幅问题不能全部显示,请点此查看更多更全内容