10.07 在 Linux 上記錄和重放終端會話活動

在 Linux 上記錄和重放終端會話活動

在某些情況下,我們要檢查上一個會話的命令輸出,並希望將其與當前會話進行比較。因此在這種情況下,script 命令就變得很方便。

-- Pradeep Kumar(作者)

通常,Linux 管理員們都使用 history 命令來跟蹤在先前的會話中執行過哪些命令,但是 history 命令的侷限性在於它不存儲命令的輸出。在某些情況下,我們要檢查上一個會話的命令輸出,並希望將其與當前會話進行比較。除此之外,在某些情況下,我們正在對 Linux 生產環境中的問題進行故障排除,並希望保存所有終端會話活動以供將來參考,因此在這種情況下,script 命令就變得很方便。

script 是一個命令行工具,用於捕獲/記錄你的 Linux 服務器終端會話活動,以後可以使用>

安裝>

在 RHEL 7/ CentOS 7 上安裝>

script 命令由 RPM 包 util-linux 提供,如果你沒有在你的 CentOS 7 / RHEL 7 系統上安裝它,運行下面的 yum 安裝它:

[root@linuxtechi ~]# yum install util-linux -y

在 RHEL 8 / CentOS 8 上安裝>

運行下面的 dnf 命令來在 RHEL 8 / CentOS 8 上安裝>

[root@linuxtechi ~]# dnf install util-linux -y

在基於 Debian 的系統(Ubuntu / Linux Mint)上安裝>

運行下面的 apt-get 命令來安裝>

root@linuxtechi ~]# apt-get install util-linux -y

如何使用>

直接使用>

[root@linuxtechi ~]#/>Script started, file is typescript
[root@linuxtechi ~]#

要停止記錄會話活動,請鍵入 exit 命令,然後按回車:

[root@linuxtechi ~]# exit
exit
Script done, file is typescript
[root@linuxtechi ~]#

script 命令的語法格式:

~] #/>

能在>


在 Linux 上記錄和重放終端會話活動

options-script-command


讓我們開始通過執行>


在 Linux 上記錄和重放終端會話活動

script-examples-linux-server


正如我們在上面看到的,終端會話日誌保存在文件 typescript 中:

現在使用 cat / vi 命令查看 typescript 文件的內容,

[root@linuxtechi ~]# ls -l typescript
-rw-r--r--. 1 root root 1861 Jun 21 00:50 typescript
[root@linuxtechi ~]#


在 Linux 上記錄和重放終端會話活動

typescript-file-content-linux


以上內容確認了我們在終端上執行的所有命令都已保存在 typescript 文件中。

在>

假設我們要使用自定義文件名來執行>

[root@linuxtechi ~]#/>Script started, file is sessions-log-21-06-2019-01:37:39.txt
[root@linuxtechi ~]#

現在運行該命令並輸入 exit:

[root@linuxtechi ~]# exit
exit
Script done, file is sessions-log-21-06-2019-01:37:39.txt
[root@linuxtechi ~]#

附加命令輸出到>

假設>

[root@linuxtechi ~]#/>Script started, file is sessions-log.txt
[root@linuxtechi ~]# xfs_info /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2746624 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=10986496, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=5364, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@linuxtechi ~]# exit
exit
Script done, file is sessions-log.txt
[root@linuxtechi ~]#

要查看更新的會話記錄,使用 cat session-log.txt 命令。

無需 shell 交互而捕獲命令輸出到>

假設我們要捕獲命令的輸出到會話記錄文件,那麼使用 -c 選項,示例如下所示:

[root@linuxtechi ~]#/>Script started, file is root-session.txt
01:57:40 up 2:30, 3 users, load average: 0.00, 0.01, 0.05
linuxtechi
Fri Jun 21 01:57:40 EDT 2019
Script done, file is root-session.txt
[root@linuxtechi ~]#

以靜默模式運行>

要以靜默模式運行>

