Bash – Adding “sudo” commands to the bash history of the *target* user

bashsudo

Something that's bothered me for many years is that if I want to search my shell history for commands which were run as root, I have to check two different locations:

  1. I have to check /root/.bash_history.
  2. I have to check /brian/.bash_history for sudo commands.

Is there a solution for bash (or, indeed, any shell – I'm willing to switch) which will deposit sudo commands into the shell history of the target user?

Best Answer

I assume you are operating in that private unix I-am-root-and-only-user mode, like me. And exactly to avoid a splitting of my personality I log in as root. For the moment - I know it's a kind of security problem.

I am so busy installing, testing etc. as a sysadmin, that I cannot present a satisfying login for a user.

And still the bash history can confuse me when I am using several consoles and/or xterms. Only organization I have so far is a small file hist-keep with a couple of commands I often use.

if I want to search my shell history for commands which were run as root

Do you want to just search, or do you want to "load" the history file, so you can history-search?

I just looked up sudo yesterday, because of this obscure sudo vs. pseudo question. I suddenly realised that "sudo" and "pseudo" are pronounced identically in english, and when you consider "pseudonym" (or "alias") there really is a semantic connection.

Sudo is actually meant to produce a separate tracking of commands. That would be how "root" checks what "brian" (and others) did under pseudonym "root".

But you rather want to merge separate histories.

I feel the solution is more a history -r of a processed file, and not injecting sudo lines into root's history file.

A first aid solution would of course be a simple function/script that searches in these two files, something like

grep $1 $root-hist
grep "sudo.*$1" $user-hist

Then you can sudoroot-combigrep "mount" to get all relevant lines containing "mount". (choose your own easily tab-completeable name)

Related Question