5

I just can't seem to get it to work in Ubuntu, it should be a simple answer, maybe someone can help me. All I am trying to do is get a sudoers to use their own history file, but if I set the history var in the .bashrc for root with:

HISTFILE="/root/.bash_history.$SUDO_USER"

nothing seems to happen.

I have tried changing the /root/.bashrc and /etc/skel/.bashrc with the above entry. Nothing. I have gotten this to work in CentOS before (See make sudoers use their own history file).

I even moved the entry to the start of the file, as I found some suggestions that stated it may be ignored after a certain point, but regardless of the position, the history files are not being created. If I use my arrow key however the history entries are there... where they come from I have no clue anymore, it seems like some kind of magic, Voodoo it seems.

Any help would be appreciated.

4
  • Likely your problem: Ubuntu uses dash and CentOS does use bash.
    – Rinzwind
    Nov 16, 2016 at 7:48
  • Do you run a sudo shell using sudo -i? What does echo $HISTFILE give when you're in a sudo session?
    – muru
    Nov 16, 2016 at 7:50
  • I just tried sudo -i and echo $HISTFILE with output: /root/.bash_history.<username> but the file does not exist. Where in the world is the .bash_history file ???
    – Adesso
    Nov 23, 2016 at 13:05
  • dash should not come into play here - it is only used as replacement for bash when invoked as /bin/sh. Interactive shells are not influenced.
    – guntbert
    Jan 4, 2017 at 16:36

1 Answer 1

3

I just followed what you described, accompanied by a few experiments

~$ sudo -s
~# ls -l $HISTFILE
-rw------- 1 g g 44174 Jan  4 16:26 /home/g/.bash_history

~$ sudo -i
~# ls -l $HISTFILE
-rw------- 1 root root 2356 Jan  4 16:23 /root/.bash_history

~$ sudo ls -l $HISTFILE
-rw------- 1 g g 44174 Jan  4 16:26 /home/g/.bash_history

So when we are running standard sudo commands or when we use sudo -s to get a root shell we already are using our own history-file.

Only when we invoke sudo -i to get a proper login shell the original root-history is used.

Now I followed your lead and edited /root/.bashrc to contain

HISTFILE="/root/.bash_history.$SUDO_USER"

as first line.

Repeating above experiments with sudo -s and sudo ls -l $HISTFILE yielded the same results as before (as expected). But sudo -i gave me a shell with no history (and no history-file either).

After executing

~$ sudo -i
~# ls -l .bash_history*
-rw------- 1 root root 2412 Jan  4 17:16 .bash_history
~# echo $HISTFILE
/root/.bash_history.g
~# ls -l $HISTFILE
ls: cannot access '/root/.bash_history.g': No such file or directory

and logging out

~$ sudo -i
~# ls -l .bash_history*
-rw------- 1 root root 2412 Jan  4 17:16 .bash_history
-rw------- 1 root root   52 Jan  4 17:25 .bash_history.g

I get the expected results (including access to "my" history by using the arrow keys.

Keep in mind that the contents of /root/.bashrc are only executed for an interactive, non-login shell (like sudo -s), for a login-shell (like sudo -i) /root/.bashrc is only sourced, and only if /root/.profile contains something like

if [ -f ~/.bashrc ]; then
 . ~/.bashrc
fi
1
  • sudo ls -l $HISTFILE doesn't really make sense because $HISTFILE is going to be expanded before actually running the command.
    – Rag
    Oct 5, 2019 at 1:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .