如何在 Ubuntu 18.04 LTS 上设置 Rsyslog 服务器
在此页
- 先决条件
- 安装 Rsyslog
- 配置 Rsyslog 服务器
- 配置 Rsyslog 客户端
- 查看客户端日志
- 结论
日志对于分析和解决与 Linux 中的系统和应用程序相关的任何问题非常有用。默认情况下,所有日志文件都位于基于 Linux 的操作系统中的 /var/log 目录中。有几种类型的日志文件,包括 cron、内核、用户、安全,这些文件中的大部分由 Rsyslog 服务控制。
Rsyslog 是一个强大而安全的日志处理系统。 Rsyslog 服务器通过网络从多个物理或虚拟化服务器接收日志,并监控不同服务的健康状况。使用 Rsyslog 服务器,您可以从集中位置监控其他服务器、网络设备和远程应用程序的日志。
在本教程中,我们将解释如何在 Ubuntu 18.04 服务器上配置 Rsyslog 服务器。
先决条件
- 两台运行 Ubuntu 18.04 的服务器。
- 静态 IP 地址 192.168.0.101 在 Rsyslog 服务器机器上配置,192.168.0.102 在 Rsyslog 客户端机器上配置。
- 在两台服务器上都配置了根密码。
安装 Rsyslog
默认情况下,Rsyslog 安装在 Ubuntu 18.04 服务器中。如果没有安装,您可以通过运行以下命令来安装它:
apt-get install rsyslog -y
安装Rsyslog后,可以通过以下命令查看Rsyslog的版本:
rsyslogd -v
您应该得到以下输出:
rsyslogd 8.32.0, compiled with:
PLATFORM: x86_64-pc-linux-gnu
PLATFORM (lsb_release -d):
FEATURE_REGEXP: Yes
GSSAPI Kerberos 5 support: Yes
FEATURE_DEBUG (debug build, slow code): No
32bit Atomic operations supported: Yes
64bit Atomic operations supported: Yes
memory allocator: system default
Runtime Instrumentation (slow code): No
uuid support: Yes
systemd support: Yes
Number of Bits in RainerScript integers: 64
See http://www.rsyslog.com for more information.
您还可以使用以下命令检查 Rsyslog 的状态:
systemctl status rsyslog
您应该看到以下输出:
? rsyslog.service - System Logging Service
Loaded: loaded (/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-10-22 04:28:55 UTC; 1min 31s ago
Docs: man:rsyslogd(8)
http://www.rsyslog.com/doc/
Main PID: 724 (rsyslogd)
Tasks: 4 (limit: 1114)
CGroup: /system.slice/rsyslog.service
??724 /usr/sbin/rsyslogd -n
Oct 22 04:28:53 ubuntu1804 systemd[1]: Starting System Logging Service...
Oct 22 04:28:54 ubuntu1804 rsyslogd[724]: imuxsock: Acquired UNIX socket '/run/systemd/journal/syslog' (fd 3) from systemd. [v8.32.0]
Oct 22 04:28:54 ubuntu1804 rsyslogd[724]: rsyslogd's groupid changed to 106
Oct 22 04:28:54 ubuntu1804 rsyslogd[724]: rsyslogd's userid changed to 102
Oct 22 04:28:54 ubuntu1804 rsyslogd[724]: [origin software="rsyslogd" swVersion="8.32.0" x-pid="724" x-info="http://www.rsyslog.com"] start
Oct 22 04:28:55 ubuntu1804 systemd[1]: Started System Logging Service.
配置 Rsyslog 服务器
Rsyslog 现已安装并运行。接下来,您需要将其配置为在服务器模式下运行。您可以通过编辑文件 /etc/rsyslog.conf 来完成。
nano /etc/rsyslog.conf
首先,您需要定义 UDP 或 TCP 或两者的协议。
要同时使用 UDP 和 TCP 连接,请搜索并取消注释以下行:
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
接下来,定义特定的子网、IP 或域来限制访问,如下所示:
$AllowedSender TCP, 127.0.0.1, 192.168.0.0/24, *.example.com
$AllowedSender UDP, 127.0.0.1, 192.168.0.0/24, *.example.com
接下来,您需要创建一个模板来告诉 Rsyslog 服务器如何存储传入的系统日志消息。在 GLOBAL DIRECTIVES 部分之前添加以下行:
$template remote-incoming-logs, "/var/log/%HOSTNAME%/%PROGRAMNAME%.log"
*.* ?remote-incoming-logs
完成后保存并关闭文件。然后,使用以下命令检查 Rsyslog 配置是否存在任何语法错误:
rsyslogd -f /etc/rsyslog.conf -N1
您应该看到以下输出:
rsyslogd: version 8.32.0, config validation run (level 1), master config /etc/rsyslog.conf
rsyslogd: End of config validation run. Bye.
最后,使用以下命令重启 Rsyslog 服务:
systemctl restart rsyslog
现在,使用以下命令验证 Rsyslog 是否正在侦听 TCP/UDP:
netstat -4altunp | grep 514
您应该得到以下输出:
tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 1332/rsyslogd
udp 0 0 0.0.0.0:514 0.0.0.0:* 1332/rsyslogd
配置 Rsyslog 客户端
Rsyslog 服务器已安装并配置为从远程主机接收日志。
现在,您需要配置 Rsyslog 客户端以将系统日志消息发送到远程 Rsyslog 服务器。
登录Client机器,打开Rsyslog配置文件,如下图:
nano /etc/rsyslog.conf
在文件末尾添加以下行:
##Enable sending of logs over UDP add the following line:
*.* @192.168.0.101:514
##Enable sending of logs over TCP add the following line:
*.* @@192.168.0.101:514
##Set disk queue when rsyslog server will be down:
$ActionQueueFileName queue
$ActionQueueMaxDiskSpace 1g
$ActionQueueSaveOnShutdown on
$ActionQueueType LinkedList
$ActionResumeRetryCount -1
保存并关闭文件。然后,重新启动 Rsyslog 服务器以应用配置更改:
systemtcl restart rsyslog
查看客户端日志
此时,Rsyslog 客户端已配置为将其日志发送到 Rsyslog 服务器。
现在,登录到 Rsyslog 服务器并检查 /var/log 目录。您应该会看到带有您的客户端机器主机名的条目,包括几个日志文件:
ls /var/log/rsyslog-client/
输出:
CRON.log kernel.log rsyslogd-2039.log rsyslogd.log sudo.log wpa_supplicant.log
结论
在上面的文章中,我们学习了如何在 Ubuntu 18.04 服务器上安装和配置 Rsyslog 服务器。我们还学习了如何配置 Rsyslog 客户端以将日志发送到 Rsyslog 服务器。如果您有任何问题,请随时问我。