Multi-user login automatically saves history to a unified directory

Scenario: In the production environment, multiple ordinary users log in, and after logging in, the history operations are automatically recorded and saved in a unified directory.

Specific requirements:

1) Subdirectories and history record files are automatically created after each user logs in;

2) Users are allowed to create history record files and append content, and modification and deletion are not allowed;

3) Users are not allowed to modify and delete subdirectories of other users ;

4) Do not allow users to view the contents of other users' subdirectories;

solution:

write /etc/profile file and add the following content
# securiry record history
# add by shenxiaoran

history
USER_IP=$(who am i 2>/dev/null | awk '{print $NF}' | sed -e 's/[()]//g')
if [ "$USER_IP" = "" ];then
    USER_IP=`hostname`
fi

if [ ! -d /tmp/ user_history ];then
    mkdir /tmp/user_history
    chown root.root /tmp/user_history
    chmod 777 /tmp/user_history
    chattr +a /tmp/user_history
fi

if [ ! -d /tmp/user_history/${LOGNAME} ];then
    mkdir -p /tmp/user_history/${LOGNAME}
fi

export HISTTIMEFORMAT='%F %T '
export HISTSIZE='40960'
time=$(date '+%Y%m%d-%H:%M:%S')
export HISTFILE="/tmp/user_history/${LOGNAME}/${USER_IP}[$time]"
chmod 600 /tmp/user_history/$ {LOGNAME}/*history* 2>/dev/null

save

# source /etc/profile to take effect

Test :

create a common user, passwd user password, log in with a common user After a

common user logs in, the history record file will not be created, and it will be generated immediately after exiting , as follows:

# ll /tmp/user_history/root/
total 8
-rw------- 1 root root 81 Apr 3 11:43 192.168.11.50[20150403-03:52:17]
-rw-- ----- 1 root root 1239 Apr 3 11:45 192.168.11.50[20150403-11:39:11]
Note: After modifying the /etc/profile file, the user can only view the history of this login by entering the history command after logging in. If you want to view the previous one, you can cat ~/.bash_history file.

Every time a user opens a terminal, a file is saved under /tmp/user_history to record the history information of the current terminal.

View the file content #cat 192.168.11.50[20150403-11:39:11]

#1428032352
history
#1428032387
cd /tmp/user_history/root/

Description: #1428032352 here is in the form of Unix time, if you want to convert it to human mode , you can use the date -d command to convert

# date -d @1428032352
Fri Apr 3 11:39:12 CST 2015

# date -d @1428032352 +'%F %T'
2015-04-03 11:39:12

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991109&siteId=291194637