Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to prevent specific user from executing history -c command in shell.

share|improve this question
    
This is quite a difficult problem. Ultimately, the user could always create a script and run that, commands executed from a script are not logged. –  minerz029 Oct 16 '13 at 10:38

2 Answers 2

history command is a bash builtin command (you can find it somewere at the line 4348 in man bash). So you can use something like this in this case.

What you can do is to harden /home/username/.bash_history by making it append-only:

sudo chattr +a /home/username/.bash_history

Also, you have to harden history environment variables by adding the following lines to /home/username/.bashrc:

shopt -s histappend 
readonly PROMPT_COMMAND="history -a" 
readonly HISTFILE 
readonly HISTFILESIZE 
readonly HISTSIZE 
readonly HISTCMD 
readonly HISTCONTROL 
readonly HISTIGNORE

Source: http://superuser.com/questions/308882/secured-bash-history-usage.

share|improve this answer
    
What do you mean by "So you can use something like this in this case."? If it's a builtin, there will be no file to chmod. As from your source, you should also set the various bash profile files to read only. –  minerz029 Oct 16 '13 at 9:58
    
What is stopping the user from editing .bashrc to remove those lines? It should be set to append as well. –  minerz029 Oct 16 '13 at 10:18
    
You could chown them all to root, but I'm not sure if that will have further consequences. –  minerz029 Oct 16 '13 at 10:40

As an alternative to Radu's answer, this will chown all the bash related files to root then give other users read only access (except .bash_history and .bashrc, for which they will have read and append premissions)

Add these lines to $HOME/.bashrc:

shopt -s histappend 
readonly PROMPT_COMMAND="history -a" 
readonly HISTFILE 
readonly HISTFILESIZE 
readonly HISTSIZE 
readonly HISTCMD 
readonly HISTCONTROL 
readonly HISTIGNORE

Then execute these commands in a terminal:

sudo chown root /home/$USER/.bash_history 
sudo chown root /home/$USER/.bash_profile 
sudo chown root /home/$USER/.bash_login
sudo chown root /home/$USER/.profile
sudo chown root /home/$USER/.bash_logout
sudo chown root /home/$USER/.bashrc

sudo chmod o+rwt     /home/$USER/.bash_history 
sudo chmod o+rt,a+x  /home/$USER/.bash_profile 
sudo chmod o+rt,a+x  /home/$USER/.bash_login
sudo chmod o+rt,a+x  /home/$USER/.profile
sudo chmod o+rwt,a+x /home/$USER/.bash_logout
sudo chmod o+rwt,a+x /home/$USER/.bashrc

sudo chattr +a /home/$USER/.bash_history 
sudo chattr +a /home/$USER/.bash_logout
sudo chattr +a /home/$USER/.bashrc

Warning: Not tested at all, this is provided with ABSOLUTELY NO WARRANTY!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.