How to Setup Central Logging Server with Rsyslog in Linux
Last Updated :
02 Jun, 2022
This article will show us how to use Rsyslog to set up Linux as a centralized logging service. When managing logs from various systems, a centralized Rsyslog setup is advantageous. All log entries from client servers will be sent to the host server, which will allow them to be monitored and preserved in one location. Compared to sshing into each server to study its logs, this strategy makes system management considerably easier, especially if there are a lot of servers. Let’s get started with setting up the central logging server.
Prerequisite
- Minimum 2 Linux machines (Here machine we are referring to 2 instances of Linux. It can be both VM, or one VM and one Machine.)
- All should be in the network and reachable.
- rsyslog service is up and running on both machines.
Steps for Setup Central Logging Server with Rsyslog in Linux
Step 1: Configure machine-1 as a central logging server.
By default, rsyslog uses “imjournal” and “imuxsock” modules for importing structured log messages from systemd journal and for accepting rsyslog messages from applications running on the local system via Unix sockets, respectively.
Two protocols can be used for the reception of the log messages on the server machine “TCP” and “UDP” which is using the port number “514” by default.
To use a TCP connection that is slower but reliable. Search and uncomment the lines below
$ModLoad imtcp
$InputTCPServerRun 514
To use a UDP connection which is faster and unreliable. search and uncomment the lines below
$ModLoad imudp
$UDPServerRun 514
We can use a port other than 514. Need to open that port from iptables and also need to turn off the firewall rule for that port. We are going to use TCP for testing purposes if we want we can UDP or both.
Step 2: Setup the destination of the logs on the server machine.
The log messages that we will get on the server machine we need to store them at a specific logfile. For that, we need to set up the rule in rsyslog.conf file.
facility.severity_level destination
- Facility: is a type of process/application generating, they include auth, cron, daemon, kernel, local0..local7. Using “*” means all facilities.
- severity_level: is type of log message: emerg-0, alert-1, crit-2, err-3, warn-4, notice-5, info-6, debug-7. Using “*” means all severity levels and none implies no severity level.
- destination: is either a local file or remote rsyslog server (defined in the form IP:port).
We will use the following ruleset for collecting logs from remote clients.
$template DynamicFile,”/var/log/loghost/%fromhost-ip%.log”
if not ($fromhost-ip == ‘127.0.0.1’) then {
*.* -?DynamicFile
}
2.1: Configure rule for log destination.
We need to put this rule before all the rules in the rsyslog.conf
Now, restart the rsyslog.service to load configuration.
[root@vm-dev ~]# systemctl restart rsyslog.service
2.2: To open the port for listening we need to set rules in SELinux or Firewall whatever one is we are using.
$ sudo firewall-cmd –permanent –add-port=514/udp
$ sudo firewall-cmd –permanent –add-port=514/tcp
$ sudo firewall-cmd –reload
2.3: To check that the port is open and listening to the log using the “netstat” utility.
[root@vm-dev ~]# netstat -tulpn
It will show we that port 514 is listening on both ip4 and ipv6.
Step 3: Configure the client machine to send logs.
To configure the client machine we will be using the legacy method which is simple to understand and which is part of rsyslog.conf.
This will force rsyslog daemon to forward all the logs to the remote rsyslog server. As this is the legacy method. If the remote system is unreachable processing will get blocked here and discard the messages after a while.
# Legacy method
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#facility.priority @@remote-host:remote-port
*.* @@remote-host:514
# New Method
*.* action(type=”omfwd” target=”remote-host” port=”hots-port” protocol=”tcp”
action.resumeRetryCount=”retry-count”
queue.type=”linkedList” queue.size=”message-queue-size”)
We are using the legacy method as it is so it will forward all the messages to the remote rsyslog server. Replace the remote host with the IP of the server machine. Just uncomment the following line and restart rsyslog service on the client machine.
Restart the rsyslog service.
[root@vm-dev ~]# systemctl restart rsyslog.service
Step 4: Test the setup with the logger command.
We are going to test our setup with the “logger” which is provided by the rsyslog as a CLI interface for logging.
Client-Machine
[root@centos ~]# logger -t myApp -p local0.emerg “Hi, This is the test message”
Client Machine:
Server-Machine:
As we can see new log file is getting created on the server machine. The server machine can receive all the logs from the client machine. If we are facing any issues getting messages from the server please check our firewall rules. Most of the time ports are blocked from the firewall at the server as well as the client machine as well. Also, we need to check if the server machine and client machine are reachable from each other.
Similar Reads
How to Setup RabbitMQ in Linux Server?
RabbitMQ is a robust message broker that is open source, used between two different applications to great advantage in handling asynchronous messaging; it is, in turn, very effective at delivering the reliable routing of messages between services. The following guide will show you how to install and
3 min read
How to Control Systemd Services on Remote Linux Server
Linux, SysV, and LSB init scripts are compatible with Systemd, a system and service manager. Aggressive parallelization capabilities are offered by Systemd, which also offers on-demand daemon starting and uses Linux cgroups to keep track of processes. Systemd also supports system snapshotting and re
2 min read
How to Generate a Self-Signed Certificate with OpenSSL in Linux?
TLS (Transport Layer Security) is a secured protocol which is currently in trend. It can see used on top of the HTTP and application layer. The primary use of this protocol is to set up secure communication between web applications and servers. TLS protocol can also be used for secure email transfer
3 min read
Getting started with RSYSLOG in Linux
The Rocket-fast System for log processing (rsyslog) is a system utility provided in Linux which provides support for message logging. It offers high performance and comes with excellent security and has a modular design. This is a very useful utility that can accept input from a wide variety of sour
7 min read
How to Set the Logging Level with application.properties?
In Spring Boot, logging is crucial for monitoring, debugging, and troubleshooting Java applications. Spring Boot provides a convenient way to configure logging levels using properties files, particularly the application.properties file of the Spring application. This allows developers to control the
5 min read
How to Install Python-logging module on Linux?
A programmer's backpack should include a logging library. It can aid in the development of a better knowledge of a program's flow and the discovery of scenarios that you may not have considered when designing. Because most third-party Python libraries employ logging, you may combine your log message
2 min read
How to Assert Log Messages with JUnit in Java?
In Java applications, logging is essential for monitoring application behavior, debugging issues, and tracking performance. Verifying log messages is important for ensuring correct entries during specific actions. In this article, we will demonstrate how to assert log messages in tests using JUnit.
6 min read
How to Secure Your Linux Server with Fail2ban?
If you are a System Administrator or Developer, it is your key responsibility to secure the Linux Server. For that purpose, you can use one of the most effective tools which is Fail2ban for Linux Server. If you can Secure Linux Server with Fail2ban, you can easily manage all kinds of external threat
5 min read
How to Enable PL/SQL Query Logging?
In database management, tracking and analyzing SQL queries are essential for optimizing performance, debugging issues, and ensuring efficient resource utilization. PL/SQL query logging provided a robust mechanism to capture and store the information about the SQL queries executed within the PL/SQL c
4 min read
How to Setup Your Own Proxy Server For Free?
In today's tech tips article, we are going to learn about how to create your own proxy server for free. A proxy server acts as an intermediary between a user and a website. It assigns a new IP address to conceal the user's original IP address and provides access to restricted content or websites. In
4 min read