Logged in hostname/IP in linux command history

1.2k Views Asked by At

Generally Linux history command is powerful to can store per user based command history with respective time stamps. But in real world it happens like, with same user login, multiple people try to login from different hosts.

Is there any way to store the host-name/IP along with the time-stamp in history ?

1

There are 1 best solutions below

0
On

You can do that by using PROMPT_COMMAND variable.

Add the following lines in /etc/profile

CUSTOM_HISTORY_FILE=/tmp/hfile_$(date +%F_%T)
PROMPT_COMMAND='echo "$(/bin/rhost)  $(history |tail -1)" >> $CUSTOM_HISTORY_FILE '

You can customize the above PROMPT_COMMAND as you need.

Create a script which can fetch the info of server which user logged in.

[root@client1 ~]# cat /bin/rhost
who am i |awk '{print $NF}'|sed -e 's/)//' -e 's/(//'