Created
May 17, 2017 08:37
-
-
Save oskarpearson/1aaa048406d2e9e4b8d633b807cabfae to your computer and use it in GitHub Desktop.
Log bash commands to syslog, even after sudo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Store this at /etc/profile.d/log-commands.sh mode 0644 | |
# | |
# Log commands to syslog, where they will be picked up by the aws log watcher | |
function log2syslog | |
{ | |
declare command | |
command=$BASH_COMMAND | |
if [ "$command" != "history -a" ]; then | |
logger -p local1.notice -t bash -i -- "$USER (sudo_user: $SUDO_USER) : $command" | |
fi | |
} | |
trap log2syslog DEBUG | |
# Only append to history file - rather than overwite it | |
shopt -s histappend | |
# Save all lines of a multi-line command in the same history entry | |
shopt -s cmdhist | |
# Force the history filename | |
HISTFILE="$HOME/.bash_history" | |
# Don't use the default size limits | |
HISTFILESIZE=1000000 | |
HISTSIZE=1000000 | |
# Don't let items with leading spaces skip history, or ignore duplicate commands | |
HISTCONTROL=ignoreboth | |
# Don't ignore specific patterns | |
HISTIGNORE="" | |
# Log the time when things happened too | |
HISTTIMEFORMAT='%F %T ' | |
# Write to the history immediately | |
PROMPT_COMMAND='history -a' | |
# Reset the history number | |
unset HISTCMD | |
# Special root settings | |
if groups | grep -q root | |
then | |
# Timeout root logins after 3600 seconds (1 hour) | |
TMOUT=3600 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment