Linux to view the login IP and the corresponding execution command

The Linux server can record the historically executed commands and the IP address of the system that has recently logged in
Use the Last command to find out which IP addresses have recently logged into the system
[root@wl-znddsjk ~]# last
root pts/2 10.254.200.252 Sat Apr 14 15:26 still logged in
root pts/4 10.4.3.76 Fri Apr 13 16:59 - 18:06 (01:07)
root pts/2 10.254.200.252 Fri Apr 13 16:54 - 18:05 (01:11)
root pts/6 10.254.200.252 Fri Apr 13 11:48 - 11:58 (00:09)
root pts/8 10.4.3.76 Fri Apr 13 09:47 - 12:05 (02:17)
root pts/7 10.4.3.47 Fri Apr 13 09:32 - 12:18 (02:46)
root pts/5 10.4.3.238 Fri Apr 13 09:22 - 20:12 (10:50)
root pts/3 10.4.3.226 Fri Apr 13 08:57 - 18:24 (09:27)

The top one still logged in indicates that the current login has not been logged out, and it is still being used.
If we want to see the commands that the server has executed in the past, we can use the histroty command to view
[root@wl-znddsjk ~]# history
4 2018-03-27 15:40:02ls
5 2018-03-27 15:40:10cd ..
6 2018-03-27 15:40:10ls
7 2018-03-27 15:40:22cd /u01
8 2018-03-27 15:40:26cd /usr

如果你的history 命令执行时看不到具体的执行时间,只有执行的命令,你需要进行下面的操作:
1.编辑/etc/profile 文件,再文件最后末尾加上下面代码
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S  "

2.重新登录再执行一些命令,然后再执行history命令你就会看到最新执行的这行命令都有对应的时间
再重新登录之前执行的那些命令,时间显示的都是第一次记录命令执行的时间,而不是这些历史命令真正的执行时间。

我们也可以使用history | grep 来过滤过不想需要看到的命令,比如说我们要看2018年4月14号都执行了哪些操作,使用下面的命令即可
[root@wl-znddsjk ~]# history | grep '2018-04-14'
1000 2018-04-14 15:26:28last
1001 2018-04-14 15:26:33history
1002 2018-04-14 15:28:10last
1003 2018-04-14 15:30:47history
1004 2018-04-14 15:32:32history | grep '2018-04-14'

这样我们结合last 和history命令就可以看到某个具体的ip地址对我们的服务器执行了哪些操作,还是以上面为例,执行last命令显示的第一条数据
root pts/2 10.254.200.252 Sat Apr 14 15:26 still logged in
显示10.254.200.252 这个IP地址是在 15:26登录的,并且还没有退出登录,
那我们使用history | grep 查看得知,在15:26以后执行的命令有
1000 2018-04-14 15:26:28 last
1001 2018-04-14 15:26:33 history
1002 2018-04-14 15:28:10 last
1003 2018-04-14 15:30:47 history

这样我们就可以知道IP地址10.254.200.252这个用户对我们的服务器进行了那些操作



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324427157&siteId=291194637