[root@linuxtechi ~]#/> 02:01:10 up 2:33, 3 users, load average: 0.00, 0.01, 0.05
Fri Jun 21 02:01:10 EDT 2019
[root@linuxtechi ~]#

要將時序信息記錄到文件中並捕獲命令輸出到單獨的文件中,這可以通過在>

語法格式:

~ ]#> {file_name}
[root@linuxtechi ~]#/>Script started, file is session.log
[root@linuxtechi ~]# uptime
02:27:59 up 3:00, 3 users, load average: 0.00, 0.01, 0.05
[root@linuxtechi ~]# date
Fri Jun 21 02:28:02 EDT 2019
[root@linuxtechi ~]# free -h
total used free shared buff/cache available
Mem: 3.9G 171M 2.0G 8.6M 1.7G 3.3G
Swap: 3.9G 0B 3.9G
[root@linuxtechi ~]# whoami
root
[root@linuxtechi ~]# exit
exit
Script done, file is session.log
[root@linuxtechi ~]#
[root@linuxtechi ~]# ls -l session.log timing.txt
-rw-r--r--. 1 root root 673 Jun 21 02:28 session.log
-rw-r--r--. 1 root root 414 Jun 21 02:28 timing.txt
[root@linuxtechi ~]#
/<timing-file-name>

重放記錄的 Linux 終端會話活動

現在,使用>

注意:scriptreplay 也由 RPM 包 util-linux 提供。scriptreplay 命令需要時序文件才能工作。

[root@linuxtechi ~]#/>

上面命令的輸出將如下所示,


在 Linux 上記錄和重放終端會話活動


記錄所有用戶的 Linux 終端會話活動

在某些關鍵業務的 Linux 服務器上,我們希望跟蹤所有用戶的活動,這可以使用>

[root@linuxtechi ~]# vi /etc/profile
……………………………………………………
if [ "x$SESSION_RECORD" = "x" ]

then
timestamp=$(date +%d-%m-%Y-%T)
session_log=/var/log/session/session.$USER.$$.$timestamp
SESSION_RECORD=started
export SESSION_RECORD
script -t -f -q 2>${session_log}.timing $session_log
exit
fi
……………………………………………………

保存文件並退出。

在 /var/log 文件夾下創建 session 目錄:

[root@linuxtechi ~]# mkdir /var/log/session

給該文件夾指定權限:

[root@linuxtechi ~]# chmod 777 /var/log/session/
[root@linuxtechi ~]#

現在,驗證以上代碼是否有效。在我正在使用 pkumar 用戶的情況下,登錄普通用戶到 Linux 服務器:

~ ] # ssh root@linuxtechi
root@linuxtechi's password:
[root@linuxtechi ~]$ uptime
04:34:09 up 5:06, 3 users, load average: 0.00, 0.01, 0.05
[root@linuxtechi ~]$ date
Fri Jun 21 04:34:11 EDT 2019
[root@linuxtechi ~]$ free -h
total used free shared buff/cache available
Mem: 3.9G 172M 2.0G 8.6M 1.7G 3.3G
Swap: 3.9G 0B 3.9G
[root@linuxtechi ~]$ id
uid=1001(pkumar) gid=1002(pkumar) groups=1002(pkumar) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@linuxtechi ~]$ whoami
pkumar
[root@linuxtechi ~]$ exit
Login as root and view user’s linux terminal session activity
[root@linuxtechi ~]# cd /var/log/session/
[root@linuxtechi session]# ls -l | grep pkumar

-rw-rw-r--. 1 pkumar pkumar 870 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05
-rw-rw-r--. 1 pkumar pkumar 494 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05.timing
[root@linuxtechi session]#


在 Linux 上記錄和重放終端會話活動

Session-output-file-linux


我們還可以使用>

[root@linuxtechi session]#/>

以上就是本教程的全部內容,請在下面的評論部分中分享你的反饋和評論。


via: https://www.linuxtechi.com/record-replay-linux-terminal-sessions-activity/

作者: Pradeep Kumar 選題: lujun9972 譯者: wxy 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


分享到:


相關文章: