본문으로 건너뛰기

[L] history 명령어에 시간정보 포함하여 출력하기

💡 요약 정리

  • CentOS 5.x 또는 CentOS 6.x 환경에서 동일하게 적용할 수 있습니다.
  • history 명령어에 시간정보를 출력하려면 서버 시간을 먼저 정확히 설정해야 합니다.
  • /etc/profile 파일에 HISTTIMEFORMAT을 설정하고 적용하면 됩니다.

1. 설치 환경

  • CentOS 5.x x86 (64bit) / 6.x 환경도 동일함

2. 서버 시간 맞추기

history 명령어에 시간을 출력하기 앞서 현재 서버 시간을 정확하게 맞춰 줍니다.

[root@localhost /]# rdate -s time.bora.net

3. /etc/profile 파일 수정

/etc/profile 파일의 가장 하단에 다음과 같이 추가합니다.

[root@localhost /root]# vi /etc/profile

내용 추가 예시는 다음과 같습니다:

.
.
.
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
    INPUTRC=/etc/inputrc
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . $i
        else
            . $i >/dev/null 2>&1
        fi
    fi
done

unset i
unset pathmunge

# Add timestamp to .bash_history                   <------ 추가합니다
HISTTIMEFORMAT="%Y-%m-%d [%H:%M:%S] "
export HISTTIMEFORMAT

변경사항 적용하기

제 로그인 과정 없이 바로 적용될 수 있도록 /etc/profile을 다시 로딩합니다.

[root@localhost /]# source /etc/profile

4. history 명령어로 날짜 출력 확인

아래는 적용 전과 적용 후의 예시입니다.

# 적용 전 #
[root@localhost /]# history
  150  service httpd restart
  151  service httpd restart
  152  curl --head localhost
  153  ll
  154  vi httpd.conf
  155  cd /etc/httpd/conf
  156  ll
  157  cd /etc/rc.d/init.d/httpd restart
  158  /etc/rc.d/init.d/httpd restart
  159  curl 127.0.0.1
  160  curl --head 127.0.0.1
  161  date --date '5 months 3 day'
  162  ll
  163  history
  164  ll
  165  clear
  166  vim /etc/profile
  167  history
# 적용 후 #
[root@localhost /]# history
  150  2013-01-15 [18:11:07] service httpd restart
  151  2013-01-15 [18:11:07] service httpd restart
  152  2013-01-15 [18:11:07] curl --head localhost
  153  2013-01-15 [18:11:07] ll
  154  2013-01-15 [18:11:07] vi httpd.conf
  155  2013-01-15 [18:11:07] cd /etc/httpd/conf
  156  2013-01-15 [18:11:07] ll
  157  2013-01-15 [18:11:07] cd /etc/rc.d/init.d/httpd restart
  158  2013-01-15 [18:11:07] /etc/rc.d/init.d/httpd restart
  159  2013-01-15 [18:11:07] curl 127.0.0.1
  160  2013-01-15 [18:11:07] curl --head 127.0.0.1
  161  2013-01-15 [18:11:07] date --date '5 months 3 day'
  162  2013-01-15 [18:11:07] ll
  163  2013-01-15 [18:11:07] history
  164  2013-01-15 [18:11:08] ll
  165  2013-01-15 [18:11:10] clear
  166  2013-01-15 [18:13:40] vim /etc/profile
  167  2013-01-15 [18:17:36] history