Skip to content

Instantly share code, notes, and snippets.

@jdavidrcamacho
Last active October 10, 2025 07:34
Show Gist options
  • Save jdavidrcamacho/c9a5dedb6e3687dcda1228857be5c14d to your computer and use it in GitHub Desktop.
Save jdavidrcamacho/c9a5dedb6e3687dcda1228857be5c14d to your computer and use it in GitHub Desktop.
stuff
# 1) Create the group (safe if it already exists)
sudo groupadd -r cmdlog || true
# 2) Make sure the log file exists
sudo touch /var/log/test.log
# 3) Give it group ownership & permissions
sudo chgrp cmdlog /var/log/test.log
sudo chmod 664 /var/log/test.log
# 4) Add *your current user* to that group
sudo usermod -aG cmdlog $USER
# 5) Start a new shell session with the updated group list
newgrp cmdlog
# 6) Verify you’re now in the group
groups
# 7) Test writing to the file (should succeed without sudo now)
echo "test line $(date --iso-8601=seconds)" >> /var/log/test.log
sudo chown root:adm /var/log/test.log
sudo chmod 664 /var/log/test.log
sudo usermod -aG adm env-admin
@jdavidrcamacho
Copy link
Author

echo $PROMPT_COMMAND

@jdavidrcamacho
Copy link
Author

# Command logger for Fluentd
if [ -n "$PS1" ] && [ -z "$BASH_COMMAND_LOGGER_SET" ]; then
  export BASH_COMMAND_LOGGER_SET=1
  shopt -s histappend
  export HISTTIMEFORMAT="%F %T "
  LOG_FILE="/var/log/test.log"
  PROMPT_COMMAND='LAST_CMD=$(HISTTIMEFORMAT= history 1 | sed "s/^ *[0-9]\+ *//"); \
    printf "%s user=%q tty=%q pwd=%q cmd=%q\n" "$(date --iso-8601=seconds)" "$USER" "$(tty 2>/dev/null)" "$PWD" "$LAST_CMD" >> "$LOG_FILE"; \
    history -a'
fi

@jdavidrcamacho
Copy link
Author

sudo usermod -aG cmdlog $USER
newgrp cmdlog

@jdavidrcamacho
Copy link
Author

ls -l /var/log/test.log # expect: -rw-rw-r-- 1 root cmdlog ...
sudo chgrp cmdlog /var/log/test.log
sudo chmod 664 /var/log/test.log

@jdavidrcamacho
Copy link
Author

sudo chgrp cmdlog /var/log/test.log
sudo chmod 664 /var/log/test.log
sudo setfacl -m g:cmdlog:rw /var/log/test.log

@jdavidrcamacho
Copy link
Author

Command logger for Fluentd

if [ -n "$PS1" ] && [ -z "$BASH_COMMAND_LOGGER_SET" ]; then
export BASH_COMMAND_LOGGER_SET=1
shopt -s histappend
export HISTTIMEFORMAT="%F %T "
LOG_FILE="/var/log/fluentd/cmd.log"
PROMPT_COMMAND='LAST_CMD=$(HISTTIMEFORMAT= history 1 | sed "s/^[[:space:]][0-9]+[[:space:]]//"); printf "%s user=%q tty=%q pwd=%q cmd=%q\n" "$(date --iso-8601=seconds)" "$USER" "$(tty 2>/dev/null)" "$PWD" "$LAST_CMD" >> "$LOG_FILE"; history -a'
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment