How do I change the .bash_history file location?

10

4

I'm running CentOS 6.x and want to move the .bash_history to a different location.

The home directories of my users are (because I run a VPS) in /var/www/vhost/<domain>.<tld> which is FTP accessible (and it should be).

Because of this, I have changed the AuthorizedKeysFile for SSH connections out of the normal ~/.ssh/authorized_keys since FTP connections would easily be able to locate them.

At the same time I want to move the .bash_history file to /home/%u/.bash_history where %u is the current user.

Brian Graham

Posted 2014-05-30T19:53:53.100

Reputation: 345

Answers

17

You need to set HISTFILE for your users to the location you need, set the following in .bash_profile for the user, and for new users set it in the user skeleton directory, most likely /etc/skel/.bash_profile

export HISTFILE=/home/$USER/.bash_history

Daniël W. Crompton

Posted 2014-05-30T19:53:53.100

Reputation: 1 716

1Thanks for how to set it up for new users automatically as well. – Brian Graham – 2014-05-30T20:23:42.820

As you are running a VPS I assumed it might come in handy. :) – Daniël W. Crompton – 2014-05-30T20:57:10.677

one note... this needs to be pretty early in the RC file parsing. As soon as anything gets written to history, any changes to this VAR are ignored. – Rich Homolka – 2014-05-30T21:04:14.673

@RichHomolka according to the man file changes to HISTFILE aren't ignored, notably: The name of the file in which command history is saved (see HISTORY below). The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits. – Daniël W. Crompton – 2014-05-30T21:21:17.453

@DaniëlW.Crompton I agree :) I use this to have separate history files based on the tty command. My point was WHEN you set it. At some point the location is fixed and further changes are ignored. – Rich Homolka – 2014-05-30T21:39:22.243

@RichHomolka that must depend on your distribution. I just tried it on bash under Ubuntu, Debian and OSX, all let me change the HISTFILE mid stride. – Daniël W. Crompton – 2014-05-30T22:00:49.903