ssh上で行った作業のログをとる
linuxサーバにログインしていろいろしたとき、翌日になって何かまずいことをしでかした事に気づく(でもログがないから何をしたかわからない)ということがままある。というわけでlinuxサーバにsshでログインして何かしたとき、すべてログに残すノウハウ。もうあちこちで紹介されているけど、自分用メモということで。
~/.bash_profileに下記を追加
ログを保存するディレクトリを作成しておく。
ログは、ホームディレクトリのlogフォルダに生成される。
以上。
ちなみに、~/.bash_profileに書くところ、間違って~/.bashrcに書くと、sshではつながるのにscpで接続するとき失敗する謎の怪現象を引き起こす。
また、この設定を全員に適用したいのであれば、以下のようにする。
~/.bash_profileの代わりに/etc/profileに上記内容を追加。
参考:
~/.bash_profileに下記を追加
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
# keep everything in the log.
P_PROC=`ps aux | grep $PPID | grep sshd | awk '{ print $11 }'`
if [ "$P_PROC" = sshd: ]; then
script ~/log/`date +%Y%m%d-%H%M%S.log`
exit
fi
ログを保存するディレクトリを作成しておく。
mkdir ~/log
ログは、ホームディレクトリのlogフォルダに生成される。
以上。
ちなみに、~/.bash_profileに書くところ、間違って~/.bashrcに書くと、sshではつながるのにscpで接続するとき失敗する謎の怪現象を引き起こす。
Received too large (1581276736 B) SFTP packet. Max supported packet size is 102400 B.
The error is typically caused by message printed from startup script (like .profile). The message may start with "^@^@".
また、この設定を全員に適用したいのであれば、以下のようにする。
[root@localhost ~]# mkdir /etc/skel/log
~/.bash_profileの代わりに/etc/profileに上記内容を追加。
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME
# keep everything in the log.
P_PROC=`ps aux | grep $PPID | grep sshd | awk '{ print $11 }'`
if [ "$P_PROC" = sshd: ]; then
script ~/log/`date +%Y%m%d-%H%M%S.log`
exit
fi
参考:
カテゴリ:
技術メモ
トラックバック(0)
このブログ記事を参照しているブログ一覧: ssh上で行った作業のログをとる
このブログ記事に対するトラックバックURL: http://mogya.com/mt/mt-tb.cgi/488
これ、/etc/profileに書くのはよくないかもしれないなぁ。
scriptでログインスクリプトの実行が止まっちゃうので、~/.bash_profileが聞かなくなるような気がする。どうしたものか。