参考链接:https://mp.weixin.qq.com/s/AJ6Wa0fH1RC40qfzLqyqpg
# 【网安合规】Rsyslog 开源日志服务器 - 快速收集企业网络日志,合规利器!
## 安装部署
### rsyslogd 服务是否安装且启动了
```shell
[root@localhost opt]# rsyslogd -v
rsyslogd 8.24.0-34.el7, compiled with:
PLATFORM: x86_64-redhat-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
Number of Bits in RainerScript integers: 64
See http://www.rsyslog.com for more information.
[root@localhost opt]# ps aux | grep "rsyslog" | grep -v "grep"
root 6862 0.0 0.0 216408 4672 ? Ssl 2月20 1:39 /usr/sbin/rsyslogd -n
[root@localhost ~]# systemctl list-unit-files rsyslog.service
UNIT FILE STATE
rsyslog.service enabled
1 unit files listed.
```
### Redhat、Debian、Alpine系安装
```shell
# Redhat 系
sudo yum install rsyslog
# 或者 CentOS8 使用 dnf 软件包安装工具进行安装
sudo dnf install rsyslog
# Debian 系
sudo apt install rsyslog
# Alpine 系
sudo apk add rsyslog
```
### 安装的最新的 rsyslog 版本
```shell
# 要在RHEL/CENTOS上安装rsyslog,只需以root用户身份从命令行执行以下命令:
wget http://rpms.adiscon.com/v8-stable/rsyslog.repo -O /etc/yum.repos.d/rsyslog.repo # for CentOS 7,8,9
wget http://rpms.adiscon.com/v8-stable/rsyslog-rhel.repo -O /etc/yum.repos.d/rsyslog-rhel.repo # for RHEL 7,8,9
cat rsyslog.repo
[rsyslog_v8]
name=Adiscon CentOS-$releasever - local packages for $basearch
baseurl=http://rpms.adiscon.com/v8-stable/epel-$releasever/$basearch
enabled=1
gpgcheck=0
gpgkey=https://rpms.adiscon.com/RPM-GPG-KEY-Adiscon
protect=1
# 安装
yum update && yum install rsyslog
# 更新
yum update && yum update rsyslog
# 启用
sudo systemctl start rsyslog
sudo systemctl enable rsyslog
```
#### 1.设置日志服务器(服务端):在任意一台安装了rsyslog 主机上述设置其成为日志服务器,需要为其配置如下指令,即可启用接收远程日志。
```shell
# 10.30.17.200 - Server
vim /etc/rsyslog.conf
# 例如,CerntOS 6/7 旧版本网络日志功能配置
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
# 例如,CerntOS 8 新版本网络日志功能配置, 只是语法上有所不同,不过官方还是推荐此种 V8 配置风格。
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")
```
#### 2.转发日志到远程服务器(客户端):在rsyslog配置文件中添加以下行,将日志转发到指定的远程服务器(请将remote-host替换为您的远程日志服务器的IP地址或主机名,514是标准的syslog端口):
```shell
# 使用两个`@`符号(`@@`)表示使用TCP传输;如果使用单个`@`符号(`@`),则表示使用UDP。
*.* @@remote-host:514
# 例如,将10.30.17.178作为客户端 将主机中所有信息都转发到 10.30.17.200:514 主机中。
*.* @@10.30.17.200:514
```
#### 3.按设施和优先级过滤:您可以配置 rsyslog 根据日志的设施(如 auth, kern, mail 等)和优先级(如 info, warn, err 等)将日志写入不同的文件。例如,要将所有内核消息写入特定文件:
```shell
kern.* /var/log/kern.log
```
#### 4: 应用服务配置更改完成后,您需要重启rsyslog服务以应用更改:
```shell
sudo systemctl restart rsyslog
```
#### 5: 验证 rsyslog 服务器配置
> 最后,确保您的配置按预期工作,您可以检查rsyslog指定的日志文件,或者在客户端使用logger命令发送测试日志消息,然后验证它们是否正确记录:
```shell
# 简单测试 rsyslog 本机会有一份日志并且 服务端也会有一份日志
200-server上:logger -t testTag "This is a testing message by Birkhoff"
178-client上:logger -t FROM178 "From 178 Message to 200 Server"
```
> 本机测试: Mar 12 10:21:27 localhost testTag: This is a testing message by Birkhoff
远程客户端测试: Mar 12 10:31:19 harbor FROM178: From 178 Message to 200 Server
10.30.17.200 - Server上查看 /var/log/message
![]()