Protocol
指定 sshd 支持的SSH协议的版本号。
'1'和'2'表示仅仅支持SSH-1和SSH-2协议。"2,1"表示同时支持SSH-1和SSH-2协议。
PubkeyAuthentication
是否允许公钥认证。仅可以用于SSH-2。默认值为"yes"。
RhostsRSAAuthentication
是否使用强可信主机认证(通过检查远程主机名和关联的用户名进行认证)。仅用于SSH-1。
这是通过在RSA认证成功后再检查 ~/.rhosts 或 /etc/hosts.equiv 进行认证的。
出于安全考虑,建议使用默认值"no"。
RSAAuthentication
是否允许使用纯 RSA 公钥认证。仅用于SSH-1。默认值是"yes"。
ServerKeyBits
指定临时服务器密钥的长度。仅用于SSH-1。默认值是 768(位)。最小值是 512 。
StrictModes
指定是否要求 sshd 在接受连接请求前对用户主目录和相关的配置文件进行宿主和权限检查。
强烈建议使用默认值"yes"来预防可能出现的低级错误。
Subsystem
配置一个外部子系统(例如,一个文件传输守护进程)。仅用于SSH-2协议。
值是一个子系统的名字和对应的命令行(含选项和参数)。比如"sft /bin/sftp-server"。
SyslogFacility
指定 sshd 将日志消息通过哪个日志子系统(facility)发送。有效值是:
DAEMON, USER, AUTH(默认), LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7
TCPKeepAlive
指定系统是否向客户端发送 TCP keepalive 消息。默认值是"yes"。
这种消息可以检测到死连接、连接不当关闭、客户端崩溃等异常。
可以设为"no"关闭这个特性。
UseDNS
指定 sshd是否应该对远程主机名进行反向解析,以检查此主机名是否与其IP地址真实对应。默认值为"yes"。
UseLogin
是否在交互式会话的登录过程中使用 login 。默认值是"no"。
如果开启此指令,那么 X11Forwarding 将会被禁止,因为 login不知道如何处理 xauthcookies 。
需要注意的是,login 是禁止用于远程执行命令的。
如果指定了 UsePrivilegeSeparation ,那么它将在认证完成后被禁用。
UsePrivilegeSeparation
是否让 sshd 通过创建非特权子进程处理接入请求的方法来进行权限分离。默认值是"yes"。
认证成功后,将以该认证用户的身份创建另一个子进程。
这样做的目的是为了防止通过有缺陷的子进程提升权限,从而使系统更加安全。
UsePAM
是否使用PAM登陆。
引用:http://www.linuxfly.org/post/176/
X11DisplayOffset
指定 sshdX11 转发的第一个可用的显示区(display)数字。默认值是 10 。
这个可以用于防止 sshd 占用了真实的 X11 服务器显示区,从而发生混淆。
X11Forwarding
是否允许进行 X11 转发。默认值是"no",设为"yes"表示允许。
如果允许X11转发并且sshd代理的显示区被配置为在含有通配符的地址(X11UseLocalhost)上监听。
那么将可能有额外的信息被泄漏。由于使用X11转发的可能带来的风险,此指令默认值为"no"。
需要注意的是,禁止X11转发并不能禁止用户转发X11通信,因为用户可以安装他们自己的转发器。
如果启用了 UseLogin ,那么X11转发将被自动禁止。
X11UseLocalhost
sshd 是否应当将X11转发服务器绑定到本地loopback地址。默认值是"yes"。
sshd 默认将转发服务器绑定到本地loopback地址并将 DISPLAY 环境变量的主机名部分设为"localhost"。
这可以防止远程主机连接到 proxy display 。不过某些老旧的X11客户端不能在此配置下正常工作。
为了兼容这些老旧的X11客户端,你可以设为"no"。
XAuthLocation
指定 xauth 程序的绝对路径。默认值是 /usr/X11R6/bin/xauth
时间格式
在 sshd命令行参数和配置文件中使用的时间值可以通过下面的格式指定:time[qualifier] 。
其中的 time 是一个正整数,而 qualifier 可以是下列单位之一:
<无> 秒
s | S 秒
m | M 分钟
h | H 小时
d | D 天
w | W 星期
可以通过指定多个数值来累加时间,比如:
1h30m 1 小时 30 分钟 (90 分钟)
一般的优化配置:
Protocol 2 #使用ssh2版本
X11Forwarding yes #允许窗口图形传输使用ssh加密
IgnoreRhosts yes#完全禁止SSHD使用.rhosts文件
RhostsAuthentication no #不设置使用基于rhosts的安全验证
RhostsRSAAuthentication no #不设置使用RSA算法的基于rhosts的安全验证
HostbasedAuthentication no #不允许基于主机白名单方式认证
PermitRootLogin no #不允许root登录
PermitEmptyPasswords no #不允许空密码
Banner /etc/motd #设置ssh登录时显示的banner
11、选择用 passwdqc进行策略设定方法
选择passwdqc,是因为网上找了一下,发现绝大多数的linux密码强度策略限制都只针对非root用户,而passwdqc可以限定到root用户。
passwdqc的主页如下:
配置文件的规则说明如下:
http://www.openwall.com/passwdqc/README.shtml
简单找一个我的规则解释一下:
(注:ubuntu的配置文件在/etc/pam.d/common-password redhat和centos的配置文件在/etc/pam.d/sys-auth), 我的规则是这样的:
- password required pam_passwdqc.so min=disabled,disabled,12,8,8 max=30 passphrase=3 match=4 similar=deny enforce=everyone disable_firstupper_lastdigit_check
规则解释如下:
1)只包含一种字符的密码,拒绝(大小写,数字,特殊字符分别为4种字符)
2)只包含两种字符的密码,拒绝
3)如果密码中被识别出了常用的单词,那么最小长度就必须为12位。常用单词的最小识别长度为3.(由passphrase=3制定)
4)包含三种字符的密码,最小长度为8位
- 包含四种字符的密码,最小长度为8位
6)如果发现本次修改的密码跟老密码有4个substring的长度类似,就认为是弱密码。(match=4 similar=deny)
- 这个策略对所有用户都生效。(enforce=everyone )
8)默认情况下,对于输入的密码,如果第一个字母是大写字母,或者最后一个字母为数字,则不会计入密码字符的种类。比如:Fuck1234,由于第一个F是大写字母,所以会被认为,该密码只有小写字母和数字两种字符。这个非常奇怪,我也不知道原因是什么。我们用disable_firstupper_lastdigit_check 禁止了这种识别。
12、hosts.equiv和.rhosts文件
涉及到 rsh.socket、 rlogin.socket、rexec.socket
1、 远程用户启动rlogin访问你的本地主机,此时做如下安全性检查:
-
本地rlogind在本地/etc/passwd文件中寻找远程用户名,没有则拒绝访问
-
/etc/passwd中存在远程用户名,rlogind在/etc/hosts.equiv寻找远程主机名,找到则允许访问。
-
/etc/hosts.equiv 无远程主机名,rlogind在**HOME/.rhosts** 寻找远程主机名,找到且该项后无用户名则允许访问,找到且该项后有用户名,若远程用户名位于其中,则允许访问。注意这里的HOME是与远程用户同名的本机用户的主目录。
-
若通过了/etc/passwd的检查,但没有通过/etc/hosts.equiv或者$HOME/.rhosts的检查,远程用户给出口令可以登录本机,但无法使用rcp、rsh等。反之则可以使用rcp或者rsh。
-
/etc/hosts.equiv中的**+意味着任意主机** ,此时与/etc/hosts无关。netterm下rlogin除root外的所有用户可以成功,solaris下简单的rlogin hostname同netterm,但是rlogin -l username hostname不成功。这个事实说明netterm下rlogin的时候,指定的参数实际上是远程主机的当前用户名。还说明hosts.equiv文件不支持rlogin -l username hostname,不支持root的rlogin。
值得注意的是**两个+号出现在HOME/.rhosts中是非常危险的** ,意味着任意主机任意用户都可以不用口令登录到你的用户上来。.rhosts中的第一个+意味着所有主机,与/etc/hosts文件无关。.rhosts中的第二个+意味着所有用户,与/etc/passwd文件无关。若只有一个+,netterm中的rlogin可以成功,但来自sco root下的rlogin -l jhli hostname不成功,来自sco jhli的rlogin -l jhli hostname和rlogin hostname都成功。说明当.rhosts文件中只有主机名时,不支持rlogin -l username hostname,这点和/etc/hosts.equiv一样。若+ root,则netterm因无法实现rlogin -l username hostname而失败(只能实现rlogin hostname),来自sco root下的rlogin -l jhli hostname成功。+ +则都成功。注意,HOME/.rhosts支持root的rlogin。
当这两个文件中没有使用+号代表所有主机时,与/etc/hosts文件有关。这两个文件中指定的hostname必须是/etc/hosts文件中出现的,而且必须是主机名,IP地址和别名无效。在/etc/hosts文件中别名位于第三列。做主机判断时与在远程主机上hostname命令所显示的东西无关,与远程主机上的/etc/hosts定义无关。如果出现与这里讲述不符合的,是因为缓冲没有得到刷新的缘故,可以通过ping相关名字得到检验。此外,若没有使用+号,rlogin localhost、rlogin 127.0.0.1的效果和rlogin 134.*.*.*、rlogin zhuzhou的效果不一样,前者的检查不会通过。当hosts.equiv中定义为localhost时,效果则反过来。
-
rlogin -l username hostname和rlogin hostname的检查有点细微的差别,首先在/etc/passwd的检查中是以-l的参数为准的,没有-l参数才使用远程主机的当前用户名,所谓当前用户名是考虑了su出来的用户。其次,HOME也是先以-l参数为准。第三,HOME/.rhosts文件中的用户名不是针对-l参数来的,而是针对远程主机的当前用户名来的,但是对于这个远程主机的当前用户名,并不要求出现在/etc/passwd文件中。这第6条尤其重要,许多不成功的$HOME/.rhost就是因为对第6条的不了解。
-
HOME/.rhosts文件的权限问题,chmod 0都没有影响,不过当时是处理root用户。对于一般用户,一定要保证.rhosts文件对于HOME的属主是可读的。一般来说,这个文件最好chmod 400。对于root,如果不希望某个用户建立自己的.rhosts或者想替该用户建立.rhosts后不允许该用户自己修改,可以通过chown root .rhosts然后chmod 444 .rhosts实现。除了root,如果HOME/.rhosts文件的属主和HOME的属主不一致,检查将失败。
-
$HOME/.rhosts文件中每行只跟一个用户名,若想多个指定,只好分多行指定用户名。
-
sco unix下$HOME/.rhosts中若用+代表主机,与其他Unix系统不同,这里的+没有任何特殊的意义,所以检查将失败。但是用户名仍然可以用+代表。并且sco unix下若root的.rhosts文件go+w,则检查失败。
2、 hosts.equiv文件的用途与格式
一、 hosts.equiv 文件的用途
/etc/hosts.equiv 和 $HOME/.rhosts 定义了哪些计算机和用户可以不用提供口令就在本地计算机上执行远程命令,如 rexec,rcp,rlogin 等等。这些不需要提供口令的计算机和用户称为受信任的。
当本地计算机收到执行远程命令的请求时,相应的远程命令服务进程,如 rlogind ,首先检查 /etc/hosts.equiv 来确认请求是否来自受信任的计算机和用户。如果这个文件不存在或者虽然存在但不包括相应的计算机和用户,服务进程就会去检查 $HOME/.rhosts 文件。
/etc/hosts.equiv 的权限必须设置为只有 root 能够写,建议权限为600。如果这个文件被设置为同组或其它用户可写,远程命令服务进程就会忽略它的存在。
如果远程命令是由 root 用户发起的,远程命令服务进程会忽略 /etc/hosts.equiv 文件的存在而去直接检查 $HOME/.rhosts 文件。
在指定受信任的计算机和用户时要非常小心,因为这有可能会造成安全漏洞。
二、 hosts.equiv 文件的格式
添加对计算机/用户的信任:
hostname:信任计算机 hostname 上的所有普通用户
hostname username:信任计算机 hostname 上的用户 username
+:信任所有计算机上的所有普通用户
禁止对计算机/用户的信任:
如果计算机名和用户都没有在 /etc/hosts.equiv 中被定义为受信任的,那么它们就是不受信任的。另外,您还可以用以下方法明确地禁止对计算机/用户的信任。
-hostname:不信任计算机 hostname 上的所有用户
hostname -username: 不信任计算机 hostname 上的用户 username
hosts.equiv 与 NIS :
在 /etc/hosts.equiv 中也可以指定是否信任 NIS 网络组(NETGROUP)。
+@netgroup:信任网络组 netgroup 中的所有计算机
-@netgroup:禁止信任网络组 netgroup 中的所有计算机
hostname +@netgroup:信任来自计算机 hostname 的所有网络组 netgroup 的成员用户的请求
hostname -@netgroup:禁止信任来自计算机 hostname 的所有网络组 netgroup 的成员用户的请求
/etc/hosts.equiv 中记录的顺序:
在 /etc/hosts.equiv 文件中,记录的顺序十分重要。远程命令服务进程在检查 /etc/hosts.equiv 文件时会在第一个匹配发现后返回,也就是说,下面这个例子中的禁止信任记录是不起作用的:
hostname
hostname -user1
计算机 hostname 上的用户 user1 将能够在不提供口令的情况下在本地计算机上执行远程命令。而下面这个例子能够提供期望中的结果:
hostname -user1
hostname
三、/etc/hosts.equiv示例
- 允许远程计算机 emerald 和 amethyst 上的所有用户在本地执行远程命令而无须提供口令:
emerald
amethyst
- 允许远程计算机 emerald 上的所有用户和 amethyst 上的用户 greygory 在本地执行远程命令而无须提供口令:
emerald
amethyst gregory
- 允许用户 peter 从任何远程计算机在本地执行远程命令而无须提供口令:
emerald
amethyst gregory
- peter
- 允许所有是 century 网络组成员的远程计算机上的所有用户在本地执行远程命令而无须提供口令:
emerald
amethyst gregory
- peter
+@century
- 允许所有在计算机 citrine 上又是 engineers 网络组成员的用户在本地执行远程命令而无须提供口令:
emerald
amethyst gregory
- peter
+@century
citrine +@engineers
- 允许所有是 servers 网络组成员的远程计算机上的所有属于 sysadmins 网络组的用户在本地执行远程命令而无须提供口令:
emerald
amethyst gregory
- peter
+@century
citrine +@engineers
+@server
+@sysadmins
13、历史及cache的信息核查
1、核查/etc/profile和$HOME/.profile,使用history命令核查。
#history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/usr/share/.history
if [ -z $USER_IP ]
then
USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]
then
mkdir -p $HISTDIR
chmod 777 $HISTDIR
fi
if [ ! -d HISTDIR/{LOGNAME} ]
then
mkdir -p HISTDIR/{LOGNAME}
chmod 300 HISTDIR/{LOGNAME}
fi
export HISTSIZE=4000
DT=`date +%Y%m%d_%H%M%S`
export HISTFILE="HISTDIR/{LOGNAME}/{USER_IP}.history.DT"
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
chmod 600 HISTDIR/{LOGNAME}/*.history* 2>/dev/null
2、释放cache(内存的缓存区)
核查/proc/sys/vm/drop_caches。
这个文件中记录了缓存释放的参数,默认值为0,也就是不释放缓存。他的值可以为0~3之间的任意数字,代表着不同的含义:
0 -- 不释放
1 -- 释放页缓存
2 -- 释放dentries和inodes
3 -- 释放所有缓存
可以使用crond定时来进行定时释放清理;
*/5 * * * * echo "3" > /proc/sys/vm/drop_chaches > /dev/null 2>&1
3、释放swap区
Swap一般不经常清理。
1、使用swapon --f核查挂在的swap区
2、用swapoff 设备名称,来卸载
3、用swapon 设备名称,挂载。
14、ip及ntp的配置核查
核查本机的ip地址:
ifconfig --a
核查ntp服务:
Systemctl status ntpd
/#因为centos7默认使用chronyd来进行时间同步服务,如果使用ntp服务需要关闭chronyd服务,否则使用chronyd。
- 使用timedatectl status命令核查服务情况:如
root@localhost etc\]# timedatectl status Local time: Sun 2021-11-14 07:26:03 PST Universal time: Sun 2021-11-14 15:26:03 UTC RTC time: Sun 2021-11-14 15:26:01 Time zone: America/Los_Angeles (PST, -0800) **NTP enabled: no** **NTP synchronized: no** RTC in local TZ: no DST active: no Last DST change: DST ended at Sun 2021-11-07 01:59:59 PDT Sun 2021-11-07 01:00:00 PST Next DST change: DST begins (the clock jumps one hour forward) at Sun 2022-03-13 01:59:59 PST Sun 2022-03-13 03:00:00 PDT 1. chronyd的核查: 服务:systemctl status chronyd ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:chronyd(8) man:chrony.conf(5) ##### ******1、配置文件:/etc/chrony.conf****** # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). **server 0.centos.pool.ntp.org iburst** **server 1.centos.pool.ntp.org iburst** **server 2.centos.pool.ntp.org iburst** **server 3.centos.pool.ntp.org iburst** # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp \* # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 **# Allow NTP client access from local network.** **#allow 192.168.0.0/16** # Serve time even if not synchronized to a time source. **#local stratum 10** # Specify file containing keys for NTP authentication. #keyfile /etc/chrony.keys **# Specify directory for log files.** **logdir /var/log/chrony** # Select which information is logged. #log measurements statistics tracking **在其它客户端的安装与配置** (1)同样在客户端安装# yum -y install chrony Package chrony-3.1-2.el7.centos.x86_64 already installed and latest version (2)编辑 /etc/chrony.conf 文件 \[root@controller1 \~\]# cat /etc/chrony.conf 注释掉下面四个,加增一个内部时间同步服务器地址 #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst **server 192.168.128.101 iburst** # systemctl enable chronyd.service # systemctl start chronyd.service 验证时间同步: \[root@mq1 \~\]# chronyc sources 210 Number of sources = 1 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== \^? 192.168.128.101 0 6 0 - +0ns\[ +0ns\] +/- 0ns \[root@mq1 \~\]# ##### ******2、配置计划:****** **\[root@CentOS7 \~\]# crontab -e //编辑crontab文件** **\[root@CentOS7 \~\]# crontab -l //查看任务计划** **#synchronization time //每隔五分钟进行同步一次** **\*/5 \* \* \* \* /usr/sbin/ntpdate 192.168.10.8 \> /dev/null 2\>\&1** ##### ******3、配置文件ntp.conf****** ###### ******1)服务端配置:****** # For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. **#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap** # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). **server 0.centos.pool.ntp.org iburst** **server 1.centos.pool.ntp.org iburst** **server 2.centos.pool.ntp.org iburst** **server 3.centos.pool.ntp.org iburst** #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats # Disable the monitoring facility to prevent amplification attacks using ntpdc # monlist command when default restrict does not include the noquery flag. See # CVE-2013-5211 for more details. # Note: Monitoring will not be disabled with the limited restriction flag. disable monitor ###### ******2)客户端配置:****** /etc/ntp.conf driftfile /var/lib/ntp/drift restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1 restrict 172.22.10.92 nomodify notrap nopeer noquery //当前节点IP地址 restrict 172.22.10.0 mask 255.255.255.0 nomodify notrap //集群所在网段的网段(Gateway),子网掩码(Genmask) server 172.22.10.6 iburst //NTP服务器IP地址,可以注释掉别的server或者在需要配置的ntp服务器后添加perfer--优先,提高优先级,如:server 172.22.10.6 perfer iburst fudge 172.22.10.6 stratum 10 //NTP服务器层级设置 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor ###### ******3)**** ****开机启动、设置防火墙****** **firewall-cmd --zone=public --add-port=123/udp --permanent** 设置ntp防火墙开放策略 firewall-cmd --reload 重新加载防火墙 ###### ******4)开启ntp服务,设置ntp自启动****** **systemctl status chronyd 查看chronyd状态** **systemctl disable chronyd.service 禁止chronyd开机启动** **systemctl start ntpd 启动ntp服务** **systemctl enable ntpd 容许ntp服务开机启动** ###### ******5)**** ****查看ntp服务状态****** ****1、查看ntp服务状态**** **systemctl status ntpd** ****2、查看ntp链接状态**** **ntpq** **-p** remote:本机和上层ntp的ip或主机名,"+"表示优先,"\*"表示次优先 refid:参考上一层ntp主机地址 st:stratum阶层 when:多少秒前曾经同步过时间 poll:下次更新在多少秒后 reach:已经向上层ntp服务器要求更新的次数 delay:网络延迟 offset:时间补偿 jitter:系统时间与bios时间差# ****3、查看ntpd进程的状态**** **watch** **"ntpq -p"** 按 Ctrl+C 停止查看进程 第一列中的字符指示源的质量。星号 ( \* ) 表示该源是当前引用。 remote:列出源的 IP 地址或主机名。 when:指出从轮询源开始已过去的时间(秒)。 poll:指出轮询间隔时间。该值会根据本地时钟的精度相应增加。 reach:是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。 offset:是源时钟与本地时钟的时间差(毫秒)。