Article Title: Audit and tracking Linux user activities. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Some abnormal users try to migrate all activity records on the system (for example ~ /. Bash_history), but we can use dedicated tools to monitor the commands executed by all users. we recommend that you use process accounting to record your activities. You can use process accounting to view the commands executed by each user, including CPU time and memory usage.
The psacct program provides several process activity monitoring tools: ac, lastcomm, accton, and sa.
The. ac command displays the user connection time statistics.
The. lastcomm command displays the commands executed by the system.
The. accton command is used to enable or disable the process accounting function.
The. sa command is used to count the accounting of system processes.
1) install the psacct or acct Software Package
If you use RHEL, use the up2date command:
# Up2date psacct
If you use CentOS/Fedora Core Linux, use the yum command:
$ Sudo apt-get install acct
Or
# Apt-get install acct
2) Start the psacct/acct Service
On Ubuntu/Debian Linux, pacct can be automatically started. (The Installation Package creates a/var/account/pacct file on the system ). however, in Red Hat/Fedora Core/Cent OS, You need to manually start the psacct service. run the following two commands to create the/var/account/pacct file and start the pacct service:
# Chkconfig psacct on
#/Etc/init. d/psacct start
If you use Suse Linux and the service name is acct, run the following command:
# Chkconfig acct on
#/Etc/init. d/acct start
Now we can learn how to use these tools to monitor user commands and time.
3). display the statistics of the user's connection time
The command prints the user's connection time (in hours) on the screen based on the number of logins/exits ). the total time can also be printed. if you execute an ac command without any parameters, the screen will display the total connection time:
$ Ac
Output:
Total 95.08
Display the statistical time of each day:
$ Ac-d
Output:
Nov 1 total 8.65
Nov-2 total 5.70
Nov 3 total 13.43
Nov 4 totally 6.24
Nov 5 total 10.70
Nov 6 total 6.70
Nov 7 total 10.30
.....
..
...
Nov 12 overall 3.42
Nov 13 total 4.55
Today total 0.52
Display the total connection time of each user and the total connection time of all users:
$ Ac-p
Output:
Vivek 87.49
Root 7.63
Total 95.11
4). Find the commands that the user previously executed
You can use the lastcomm command to print the commands executed by the user in the past. You can also use the user name, tty name, or command name to search for previously executed commands.
For example, the command that the vivek user previously executed is displayed:
$ Lastcomm vivek
Output:
[Code]
Userhelper s x vivek pts/0 0.00 secs Mon Nov 13
Userhelper S vivek pts/0 0.00 secs Mon Nov 13
Rpmq vivek pts/0 0.01 secs Mon Nov 13
Rpmq vivek pts/0 0.00 secs Mon Nov 13
Rpmq vivek pts/0 0.01 secs Mon Nov 13
Gcc vivek pts/0 0.00 secs Mon Nov 13
Which vivek pts/0 0.00 secs Mon Nov 13
Bash F vivek pts/0 0.00 secs Mon Nov 13
Ls vivek pts/0 0.00 secs Mon Nov 13
Rm vivek pts/0 0.00 secs Mon Nov 13
Vi vivek pts/0 0.00 secs Mon Nov 13
Ping S vivek pts/0 0.00 secs Mon Nov 13
Ping S vivek pts/0 0.00 secs Mon Nov 13
Ping S vivek pts/0 0.00 secs Mon Nov 13
Cat vivek pts/0 0.00 secs Mon Nov 13
Netstat vivek pts/0 0.07 secs Mon Nov 13
Su S vivek pts/0 0.00 secs Mon Nov 13
[/Code]
Each line of information is printed on the screen. Let's take the first line of output as an example:
Userhelper s x vivek pts/0 0.00 secs Mon Nov 13
[1] [2] Next page