在Linux下記錄所有使用者的登入和操作日誌

2022-07-26 19:24:09 字數 2825 閱讀 4923

history

user=`whoami`

user_ip=`who -u am i 2>/dev/null| awk ''|sed -e 's/[()]//g'`

if [ "$user_ip" = "" ]; then

user_ip=`hostname`

fiif [ ! -d /var/log/history ]; then

mkdir /var/log/history

chmod 777 /var/log/history

fiif [ ! -d /var/log/history/$ ]; then

mkdir /var/log/history/$

chmod 300 /var/log/history/$

fiexport histsize=4096

dt=`date +"%y%m%d_%h:%m:%s"`

export histfile="/var/log/history/$/$@$_$dt"

chmod 600 /var/log/history/$/*history* 2>/dev/null

注釋: /var/log/history這是記錄日誌的存放位置,可以自定義。

在/var/log/history下會以每個使用者為名新建乙個資料夾,

[root@telecom225 /]# cd /var/log/history/

[root@telecom225 history]# ll

total 20

d-wx------ 2 test dennishan 4096 jul 4 12:06 test

d-wx------ 2 jerryhe jerryhe 4096 jul 4 14:12 jerryhe

每次使用者退出後都會產生以使用者名稱、登入ip、時間的檔案,裡面包含此使用者本次的所以操作

[root@telecom225 history]# cd jerryhe/

[root@telecom225 jerryhe]# ls

[email protected]_20130626_14:59:51

[email protected]_20130628_14:40:26

[email protected]_20130703_09:44:21

[email protected]_20130703_11:18:40

[email protected]_20130703_11:21:11

[email protected]_20130703_14:21:49

[email protected]_20130703_14:30:48

[email protected]_20130704_13:37:19

history

user=`whoami`

user_ip=`who -u am i 2>/dev/null| awk ''|sed -e 's/[()]//g'`

if [ "$user_ip" = "" ]; then

user_ip=`hostname`

fiif [ ! -d /var/log/history ]; then

mkdir /var/log/history

chmod 777 /var/log/history

fiif [ ! -d /var/log/history/$ ]; then

mkdir /var/log/history/$

chmod 300 /var/log/history/$

fiexport histsize=4096

dt=`date +"%y%m%d_%h:%m:%s"`

export histfile="/var/log/history/$/$@$_$dt"

chmod 600 /var/log/history/$/*history* 2>/dev/null

注釋: /var/log/history這是記錄日誌的存放位置,可以自定義。

在/var/log/history下會以每個使用者為名新建乙個資料夾,

[root@telecom225 /]# cd /var/log/history/

[root@telecom225 history]# ll

total 20

d-wx------ 2 test dennishan 4096 jul 4 12:06 test

d-wx------ 2 jerryhe jerryhe 4096 jul 4 14:12 jerryhe

每次使用者退出後都會產生以使用者名稱、登入ip、時間的檔案,裡面包含此使用者本次的所以操作

[root@telecom225 history]# cd jerryhe/

[root@telecom225 jerryhe]# ls

[email protected]_20130626_14:59:51

[email protected]_20130628_14:40:26

[email protected]_20130703_09:44:21

[email protected]_20130703_11:18:40

[email protected]_20130703_11:21:11

[email protected]_20130703_14:21:49

[email protected]_20130703_14:30:48

[email protected]_20130704_13:37:19

Linux下記錄所有使用者操作的指令碼

這個指令碼是在網上找到的,稍微做了一些修改,可以實現在linux下所有使用者,不管是遠端還是本地登陸,在本機的所有操作都會記錄下來,並生成包含 使用者 ip 時間 的檔案存放在指定位置,方便管理員以後檢視不同使用者都在伺服器上幹了些什麼!目前這個 只實現了記錄使用者的操作命令,但是像vi編輯只會記錄...

Linux下記錄所有使用者的登入和操作日誌

linux下記錄所有使用者的登入和操作日誌 history user whoami user ip who u am i 2 dev null awk sed e s g if user ip then user ip hostname fiif d var log history then mkd...

Linux下記錄所有使用者的登入和操作日誌

history user whoami user ip who u am i 2 dev null awk sed e s g if user ip then user ip hostname fiif d var log history then mkdir var log history chm...