Sudo Command Audit: Track Who Ran Commands in Linux

Quick Linux Tip #122:

Question: Who ran sudo commands on my server today?

Try: sudo grep 'sudo' /var/log/auth.log | grep -v 'session' | tail -10

Info: Filters authentication logs to display sudo command executions while excluding noisy session events.

Examples:

  • $ sudo journalctl _COMM=sudo --since today  # Query sudo logs using systemd journalctl
  • $ sudo grep 'FAILED' /var/log/auth.log  # Audit failed authentication or sudo attempts
  • $ sudo aureport -ts today --auth  # Generate authentication reports via auditd

Note: On RHEL/CentOS systems, authentication logs are stored in /var/log/secure. Modern systemd distributions rely primarily on journalctl. Enable Defaults log_output in your sudoers configuration for keystroke-level auditing.




LinuxTeck.com
linuxteck:~$ sudo grep 'sudo' /var/log/auth.log | grep -v 'session' | tail -10
Nov  9 08:12:34 server01 sudo:    linuxteck : TTY=pts/0 ; PWD=/home/linuxteck ; USER=root ;
COMMAND=/usr/bin/apt update
Nov  9 08:23:45 server01 sudo:    linuxteck : TTY=pts/0 ; PWD=/home/linuxteck ; USER=root ;
COMMAND=/usr/sbin/service nginx restart
Nov  9 09:34:56 server01 sudo:    admin : TTY=pts/1 ; PWD=/home/admin ; USER=root ; COMMAND=/bin/vi
/etc/nginx/nginx.conf
Nov  9 10:12:45 server01 sudo:    deploy : TTY=pts/2 ; PWD=/opt/app ; USER=root ;
COMMAND=/opt/app/deploy.sh
Nov  9 10:45:23 server01 sudo:    linuxteck : TTY=pts/0 ; PWD=/home/linuxteck ; USER=root ;
COMMAND=/usr/bin/systemctl restart mysql
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #121: systemctl show: Find the Exact Systemd Service Executable NEXT ARTICLE Quick Linux Tip #123: Linux Filesystem Check: 3 Commands to Detect Disk Errors
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.