LoginSignup
3
1

More than 3 years have passed since last update.

historyコマンドに日時を付与し、全ユーザのhistoryファイルをスクリプトで収集する

Last updated at Posted at 2020-06-02

概要

linuxで手取り早く操作ログを取得する方法として、ユーザの~/.bash_historyに日時を付与することで対応しました。

設定

historyコマンドを実行すると過去に実行したコマンドの履歴を見ることが出来ますが、日時がないのでいつ実行されたのか分かりません。
そこで、~/.profileに以下の一文を追記します。

HISTTIMEFORMAT='%y/%m/%d %H:%M:%S ';

~/.profileに記述すると個別アカウントごとに設定出来ますが、今回は全体に適用させたいので/etc/profile.d/history.shに記述します。

設定ファイル 利用法
~/.profile ・ログイン時にそのセッション全体に適用するものを記述する シェルの種類に依存しないものを記述する
~/.bashrc bashでしか使わないものを記述する エイリアス
シェルオプション プロンプト設定 ~/.bash_profile

こちらの記事を参考にしました。
Linux: .bashrcと.bash_profileの違いを今度こそ理解する

取得スクリプト

ユーザごとのファイル名が~/.bash_historyで同じなので、ユーザごとのフォルダを用意した上で以下のようなスクリプトでコピーしてきます。


#!/bin/sh
#user1を作業ユーザとし、コピー前に作業フォルダの前回ファイルを削除する
sudo find /home/user1/log/ -name ".bash_history" -type f -print | xargs rm -f

#user1,user2,user3のファイルをuser1のlogフォルダにコピーする
for a in "user1" "usesr2" "user3" ; do

sudo cp -f /home/$a/.bash_history /home/user1/log/$a/;

done

結果

このような形のファイルが取得出来ます。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1