三、SSHD配置文件及安全配置:
介绍下配置文件,以及需要安全调优的地方
注:参数前面有#,表示是默认值。 当然#号也表示注示。
/etc/ssh/sshd_config 配置文件
说明:这里以xiaolyu76作为服务器,xiaolyu77作为客户端。
#下面是系统默认的sshd_config的配置文件:
[root@xiaolyu76 ~]# cat /etc/ssh/sshd_config # $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm 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 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress :: # Disable legacy (protocol version 1) support in the server for new # installations. In future the default will change to require explicit # activation of protocol 1 Protocol 2 # HostKey for protocol version 1 #HostKey /etc/ssh/ssh_host_key # HostKeys for protocol version 2 #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_dsa_key # Lifetime and size of ephemeral version 1 server key #KeyRegenerationInterval 1h #ServerKeyBits 1024 # Logging # obsoletes QuietMode and FascistLogging #SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin yes #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 #RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys #AuthorizedKeysCommand none #AuthorizedKeysCommandRunAs nobody # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts #RhostsRSAAuthentication no # similar for protocol version 2 #HostbasedAuthentication no # 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 # 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 # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no #KerberoSUSEKuserok yes # GSSAPI options #GSSAPIAuthentication no GSSAPIAuthentication yes #GSSAPICleanupCredentials yes GSSAPICleanupCredentials yes #GSSAPIStrictAcceptorCheck yes #GSSAPIKeyExchange no # 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 and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to '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 LANGUAGE AcceptEnv XMODIFIERS #AllowAgentForwarding yes #AllowTcpForwarding yes #GatewayPorts no #X11Forwarding no X11Forwarding yes #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes #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:30:100 #PermitTunnel no #ChrootDirectory none # no default banner path #Banner none # override default of no subsystems Subsystem sftp /usr/libexec/openssh/sftp-server # Example of overriding settings on a per-user basis #Match User anoncvs # X11Forwarding no # AllowTcpForwarding no # ForceCommand cvs server
1. 端口:Port 22
设置sshd 监听端口号
# SSH 预设使用 22 这个port,也可以使用多个port,即重复使用 port 这个设定项目!
# 例如想要开放 sshd 端口为 22和 222 ,则多加一行内容为: Port 222 即可
# 然后重新启动 sshd 这样就好了。 建议修改 port number 为其它端口。防止别人暴力破解。
eg:修改sshd服务默认监听的端口为222
vim /etc/ssh/sshd_config

将Port 22修改为Port 222

重启sshd服务: service sshd restart
测试sshd服务的端口是否已经改变了: netstat -tlunp | grep sshd

修改完端口默认端口后,登录方法:
ssh -p 222 192.168.31.76 #-p后面跟的就是修改后的端口号。

2. 设置sshd 服务器绑定的本地IP 地址。
当本地有多个网卡时,就会出现多个ip地址,可以选择一个ip地址作为sshd服务器的ip地址。
当然了,0.0.0.0 表示本地所有的ip地址。

ListenAddress 是监听本地ip地址的,而不是远端ip地址的。
这个值可以写成本地某一个 ip地址或所有地址(0.0.0.0)。

#Protocol 2
选择的 SSH 协议版本,可以是 1 也可以是 2 ,CentOS 5.x 预设是仅支援 V2。出于安全考虑,设置为最新的协议版本
#HostKey /etc/ssh/ssh_host_key
设置包含计算机私人密匙的文件
#SyslogFacility AUTHPRIV
当有人使用 SSH 登入系统的时候,SSH 会记录信息,这个信息要记录的类型为AUTHPRIV。
在这个配置文件中,没有看到登录系统的默认日志存放路径,那么登录系统的默认日志存放在哪?
sshd服务日志存放在: /var/log/secure 。
ls /var/log/secure

例: 为什么sshd配置文件中没有指定日志,但日志却存放在了: /var/log/secure ?
vim /etc/rsyslog.conf

就是在这个/etc/rsyslog.conf定义了sshd服务日志的存放路径。
#LogLevel INFO
# 登录记录的等级!INFO级别以上。
3.下面是安全调优的重点:
#LoginGraceTime 2m
# 当使用者连上 SSH server 之后,会出现输入密码的画面
# 在多久时间内没有成功连上 SSH server 就强迫断线!若无单位则默认时间为秒!
可以根据实际情况来修改实际
# PermitRootLogin yes
#是否允许 root 登入!预设是允许的,但是建议设定成 no !
真实的生产环境服务器,是不允许root账号登陆的!!!
#PasswordAuthentication yes
# 密码验证当然是需要的!所以这里写 yes,也可以设置为no
#在真实的生产服务器上,根据不同安全级别要求,有的是设置不需要密码登陆的,通过认证的秘钥来登陆
# PermitEmptyPasswords no
# 若上面那一项如果设定为 yes 的话,这一项就最好设定为 no ,
# 这个项目在是否允许以空的密码登入!当然不许!
# PrintMotd yes
# 登入后是否显示出一些信息呢?例如上次登入的时间、地点等等,预设是 yes
# 亦即是打印出 /etc/motd 这个文档的内容。
例:给sshd服务添加一些警告信息
#服务器端:
[root@xiaolyu76 ~]# cat /etc/motd
[root@xiaolyu76 ~]# echo 'Warning ! From now on, all of your operations have been recorded!'> /etc/motd
#客户端:
[root@xiaolyu77 ~]# ssh -p222 xiaolyu76
root@xiaolyu76's password:
Last login: Mon Sep 19 02:08:16 2016 from xiaolyu77
Warning ! From now on, all of your operations have been recorded!


# PrintLastLog yes
# 显示上次登入的信息!预设也是 yes !实际生产上也是yes! 从上面可以明显的看到这个参数的效果。
# UseDNS yes
#一般来说,为了要判断客户端来源是正常合法的,因此会使用 DNS 去反查客户端的主机名
# 不过如果是在内网互连,这项目设定为 no 会让联机速度比较快。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-06/144905.htm

