Where is the bash history for the root user saved?

I'm not talking about .bash_history from my home directory. I usually prefer to use sudo -i or sudo su and then run commands as root user.

Since, after these commands the history is not saved in the .bash_history file from my home directory, I want to know if this history is saved somewhere or not and if yes, where?

1

3 Answers

Well, when you're logged in as root, the bash history is saved in /root/.bash_history file, where /root is the default home directory for root user. To prove this, run the following command when you are logged as root:

echo $HISTFILE

The $HISTFILE variable points to the file that contains your history.

Source: $HISTFILE

Generally when you log into another user account, the bash history will be saved in a file called .bash_history located in the home directory of that user.

Furthermore, if you want to save the bash history for the root user in another file, let say in .bash_history file from my home directory, you have to edit /root/.bashrc file, and add a new line at the end of file with the following content:

HISTFILE="/home/username/.bash_history" #change username with your user name

I'm used to using the shortcut list via the up key on the keyboard which shows the last used command for that user. If you log in as root then you will be shown the history for the root user.

Test it like this, sudo -i fill in the password and then hit the up key on the keyboard. Now you will scroll through the last used commands in the order recent to oldest.

I know Radu Rădeanu answer is what you wanted. But this also solves the problem at hand. My solution is more of a handy way of remembering an old command which may save time and hustle.

2

Typing history on the shell list the all the commands in the history. One can use the following command to save history to another file:

history > *textfile*

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like