9.4. 限制控制台消息

9.4.1. 限制来自系统日志的控制台消息

持续不断地生成控制台消息很容易压垮 9600 bps 的链接。

尽管在控制台上显示所有 syslog 消息看起来是个好主意,但这实际上为非特权用户提供了一种简单的方法来拒绝有效使用远程控制台。

将系统日志消息配置为控制台的最小值。查看/etc/syslog.conf查找以以下内容结尾的行/dev/console.

考虑将所有日志消息发送到另一台机器进行记录和分析。图 9-2 显示了标准的/etc/syslog.confRed Hat Linux 7.2 修改而来,用于将日志消息记录到日志服务器。每一行syslog.conf已被重复,以便将消息的副本发送到日志服务器。日志服务器具有 DNS 别名 loghost.example.edu.au; 使用 DNS 别名允许移动日志服务器,而无需更新所有远程机器的配置。本地日志消息副本不再是确定系统故障原因的唯一手段,因此我们可以通过禁用同步文件写入来获得一些性能优势,尽管这会增加文件系统不一致的几率(对于不进行日志记录的文件系统来说,这是一个问题)。将一个-放在文件名之前会禁用同步文件写入。

图 9-2。/etc/syslog.conf修改为将日志消息复制到日志服务器

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none  @loghost.example.edu.au
*.info;mail.none;authpriv.none;cron.none  -/var/log/messages

# The authpriv file has restricted access.
authpriv.*                                @loghost.example.edu.au
authpriv.*                                /var/log/secure

# Log all the mail messages in one place.
mail.*                                    @loghost.example.edu.au
mail.*                                    -/var/log/maillog

# Log cron stuff
cron.*                                    @loghost.example.edu.au
cron.*                                    -/var/log/cron

# Everybody gets emergency messages
*.emerg                                   @loghost.example.edu.au
*.emerg                                   *

# Save news errors of level crit and higher in a special file.
uucp,news.crit                            @loghost.example.edu.au
uucp,news.crit                            -/var/log/spooler

# Save boot messages also to boot.log
local7.*                                  @loghost.example.edu.au
local7.*                                  -/var/log/boot.log

使用标准的配置日志服务器/etc/syslog.conf配置为允许接收远程 syslog 消息。 Red Hat Linux 的此配置如 图 9-3 所示。 除了配置系统日志守护进程之外,还可以通过配置 IP Tables 来限制 syslog 消息的来源,从而防止拒绝服务攻击;并通过检查 nscd 是否正在运行以缓存反向 DNS 查找来提高性能。

图 9-3. 通过设置以下选项来允许远程日志消息/etc/sysconfig/syslog

# Red Hat Linux default value, does not write timer mark messages
SYSLOGD_OPTIONS="-m 0"
# Add option to accept remote syslog messages
SYSLOGD_OPTIONS="${SYSLOGD_OPTIONS} -r"

图 9-4. 将 syslog 消息限制为 remote.example.edu.au

 bash# chkconfig iptables on
 bash# /etc/init.d/iptables restart
# Allow all IP traffic from this machine
 bash# iptables --append INPUT --source 127.0.0.0/8 --in-interface lo --jump ACCEPT
# Perhaps filter other traffic
…
# Accept syslog messages from remote.example.edu.au
 bash# iptables --append INPUT --source remote.example.edu.au --protocol udp --destination-port syslog -j ACCEPT
# Silently drop unexpected syslog messages
 bash# iptables --append INPUT --protocol udp --destination-port syslog -j DROP
# Save the running configuration
 bash# /etc/init.d/iptables save

图 9-5. 使用 nscd 缓存反向 DNS 查找

bash# chkconfig nscd on
bash# /etc/init.d/nscd restart

9.4.2. 限制广播消息到控制台

登录到串行控制台的用户不应接受广播消息。 将新文件添加到/etc/profile.d去做这个。 图 9-6 显示了 Bourne shell 使用的文件。

图 9-6. 限制向控制台用户发送消息

#
# Do we have files referred to?
if [ -x /usr/bin/mesg -a -x /usr/bin/tty ]
then
  # Are we on serial console?
  if [ `/usr/bin/tty` = /dev/ttyS0 ]
  then
    # Do not accept broadcast messages
    /usr/bin/mesg n
  fi
fi

由于此文件经常运行,因此我们使用了 图 9-6 的更快但可读性较差的版本,如 图 9-7 所示。

图 9-7. 限制向控制台用户发送消息,/etc/profile.d/mesg.sh

#
# /etc/profile.d/mesg.sh -- prevent people hassling the serial console user
[ -x /usr/bin/mesg -a -x /usr/bin/tty -a `/usr/bin/tty` = /dev/ttyS0 ] && /usr/bin/mesg n

我们还需要一个 C shell 版本,如 图 9-8 所示。

图 9-8. 限制向控制台用户发送消息,/etc/profile.d/mesg.csh

#
# /etc/profile.d/mesg.csh -- prevent people hassling the serial console user
if (-X mesg && -X tty && `tty` == /dev/ttyS0) then
  mesg n
endif

虽然mesg.shmesg.csh由父 shell 包含而不是执行,但文件需要设置执行权限。 图 9-9 中的过程安装文件并设置权限。

图 9-9. 将文件安装到/etc/profile.d

bash# cp mesg.*sh /etc/profile.d/
bash# chown root:root /etc/profile.d/mesg.*sh
bash# chmod u=rwx,g=rx,o=rx /etc/profile.d/mesg.*sh