Rsyslog 是一个 syslogd 的多线程增强版。它提供高性能、极好的安全功能和模块化设计。虽然它基于常规的 syslogd,但 rsyslog 已经演变成了一个强大的工具,可用于:
准备
安装Rsyslog
默认情况下,Rsyslog安装在Ubuntu 18.04服务器上。如果没有安装,您可以通过运行以下命令来安装它:
linuxprobe@ubuntu-18-04-lts:~$ apt-get install rsyslog -y
在安装Rsyslog之后,您可以使用以下命令检查Rsyslog的版本:
linuxprobe@ubuntu-18-04-lts:~$ 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的状态:
linuxprobe@ubuntu-18-04-lts:~$ systemctl status rsyslog
? rsyslog.service - System Logging Service
Loaded: loaded (/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
Active: active (running) since Thur 2020-01-16 11:20:32 CST; 1min 31s ago
Docs: man:rsyslogd(8)
Main PID: 724 (rsyslogd)
Tasks: 4 (limit: 1114)
CGroup: /system.slice/rsyslog.service
??724 /usr/sbin/rsyslogd -n
Jan 16 04:28:53 ubuntu-18-04-lts systemd[1]: Starting System Logging Service...
Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: imuxsock: Acquired UNIX socket '/run/systemd/journal/syslog' (fd 3) from systemd. [v8.32.0]
Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: rsyslogd's groupid changed to 106
Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: rsyslogd's userid changed to 102
Jan 16 04:28:54 ubuntu-18-04-lts rsyslogd[724]: [origin software="rsyslogd" swVersion="8.32.0" x-pid="724" x-info="http://www.rsyslog.com"] start
Jan 16 04:28:55 ubuntu-18-04-lts systemd[1]: Started System Logging Service.
配置Rsyslog服务端
linuxprobe@ubuntu-18-04-lts:~$ vim /etc/rsyslog.conf
取消这几行前面的注释,同事使用UDP和TCP协议的514端口
$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如何存储传入的syslog消息。在GLOBAL DIRECTIVES部分之前添加以下几行:
$template remote-incoming-logs,"/var/log/%HOSTNAME%/%PROGRAMNAME%.log"
*.* ?remote-incoming-logs
用以下命令检查Rsyslog配置信息是否有语法错误:
linuxprobe@ubuntu-18-04-lts:~$ 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:
linuxprobe@ubuntu-18-04-lts:~$ systemctl restart rsyslog
验证Rsyslog正在使用以下命令监听TCP/UDP:
linuxprobe@ubuntu-18-04-lts:~$ 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客户端来向远程服务端发送系统日志消息。登录客户端,打开/etc/rsyslog.conf添加如下信息:
linuxprobe@ubuntu-18-04-lts:~$ vim /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:
linuxprobe@ubuntu-18-04-lts:~$ systemtcl restart rsyslog
查看日志
此时,Rsyslog客户端被配置为将它们的日志发送到Rsyslog服务端。 现在,登录到Rsyslog服务器并检查/var/log目录。看到客户端机器的主机名,包括几个日志文件:
linuxprobe@ubuntu-18-04-lts:~$ ls /var/log/rsyslog-client/
CRON.log kernel.log rsyslogd-2039.log rsyslogd.log sudo.log wpa_supplicant.log
总结
在上面的文章中,我们学习了如何在Ubuntu 18.04服务器上安装和配置RysLogServer。我们还学习了如何配置rsyslog客户端 向rsyslog服务端发送日志。
以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !