1./etc/sshd_config文件的详细解释


#       $OpenBSD: sshd_config,v 1.73 2005/12/06 22:38:28 reyk Exp $


# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.


# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin


# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.


#Port 22                    #####   使用的端口,可以进行修改
#Protocol 2,1               #####    使用的协议有1,2两个版本
Protocol 2                  #####    2比1要好
#AddressFamily any         
#ListenAddress 0.0.0.0      #####   设定监听的网卡,比如有2块网卡,设定相应网卡的IP即可
#ListenAddress ::


# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key        #####V版本1私钥位置
# HostKeys for protocol version 2     #####版本2加密文件位置
#HostKey /etc/ssh/ssh_host_rsa_key    #####版本2RSA私钥位置
#HostKey /etc/ssh/ssh_host_dsa_key    #####版本3DSA私钥位置


# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h         #####版本1公钥的重建时间
#ServerKeyBits 768                  #####Server key的长度 取默认值即可


# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV        ##### 日志文件 默认在/var/log/secure
#LogLevel INFO                 ##### 登录 记录的等级


# Authentication:


#LoginGraceTime 2m
#PermitRootLogin yes       ##### 是否允许root登录
#StrictModes yes           ##### 当用户的host key修改后,服务端不接受连接(可以方式***)
#MaxAuthTries 6            #####最大的验证次数
#RSAAuthentication yes     ##### 版本1是否使用纯正的RSA 验证?
#PubkeyAuthentication yes  #####针对版本2,是否使用Public Key?
#AuthorizedKeysFile     .ssh/authorized_keys    #使用不需要密码登陆时,的一个文件。此文件名必须要记好。
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no    #####本机系统不使用.rhosts,.rhosts非常不安全,所有要设定为no。
# similar for protocol version 2
#HostbasedAuthentication no    #####版本1使用.rhosts RAS进行加密
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no     #####时候忽略记录用户登录的信息
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes           #####是否取消.rhosts认证,当然要yes


# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes     #####密码是否要验证?
#PermitEmptyPasswords no        ##### 是否允许空密码?
PasswordAuthentication yes


# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no      #####是否允许任何模式验证密码?我们会使用pam来帮我们验证,所以设置为 no。             
# Kerberos options                #####我们没有 Kerberos主机,所以不设置
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no


# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes


# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no
UsePAM yes


# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes           #####是否显示上一次登录的时间,地点等。默认是yes。为了安全起见。要设置为no。查看的内容是/etc/motd
#PrintLastLog yes        #####显示上一次登录信息
#TCPKeepAlive yes        #####确保通信的双方都是保持连接的,任何一方失去连接将自动断开。
#UseLogin no
#UsePrivilegeSeparation yes        #####用户的权限设置项目
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10                 #####允许的最大几个尚未登录界面
#PermitTunnel no
#DenyUsers dd                  #####设置不允许登录的账号
#DenyGroups XXX                #####设置不允许登录的组


# no default banner path
#Banner /some/path


# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server   #####sftp的位置

 


2.设置不需要登录密码的ssh    
       利用ssh-keygen -t rsa,产生公钥和私钥。产生的默认目录在家目录的.ssh目录下面。如果有A,B两个主机。B为服务端,在A上运行上述命令后,后在当前用户的.ssh目录下生成两个文件:id_rsa id_rsa.pub 。将id_rsa.pub 拷贝到B机中,如果你想使用某个账号不需要密码进行登录的话,可以将id_rsa.pub内容添加到 ~/.ssh/authorized_keys  中。就可以实现无密码登录了。
3.问题
        无密码登录,在我试验的当中只能实现利用root无密码登录,但是不能实现某个普通账号登陆。如果有高人,请赐教。我的实验环境是RED HAT 5.2 内核2.6.18-92.el5
4.探讨
      1.如果我将.ssh/authorized_keys 这个参数更改为 任意的文件名结果会如何? .ssh/authorized_keys 。经过验证时完全可以实现的。     
      2.PermitEmptyPasswords no 选项更改为yes。按理说登录密码为空,就没有安全性了。但是我试验过后。甚至本机上的ssh登录软件都不可以登录。使用原先的ssh会直接提示Permission denied (publickey,password).     
      3.ssh 登录很慢的解决办法.修改 GSSAPIAuthentication 值为no
          
5.安全设置            
    主要是从sshd_config /etc/hosts.deny /etc/hosts.allow 和iptables三个方面去考虑。
    一 修改/etc/ssh/sshd_config
      1.sshd_config 设置不允许root用户登录。修改PermitRootLogin 值改为no .     
      2.不希望其他的群组远程进来,例如 ftp,pop3,sendmail。可以再其中添加一段     
        DenyGroups nossh;       
      3.不允许某个账号登陆。DenyUsers XX;     
    二 修改hosts.   
      1.allow 文件添加:sshd:IP1,IP2……:allow
      2.deny  文件添加:sshd:ALL
    三添加iptables规则:
  iptables -A INPUT -p tcp -i $INIF -s 192.168.5.0/24 --dport 22 -j ACCEPT
  iptables -A INPUT -p tcp -i $OUTIF -s 221.23.24.1 --dport 22 -j ACCEPT
  iptables -A INPUT -p tcp -i $OUTIF                --dport 22 -j DROP