Guide: centralized command logging with rsyslog

  1. Step 1 — Forward all logs to the central syslog server
  2. Step 2 — Enable command logging for root
  3. Step 3 — Enable it for existing and future users
  4. Step 4 — Route the `local6` facility to `/var/log/cmdlog.log`
  5. Step 5 — Add timestamps to `history`
  6. Step 6 — Configure logrotate for `/var/log/cmdlog.log`
  7. Illustration 1 — Check that rsyslog is running
  8. Illustration 2 — Run any command, then read the cmdlog file
  9. Illustration 3 — Verify the log reached the central server
  10. Illustration 4 — Handle duplicated logs
  11. References
  12. Related articles

Estimated time: 60 minutes.

Set up centralized command logging on Linux nodes: every shell command a user runs is recorded together with user, timestamp, shell_pid, exit code and client IP, and then forwarded to a central syslog server.

The mechanism:

the PROMPT_COMMAND (Bash) variable runs logger to write to the local6 facility (one of local0–local7,

RFC 5424 §6.2.1);

rsyslog routes local6.* to /var/log/cmdlog.log and ships it to the server using the RSYSLOG_SyslogProtocol23Format (RFC 5424) template.

Completion requirements:

Prerequisites: 2 Linux nodes (1 as the syslog server that receives logs, 1 as the client), sudo access, and a basic understanding of rsyslog.

The default syslog port is 514 (RFC 5424); this guide uses the custom port 9514 on the server.

The addresses in this article are illustrative addresses from the range reserved for documentation (RFC 5737): syslog server 192.0.2.21, client 192.0.2.22; when you do this for real, replace them with your own IPs.

Step 1 — Forward all logs to the central syslog server

Add an action rule to /etc/rsyslog.conf and then restart. The @ prefix = send over UDP, @@ = TCP (rsyslog omfwd).

IP_SERVER='192.0.2.21:9514'   # syslog server : port
echo "*.*  @$IP_SERVER;RSYSLOG_SyslogProtocol23Format" | sudo tee -a /etc/rsyslog.conf > /dev/null
sudo systemctl restart rsyslog

Note: *.* = every facility, every severity.

RSYSLOG_SyslogProtocol23Format packs the message according to RFC 5424 (with structured data and an ISO 8601 timestamp).

Step 2 — Enable command logging for root

Append the following block to /root/.bashrc.

PROMPT_COMMAND runs right before the shell displays the next prompt (Bash manual),

so the command you just entered is passed through logger into the local6 facility at debug severity.

sudo tee -a /root/.bashrc >/dev/null <<'EOF'
# Command log — each interactive command emits one JSON line through logger local6.debug
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "{\"log_type\": \"cmdlog\", \"time_cli\": \"$(date "+%d/%m/%y %T")\", \"user\": \"$(whoami)\", \"shell_pid\": \"$$\", \"command\": \"$(HISTTIMEFORMAT= history 1 | sed "s/^ *[0-9]* *//")\", \"return_value\": \"$RETRN_VAL\", \"client_ip\": \"${SSH_CLIENT%% *}\"}"'
export HISTTIMEFORMAT="%d/%m/%y %T "
EOF

Note: HISTTIMEFORMAT= history 1 fetches the command you just ran with HISTTIMEFORMAT switched off locally (otherwise the command would carry a date and time prefix,

because this block turns HISTTIMEFORMAT on); sed strips the history sequence number. client_ip comes from ${SSH_CLIENT%% *} —

the first field of the SSH_CLIENT variable that sshd sets for you (<client-ip> <client-port> <server-port>); do not overwrite this variable.

The payload is single-line JSON so the server side can parse it easily (log_type, time_cli, user, shell_pid, command, return_value, client_ip).

This block is identical to the one in Exercise: shell startup files over SSH and command logging, which shows why the original script (history 1, who am i | awk '{print $5}') logs incorrectly on Ubuntu 24.04.

Step 3 — Enable it for existing and future users

Append the same PROMPT_COMMAND block from Step 2 to the system-wide bashrc file for your distro. This one file covers both existing users and users created later.

Ubuntu/Debian: interactive bash reads /etc/bash.bashrc automatically for every user, so there is no need to edit /etc/skel/.bashrc.

# --- Ubuntu / Debian ---
sudo tee -a /etc/bash.bashrc >/dev/null <<'EOF'
# Command log — each interactive command emits one JSON line through logger local6.debug
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "{\"log_type\": \"cmdlog\", \"time_cli\": \"$(date "+%d/%m/%y %T")\", \"user\": \"$(whoami)\", \"shell_pid\": \"$$\", \"command\": \"$(HISTTIMEFORMAT= history 1 | sed "s/^ *[0-9]* *//")\", \"return_value\": \"$RETRN_VAL\", \"client_ip\": \"${SSH_CLIENT%% *}\"}"'
export HISTTIMEFORMAT="%d/%m/%y %T "
EOF

RHEL/CentOS/Rocky: bash does not read /etc/bashrc on its own, but the default ~/.bashrc (copied from /etc/skel/.bashrc) already does source /etc/bashrc, so there is no need to edit skel here either.

# --- RHEL / CentOS / Rocky ---
sudo tee -a /etc/bashrc >/dev/null <<'EOF'
# Command log — each interactive command emits one JSON line through logger local6.debug
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "{\"log_type\": \"cmdlog\", \"time_cli\": \"$(date "+%d/%m/%y %T")\", \"user\": \"$(whoami)\", \"shell_pid\": \"$$\", \"command\": \"$(HISTTIMEFORMAT= history 1 | sed "s/^ *[0-9]* *//")\", \"return_value\": \"$RETRN_VAL\", \"client_ip\": \"${SSH_CLIENT%% *}\"}"'
export HISTTIMEFORMAT="%d/%m/%y %T "
EOF

Note: Root also reads this system-wide file, so after Step 3 the block from Step 2 is redundant but harmless (the same values are assigned twice, and each command still produces only one log line).

Any user who has removed the source /etc/bashrc line from their ~/.bashrc (RHEL) will not be logged.

Step 4 — Route the `local6` facility to `/var/log/cmdlog.log`

The location of the rule file differs by distro (rsyslog config). Create the log file and give the syslog user ownership before restarting.

# CentOS/RHEL:
echo "local6.* /var/log/cmdlog.log" | sudo tee -a /etc/rsyslog.conf > /dev/null
# Ubuntu/Debian:
echo "local6.* /var/log/cmdlog.log" | sudo tee -a /etc/rsyslog.d/50-default.conf > /dev/null

sudo touch /var/log/cmdlog.log
sudo chown syslog:syslog /var/log/cmdlog.log
sudo systemctl restart rsyslog

Note: Declare the local6.* rule in only ONE place. Declaring it in both /etc/rsyslog.conf and /etc/rsyslog.d/50-default.conf is what causes the log to be written twice (see the Illustration section).

Step 5 — Add timestamps to `history`

HISTTIMEFORMAT (%d/%m/%y %T) is already in the block from Steps 2–3, so each history line carries a date and time that matches time_cli in the cmdlog.

There is no need to add another export HISTTIMEFORMAT line. Check it in a fresh SSH session:

history 3

Step 6 — Configure logrotate for `/var/log/cmdlog.log`

(logrotate(8)) to cap the size: rotate when the file reaches 100M, keep 4 copies, compress them, and name them by date.

cat << 'EOF' | sudo tee /etc/logrotate.d/cmdlog > /dev/null
/var/log/cmdlog.log {
    size 100M
    rotate 4
    create
    dateext
    compress
}
EOF

Note: size 100M rotates by size (not by schedule). create recreates an empty file after rotation so rsyslog can keep writing.

Illustration 1 — Check that rsyslog is running

after the configuration.

systemctl status rsyslog --no-pager

Result:

● rsyslog.service - System Logging Service
     Active: <strong>active (running)</strong>
       ...

Note: If it shows failed: check the syntax of the rule file you just added with rsyslogd -N1.

Illustration 2 — Run any command, then read the cmdlog file

on the client. Each command produces one JSON line.

ls -la /etc > /dev/null
tail -n 1 /var/log/cmdlog.log

Result:

{"log_type": "cmdlog", "time_cli": "23/06/26 14:30:01", "user": "root", "shell_pid": "20455", "command": "ls -la /etc", "return_value": "0", "client_ip": "10.0.0.5"}

Note: return_value is the exit code of the previous command; client_ip comes from SSH_CLIENT, so it is empty when you work directly on the console (not over SSH).

Illustration 3 — Verify the log reached the central server

On the syslog server, filter by the client's host.

tail -f /var/log/remote/<client-host>/*.log
# or: journalctl -u rsyslog -f

Result:

<client-host> ... {"log_type": "cmdlog", "user": "root", "command": "ls -la /etc", ...}

Note: If you do not see the log on the server: check the firewall/port (default 514, this guide uses 9514) and that the server has enabled an input module to receive logs.

Illustration 4 — Handle duplicated logs

Symptom: every command shows up as 2 lines for the same event → the local6 rule is declared in more than one place.

cd /etc/rsyslog.d
grep -Rin "cmd"

Result:

50-default.conf:7:local6.* /var/log/cmdlog.log
# (this rule also appears in /etc/rsyslog.conf → duplicate)

Note: Comment out or delete one of the duplicate rule lines (usually the one in 50-default.conf), keep exactly one place, and then run sudo systemctl restart rsyslog.

References

Related articles

Guide: centralized command logging with rsyslog