Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Syslog configuration for remote logservers for syslog-ng and rsyslog, both client and server

Published: 21-06-2018 | Author: Remy van Elst | Text only version of this article


❗ This post is over six years old. It may no longer be up to date. Opinions may have changed.


A Teletype ASR-33 printing system output

Syslog is the protocol, format (and software) linux and most networking devices use to log messages. All kinds of messages, system, authentication, login and applications. There are multiple implementations of syslog, like syslog-ng and rsyslog. Syslog has the option to log to a remote server and to act as a remote logserver (that receives logs). With a remote logging server you can archive your logs and keep them secure (when a machine gets hacked, if root is compromised the logs on the machine are no longer trustworthy). This tutorial shows how to set up a syslog server with rsyslog and syslog-ng and shows how to setup servers as a syslog client (that log to a remote server) with syslog-ng and rsyslog.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

Server: rsyslog

rsyslog is the default syslog service on Ubuntu, Debian, OpenSUSE and CentOS (next to systemd's journald). The configuration syntax is simpler than syslog- ng's, but complex configuration is more clear in syslog-ng. Bottom line they both work just as well. The below steps are to be taken to setup rsyslog as a syslog service to receive syslogs.

Edit the following file:

vim /etc/rsyslog.d/10-remote.conf 

Add the following:

$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 192.0.2.0/24

$template RemoteStore, "/var/log/remote/%HOSTNAME%/%timegenerated:1:10:date-rfc3339%"
:source, !isequal, "localhost" -?RemoteStore
:source, isequal, "last" ~

This will allow all hosts in the subnet 192.0.2.0/24 to log to this machine.

Restart rsyslog to make the changes active:

service rsyslog restart

The files will be placed in /var/log/remote, sorted on hostname. For example:

 ls -la /var/log/remote/server1.example.net/

Output:

total 10G
drwxr-xr-x 2 syslog syslog 4.0K Jun 19 08:44 .
drwxr-xr-x 4 syslog syslog 4.0K Jun 13 13:54 ..
-rw-r----- 1 syslog adm    2.6G Jun 14 23:59 2018-06-14
-rw-r----- 1 syslog adm    2.5G Jun 15 23:59 2018-06-15
-rw-r----- 1 syslog adm    1.4G Jun 16 23:59 2018-06-16
-rw-r----- 1 syslog adm    1.3G Jun 17 23:59 2018-06-17
-rw-r----- 1 syslog adm    1.1G Jun 18 23:59 2018-06-18
-rw-r----- 1 syslog adm    1.5G Jun 19 16:23 2018-06-19

When you just configured a client, it will take some time (a few minutes) before the logs and folder appear under /var/log/remote.

Logrotate

As you can see logging can take up some space, I recommend to setup logrotate for this remote folder. You can do so on Ubuntu by creating the following logrotate config file:

vim /etc/logrotate.d/remote

Contents:

/var/log/remote/*/*
{
        rotate 90
        daily
        missingok
        compress
}

This will compress and rotate logs every day and keep them for 90 days (3 months). To test your config, use the following command:

logrotate -d --force /etc/logrotate.d/remote

(that will rotate all your logs, don't CTRL+C it otherwise your log folder will be messed up)

You don't have to restart a service since logrotate is ran via cron (/etc/cron.daily/logrotate).

Client: rsyslog (Ubuntu)

On Ubuntu or any rsyslog server, to log to a remote syslogserver, add the following to rsyslog.conf:

    *.*   @192.0.2.10:514

(Replace 192.0.2.10 with the IP or hostname of your syslog server)

The file can be either:

  • /etc/rsyslog.conf
  • /etc/rsyslog.d/99-remote.conf

Restart rsyslog to make the changes active:

service rsyslog restart

Server: syslog-ng

syslog-ng is the default on older versions of SUSE Enterprise Linux and OpenSUSE next to systemd's journald and on HP-UX. Most older distro's use it as well, Debian, Fedora and Arch all had it as their default years ago.

To set up syslog-ng as a remote log server that can receive logs, edit the following file:

vim /etc/syslog-ng/syslog-ng.conf

Add or edit:

source net { udp(); };
destination remote { file("/var/log/remote/${FULLHOST}"); };
log { source(net); destination(remote); };

This file can also be in /etc/syslog-ng/conf.d/ under a different name. Restart syslog-ng to make the changes active:

service syslog-ng restart

This will place the logfiles in /var/log/remote. As far as I could find in the documentation, there is no option to limit on subnet like rsyslog has in the above example. Use the firewall to allow access from different networks.

With syslog-ng it is also recommended to setup logrotate and compression. See the rsyslog server section on how to do that.

Client: syslog-ng

The setup for sending logs to a remote syslog server is simple. Edit the syslog-ng.conf file:

vim /etc/syslog-ng/syslog-ng.conf

Add or edit the following:

destination remote { network("192.0.2.10" transport("udp") port(514)); };

Older versions do not support the network() syntax, you need to use the older tcp() or udp() syntax:

destination remote { udp("192.0.2.10" port(514)); };

In both cases, replace 192.0.2.10 with your logserver's IP. (Unless you are using TEST-NET-1 of course).

This file can also be in /etc/syslog-ng/conf.d/ under a different name. Restart syslog-ng to make the changes active:

service syslog-ng restart

What about systemd / journald?

Systemd and journald are taking over every part of your linux system including logging. Most distro's supply a syslog service which journald (systemd's binary logging component) forwards logs to. If your system is not set up like that, you need to install either rsyslog or syslog-ng and tell journald to forward the logs to syslog:

vim /etc/systemd/journald.conf 

Add or change:

ForwardToSyslog=yes

Restart journald:

systemctl restart systemd-journald

If your syslog-ng or rsyslog version is recent enough, all journald logs will now appear in syslog as well.

Tags: hp-ux , log , logging , opensuse , rsyslog , security , sles , suse , syslog , syslog-ng , tutorials