Configure systemd-journal-upload URL

Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tiene alguna pregunta o comentario sobre nuestro proyecto de traducción actual, ¡no dude en contactarnos!

Description

Ubuntu 22.04 must offload rsyslog messages for networked systems in real time and offload standalone systems at least weekly

Rationale

Information stored in one location is vulnerable to accidental or incidental deletion or alteration. Offloading is a common process in information systems with limited audit storage capacity

Remediation

Shell script

The following script can be run on the host to remediate the issue.

#!/bin/bash

# Remediation is applicable only in certain platforms
if dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$' && { ! (systemctl is-active rsyslog &>/dev/null); }; then

dropin_conf=/etc/systemd/journal-upload.conf.d/60-journald_upload.conf
mkdir -p /etc/systemd/journal-upload.conf.d
chmod 0755 /etc/systemd/journal-upload.conf.d
touch "${dropin_conf}"
chmod 0644 "${dropin_conf}"


for conf in /etc/systemd/journal-upload.conf /etc/systemd/journal-upload.conf.d/*; do
    [[ -e "${conf}" ]] || continue
    sed -i --follow-symlinks 's/^URL\>/#&/g' "${conf}"
done

var_journal_upload_url='remotelogserver'


found=false

# set value in all files if they contain section or key
for f in $(echo -n "${dropin_conf}"); do
    if [ ! -e "$f" ]; then
        continue
    fi

    # find key in section and change value
    if grep -qzosP "(?m)^[[:space:]]*\[Upload\]([^\n\[]*\n+)+?[[:space:]]*URL" "$f"; then
        if ! grep -qzosP "(?m)^[[:space:]]*URL[[:space:]]*=[[:space:]]*$var_journal_upload_url"'[[:space:]]*$' "$f"; then

            sed -i "/^[[:space:]]*URL/s/\([[:blank:]]*=[[:blank:]]*\).*/\1$var_journal_upload_url/" "$f"

        fi

        found=true

    # find section and add key = value to it
    elif grep -qs "^[[:space:]]*\[Upload\]" "$f"; then

            sed -i "/^[[:space:]]*\[Upload\]/a URL=$var_journal_upload_url" "$f"

            found=true
    fi


done

# if section not in any file, append section with key = value to FIRST file in files parameter
if ! $found ; then
    file=$(echo "${dropin_conf}" | cut -f1 -d ' ')
    mkdir -p "$(dirname "$file")"
    chmod 0755 "$(dirname "$file")"

    echo -e "[Upload]\nURL=$var_journal_upload_url" >> "$file"

fi

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi