Sudo: history: command not found - Why I can't sudo history?

Hi,

When I run the following it works:

sudo ls  /root/

I wanted to see root user history. So I ran

sudo history

It said:

Password:
sudo: history: command not found

How come it is not working? How do I tell sudo to run the history command on Linux system?

The history is a shell builtin command. We can see it as follows:

type -a history
command -V history

The trick is to run bash shell with sudo and then call history. One way to do this:

sudo bash -i <<<history
3 Likes

Another option:

sudo cat ~/.bash_history 
sudo cat ~/.bash_history  | grep 'something'

But I agree @monk’s solution is pretty good.

1 Like

Huh, didn’t know you could use a Here String like that. I always used it to redirect either a var or the stdout of a subprocess.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki