建立 rsyslog 日志服务器

article/2025/10/5 22:15:25

文章目录

  • 1. rsyslog 介绍
  • 2. 实验目的
  • 3. 实验环境
  • 4. 配置服务端
  • 5. 配置客户端
  • 6. 在服务端验证效果


1. rsyslog 介绍

  rsyslog 是一个快速处理收集系统日志的开源程序,提供了高性能、安全功能和模块化设计。rsyslog 是 syslog 的升级版,它将多种来源输入输出转换结果到目的地, rsyslog 被广泛用于 Linux 系统以通过 TCP/UDP 协议转发或接收日志消息。
在这里插入图片描述

在这里插入图片描述

  rsyslog 守护进程可以被配置成两种环境,一种是配置成日志收集服务器,rsyslog 进程可以从网络中收集其它主机上的日志数据,这些主机会将日志配置为发送到另外的远程服务器。rsyslog 的另外一个用法,就是可以配置为客户端,用来过滤和发送内部日志消息到本地文件夹(如 /var/log)或一台可以路由到的远程 rsyslog 服务器上。

2. 实验目的

  实现 Client 主机通过 rsyslog 发送自身的系统日志到 Rsyslog Server 服务器,服务器端将该主机系统日志存放到一个指定的目录里面,进行按 IP 和日志简单分类存储。

3. 实验环境

  • 服务端和客户端系统都为 Centos7.7
  • 服务端 IP:10.0.0.120  客户端 IP:10.0.0.100
  • 服务端和客户端关闭防火墙和 selinux
systemctl stop firewalld
setenforce 0
  • 服务端和客户端都安装 rsyslog 服务
yum -y install rsyslog  #无网络自行配置 yum 源

4. 配置服务端

vim /etc/rsyslog.conf  #修改rsyslog配置文件,标蓝的即为需要的内容,标红的为解释说明# rsyslog configuration file# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html#### MODULES ##### The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark # provides --MARK-- message capability# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514#### GLOBAL DIRECTIVES ##### Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog
$AllowedSender udp, 10.0.0.0/24
#收集的IP网段# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$template Remote,"/opt/n9e/rsyslog/logs/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%-%$HOUR%.log"   #定义模板,接受日志文件路径,区分了不同主机的日志,日志目录自行指定
:fromhost-ip, !isequal, "127.0.0.1" ?Remote  # 过滤服务端本机的日志# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on# File to store the position in the journal
$IMJournalStateFile imjournal.state#### RULES ####
# 添加创建目录的注释
$CreateDirs on
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages# The authpriv file has restricted access.
authpriv.* /var/log/secure# Log all the mail messages in one place.
mail.* -/var/log/maillog# Log cron stuff
cron.* /var/log/cron# Everybody gets emergency messages
*.emerg :omusrmsg:*# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler# Save boot messages also to boot.log
local7.* /var/log/boot.log# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList # run asynchronously
#$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
# *.* @@192.168.44.212:514
# ### end of the forwarding rule ###systemctl restart rsyslog  #重启rsyslog服务

5. 配置客户端

vim /etc/rsyslog.conf  #修改rsyslog配置文件,标蓝的即为需要的内容,标红的为解释说明# rsyslog configuration file# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html#### MODULES ##### The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark # provides --MARK-- message capability# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514#### GLOBAL DIRECTIVES ##### Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on# File to store the position in the journal
$IMJournalStateFile imjournal.state#### RULES ##### Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages# The authpriv file has restricted access.
authpriv.* /var/log/secure# Log all the mail messages in one place.
mail.* -/var/log/maillog# Log cron stuff
cron.* /var/log/cron# Everybody gets emergency messages
*.emerg :omusrmsg:*# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler# Save boot messages also to boot.log
local7.* /var/log/boot.log# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
$ActionQueueFileName fwdRule1 # unique name prefix for spool files
$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
$ActionQueueType LinkedList # run asynchronously
$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###
*.* @10.0.0.120  #指定服务端IPsystemctl restart rsyslog  #重启rsyslog服务

6. 在服务端验证效果

切换到服务端存放日志文件的路径,可以看到已经生成了日志,rsyslog 日志服务配置成功。
在这里插入图片描述


配置文件详解:
https://www.cnblogs.com/shu-sheng/p/13275474.html


http://chatgpt.dhexx.cn/article/kUX6OtJo.shtml

相关文章

rsyslog配置

rsyslog配置文件详解: #### MODULES #### #定义日志的模块。 $ModLoad imuxsock #imuxsock为模块名,支持本地系统日志的模块。 $ModLoad imjournal #imjournal为模块名,支持对系统日志的访问。 #$ModLo…

syslog 和 rsyslog

1. 介绍 rsyslog可以简单的理解为syslog的超集,在老版本的Linux系统中,Red Hat Enterprise Linux 3/4/5默认是使用的syslog作为系统的日志工具,从RHEL 6 开始系统默认使用了rsyslog。 其特性包括: 支持输出日志到各种数据库&…

rsyslog日志服务详解

rsyslog日志服务详解 原文出处:http://blog.51cto.com/6638225/1862902 内容: 1、rsyslog日志服务简介 2、rsyslog的配置详解 3、实现日志服务器收集日志及last、lastb、dmseg命令的使用 4、实现日志存储在mysql中 一、rsyslog日志服务简介 ​ 日…

【Linux】rsyslog日志服务(配置,测试、日志转储)

一、rsyslog简介 Rsyslog的全称是 rocket-fast system for log ,可用于接受来自各种来源的输入,转换 它们,并将结果输出到不同的目的地。 它提供了高性能、强大的安全功能和模块化设计。虽然rsyslog最初是一个常规的系 统日志,但它已经发展…

Linux原生日志系统Rsyslog详解

一、概述 Rsyslog 是一个 syslogd 的多线程增强版,依然基于Syslog协议(linux6之前默认使用syslog程序,centos6用rsyslog所取代)完成系统日志的处理转发,官方形容它是一个极速(如火箭般快速)的日…

N皇后问题-回溯法-C语言

关于对N皇后问题和回溯法的理解 个人非常推荐下面这个视频:算法与数据结构,回溯法求解八皇后,最经典的递归问题_哔哩哔哩_bilibili八皇后问题是计算机科学中最为经典的问题之一,该问题由国际西洋棋棋手马克斯贝瑟尔于1848年提出。…

Java——N皇后问题

题目链接 leetcode在线oj题——N皇后 题目描述 按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。 n 皇后问题 研究的是如何将 n 个皇后放置在 nn 的棋盘上,并且使皇后彼此之间不能相互攻击。 给你一个整数 n &#xff…

N 皇后问题

n 皇后问题 研究的是如何将 n 个皇后放置在 nn 的棋盘上,并且使皇后彼此之间不能相互攻击。 给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。 每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 Q 和 . 分别代表了皇后和…

N皇后问题(C++)

n−皇后问题是指将 n 个皇后放在 nn 的国际象棋棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行、同一列或同一斜线上。 现在给定整数 n,请你输出所有的满足条件的棋子摆法。 输入格式 共一行,包含整数 n。 输出…

回溯法求解n皇后问题

一、实验目的 1.掌握能用回溯法求解的问题应满足的条件; 2.加深对回溯法算法设计方法的理解与应用; 3.锻炼学生对程序跟踪调试能力; 4.通过本次实验的练习培养学生应用所学知识解决实际问题的能…

N皇后问题(java)

n皇后问题是一个以国际象棋为背景的问题:在nn的国际象棋棋盘上放置n个皇后,使得任何一个皇后都无法直接吃掉其他的皇后,即任意两个皇后都不能处于同一条横行、纵行或斜线上。 我们通过回溯的方法将所有可能的情况遍历一遍 假设现在有一个4*4…

N皇后问题(Python实现)

n 皇后问题研究的是如何将 n 个皇后放置在 nn 的棋盘上,并且使皇后彼此之间不能相互攻击。 也就是说:存在一个N*N的棋盘,要放N个棋子,每个棋子不同行,不同列,不同(正反)对角线&…

数据结构之N皇后问题(C语言)

一、N皇后问题的概念 n 皇后问题,研究的是如何将 n 个皇后放置在 nn 的棋盘上,并且使皇后彼此之间不能相互攻击。 一个皇后可以向水平、垂直以及向斜对角方向移动,如果一个皇后出现在另一个皇后的同一行,同一列或者斜对角&#x…

n皇后问题 C++

先上代码&#xff01; #include<iostream> using namespace std;const int N 20;int n; bool row[N],col[N], dg[N], udg[N]; char g[N][N];void dfs(int x, int y, int z) {if (y n) y 0, x; //判断y是否已经抵达边界&#xff0c;抵达后&#xff0c;x1进行下一行if …

回溯法-N皇后问题

一、N皇后问题 n皇后问题&#xff1a;要求在一个nn的棋盘上放置n个皇后&#xff0c;使得任意两个皇后不在同一行或同一列或同一斜线上。 二、回溯法 回溯法是一类非常重要的算法设计方法&#xff0c;有“通用解题法”之称。 回溯法&#xff08;探索与回溯法&#xff09;&am…

(新手向)N皇后问题详解(DFS算法)

非常经典的一道题&#xff1a; N皇后问题&#xff1a; 国际象棋中皇后的势力范围覆盖其所在的行、列以及两条对角线&#xff0c;现在考察如下问题&#xff1a;如何在n x n的棋盘上放置n个皇后&#xff0c;使得她们彼此互不攻击 。 免去麻烦我们这里假定n不是很大。。 &#…

n皇后问题-c语言实现

n皇后问题描述为:在一个nxn的棋盘上摆放n个皇后&#xff0c;要求任意两个皇后不能冲突&#xff0c;即任意两个皇后不在同一行、同一列或者同一斜线上。 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4 4,1 4,2 4,3 4,4 上面是4皇后摆放方案&#xff0c;只有…

n皇后问题合集

八皇后问题是dfs的经典问题&#xff0c;目前遇到过的类型大致有这么几种&#xff1a; 1.经典n皇后问题 题目描述 n−皇后问题是指将 n 个皇后放在 nn 的国际象棋棋盘上&#xff0c;使得皇后不能相互攻击到&#xff0c;即任意两个皇后都不能处于同一行、同一列或同一斜线上。 …

N皇后问题(c语言实现)

问题描述&#xff1a; 有一个n*n的棋盘&#xff0c;在这个棋盘中放n个皇后&#xff0c;使得这n个皇后&#xff0c;任意两个皇后不在同一行&#xff0c;同一列&#xff0c;同一条对角线。例如&#xff0c;当n等于4时&#xff0c;有两种摆法。 输入只有一个整数n。 思路 如果…

N皇后问题

问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 nn 的棋盘上&#xff0c;并且使皇后彼此之间不能相互攻击。 上图为 8 皇后问题的一种解法。 给定一个整数 n&#xff0c;返回所有不同的 n 皇后问题的解决方案。 每一种解法包含一个明确的 n 皇后问题的棋子放置方案&#…