• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

The Geek Diary

HowTos | Basics | Concepts

  • Solaris
    • Solaris 11
    • SVM
    • ZFS
    • Zones
    • LDOMs
    • Hardware
  • Linux
    • CentOS/RHEL 7
    • RHCSA notes
    • SuSE Linux Enterprise
    • Linux Services
  • VCS
    • VxVM
  • Interview Questions
  • oracle
    • ASM
    • mysql
    • RAC
    • oracle 12c
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Hadoop
    • Hortonworks HDP
      • HDPCA
    • Cloudera
      • CCA 131

How to configure rsyslog server to log messages from different hosts to different files and folders in CentOS/RHEL

By admin

The Ask

Syslog does not support remote logging with individual logs on per host basis. How can remote logging with individual logs on a per-host basis be configured with rsyslog?

How can remote logging be enabled with separate logs for each remote host (system-hostname.log) with date (system-hostname-date.log)?

The Answer

Follow the steps outlined below to install and configure the rsyslog server to log individual logs on a per-host basis. There are various ways that are described below to organize the log files for each host. You may use any one or combine more than one ways as per your requirement.

If rsyslog is not installed then install it using the following:

# yum install rsyslog

For RHEL 6

Separate log file for each host

To configure rsyslog to generate a separate log file for each host, specify the below in /etc/rsyslog.conf:

# vi /etc/rsyslog.conf
$template DynFile,"/var/log/system-%HOSTNAME%.log"
*.* -?DynFile
& ~

Separate log file for each host along with date of creation (year-month-day)

To configure rsyslog to generate a separate log file for each host along with date of creation (year-month-day), modify the template to as shown below:

# vi /etc/rsyslog.conf
$template DynFile,"/var/log/syslog/system-%HOSTNAME%-%$YEAR%-%$MONTH%-%$DAY%-messages.log"
*.* -?DynFile
& ~

Prevent rsyslog server itself logging in to a single file

To configure rsyslog to generate a separate log file for each host and prevent rsyslog server itself logging in to a single file, modify the template as shown below:

# vi /etc/rsyslog.conf
$template DynFile,"/var/log/syslog/system-%HOSTNAME%-%$YEAR%-%$MONTH%-%$DAY%-messages.log"
:fromhost-ip,!isequal,"127.0.0.1" -?DynFile
& ~

generate a separate folder for each host

Configure rsyslog to generate a separate folder for each host :

# vi /etc/rsyslog.conf
$template DynFile,"/var/log/syslog/system-%HOSTNAME%/messages.log"
*.* -?DynFile
& ~

Once you have confirmed the changes to the /etc/rsyslog.conf file, restart rsyslog service.

# service rsyslog restart

For RHEL 7

You can also specify the rules in a separate file /etc/rsyslog.d/from_remote.conf:

# vi /etc/rsyslog.d/from_remote.conf
template(name="DynFile" type="string" string="/var/log/remote/system-%FROMHOST-IP%.log")
ruleset(name="RemoteMachine"){  action(type="omfile" dynaFile="DynFile")  }
module(load="imudp")
input(type="imudp" port="514" ruleset="RemoteMachine")
module(load="imtcp")
input(type="imtcp" port="514" ruleset="RemoteMachine")

Restart/reload rsyslog service once you are done with the changes:

# systemctl restart rsyslog

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL 7 : Unable to start/enable iptables
  2. CentOS / RHEL 7 : How to create custom script to run automatically during boot
  3. Beginners Guide to Udev in Linux
  4. CentOS / RHEL 7 : Configuring static IP adress using network interface configuration files
  5. Linux OS service ‘iscsid’
  6. How to change the number of commands stored in Bash History
  7. CentOS / RHEL 6 : How to rebuild Initial Ramdisk Image
  8. How to configure DM-Multipath from an iSCSI initiator to an iSCSI target in CentOS / RHEL
  9. UNIX / Linux : How to install and configure mutt
  10. Understanding shell profiles in CentOS/RHEL

You May Also Like

Primary Sidebar

Recent Posts

  • How to disable firewalld and nftables and use iptables instead in CentOS/RHEL 8
  • How to add an Ethernet connection using nmcli in CentOS/RHEL 7
  • How to set the order to load certain modules in CentOS/RHEL 7 and 8
  • How to configure initrd / initramfs to including kernel modules in CentOS/RHEL
  • How to configure systemd.path to trigger an event when any changes made to a directory
  • Archives
  • Contact Us
  • Copyright

© 2019 · The Geek Diary