LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-12-2013, 07:53 AM   #1
xploreraj
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Rep: Reputation: 0
Recording all the command history in a log


Hello.

Thank you for reading my post.
I have a requirement. I have multiple Linux users sharing the same home path. Multiple terminals may be used for logging in by these different users. I want to generate a log file for all the commands issued for this particular home path along with the username.

I have tried redirecting "history" to a file in the ".login" file itself, but this repeats the information. I can not find any other solution. I am a novice in UNIX programming and I have a C shell.

Please help me in the best logging technique in terms of complexity as well as information.

Regards,
Raj

Last edited by xploreraj; 11-12-2013 at 07:53 AM. Reason: mentioned regards
 
Old 11-12-2013, 08:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
This has been asked many times before.

You might want to start here: Bash logging patches (Blog entry from unSpawn one of our Moderators).
 
1 members found this post helpful.
Old 11-13-2013, 12:28 AM   #3
xploreraj
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

It looks very complex...Could you please guide me to a simple script or some ideas. I have a C shell and want this to be very simple.
 
Old 11-13-2013, 01:07 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,924

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
have you heard about the command screen? Is this what you are looking for?
 
Old 11-13-2013, 11:49 PM   #5
xploreraj
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
I just want the "history" command but in a more meaningful way like recording the username and commands that user executed. The same home path will be shared by multiple users (the users I don't know, I only know that the users will share the same home path), and I want to log every user for same home path.

What I have tried is:
1. In .login file in the home path, I have redirected history to a log so that when an user logs in then the history at the point gets written to the log. Then the current user performs some commands and these are saved in history. When again logged in, any user which has same home path, the .login file will execute and the history of previous login will be logged.
But this has limitations:
- The history is duplicated, even if I delete the .history file at the home path, so a lot of duplicate data in the log file.
- History is written in next login only. So the last login will not have the commands logged.
- The log is not properly managed as the username and history are not properly mapped.

2.
2.1 Written a script: loggerscript.sh
Code:
#!/bin/csh

set logname=usercommandhist_`date +%Y%m%d_%H%M%S`.log
echo "Current user is:" $user >> $logname
script -aq $logname
2.2 Given it permission from main user:
Code:
chmod -R g+rwx $HOME/loggerscript.sh
to run in whole group.
2.3 In .login file, added
Code:
./loggerscript.sh
This has problems:
- Everything is logged, not just commands, the whole screen.
- 'exit' command has to be issued an extra time to stop the script by each user, and I don't want the users to know about the login.
- The data saved in log has many junk values
For example,
Quote:
[1mlinux89[0m /home/user1> ls
[0m[01;34mABC[0m [00mddr.txt[0m
So I hope I have explained what I am trying to do and where I am stuck.

Regards,
Raj
 
Old 11-14-2013, 07:10 AM   #6
voleg
Member
 
Registered: Oct 2013
Distribution: RedHat CentOS Fedora SuSE
Posts: 354

Rep: Reputation: 51
man bash:
Quote:
HISTFILE
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.
Put this in your .bash_profile:
Code:
export HISTFILE=~/.bash_history_$LOGNAME
This way history will be separated even in shared $HOME.
Read man for more options about history.
 
Old 11-14-2013, 07:19 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@voleg: I do believe the OP is using a C-Shell and not Bash.

@xploreraj: You do realize that any user can delete/edit the files in his/her home directory? That's the reason I pointed you to unSpawn's blog entry; Those implementations should prevent just that.
 
Old 11-15-2013, 01:33 AM   #8
xploreraj
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Ok. I am seeing.
 
Old 11-24-2013, 11:30 PM   #9
xploreraj
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi there. Hope I find you all well. I have made some changes. Since I have the need for an IBM AIX 5.3 machine, I have to change the following two files:
1. /etc/profile (for all non-C shell users)
2. /etc/csh.login (for C shell users)

After adding the following script, when the root user logs in, then the logs are created for all the users under following directory:
/var/audit_log/<username>/<xxxxx.log>
This is working for non C shell users, but when the same script is added to csh.login in C syntax, then it does creates the necessary directory, but does not writes the history log. I have seen in some places that for C shell the histfile can not be set, but I want the logs to be written in a similar fashion as other shell users (the code in /etc/profile).

Please help.

The script is:
Code:
echo "welcome to C SHell <csh.login file>"

setenv USER_IP `who -u am i | awk '{print $NF}'|sed -e 's/[()]//g'`
if ( "X${USER_IP}" == "X" )  then
        setenv USER_IP `hostname`
endif

if ( ! -d /var/audit_log )  then
        mkdir /var/audit_log
        chmod 777 /var/audit_log
endif

if ( ! -d /var/audit_log/${LOGNAME} ) then
        mkdir /var/audit_log/${LOGNAME}
        chmod 300 /var/audit_log/${LOGNAME}
endif

set savehist=9999

setenv DT `date "+%Y%m%d_%H%M%S"`
set histfile=/var/audit_log/${LOGNAME}/${USER_IP}_$DT
 
  


Reply

Tags
commands, history, unix



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash command history log - how to write it continuous? Skaperen Linux - Software 12 04-15-2011 02:56 PM
Get log of all history Ejdaha Linux - Newbie 2 01-18-2007 04:28 AM
Need to log users command history FatSteve Linux - Security 2 07-22-2004 06:25 PM
Mozilla problems: Print/print preview doesn't work, not recording sites in history... andy_g_gray Linux - Software 3 01-23-2004 06:27 AM
command history log??? granny Linux - Newbie 3 02-11-2003 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration