본문 바로가기
리눅스

[Linux] 로그 기본 개념 설명

by 공대냥이 2018. 12. 12.
반응형

로그 기록 용도

    • 사용자 및 서버의 활동기록
    • 시스템 공격에 대한 흔적 기록
    • 서버 장애에 대한 흔적 기록
    • 로그 기록을 통한 성능 카운트

 

로그의 위치

    • /var/log 일부 BSD 계열, FreeBSD, Linux
    • /var/run 일부 Linux 
 
​syslog 체계 
 
리눅스 운영체제에서 메세지 생성에 관해서는 syslogd 데몬에 의해서 총괄 관리 되고 있다.
 

/etc/syslog.conf 파일에는 어디에서 메세지가 생성이 되면 어디에 메세지를 남겨라라고 설정 되어 있다. 

그럼, syslogd 데몬은 /etc/syslog.conf 파일에 설정된대로 생성된 메세지를 적당한 곳에 기록하게 된다

 

/etc/syslog.conf 파일

 

(/etc/syslog.conf 파일의 필드 구분)

 

Selector [TAB/SPACE] Action

Facility.Level [TAB/SPACE] Action

EX) user.notice /var/log/test.log

 

Facility : 메세지의 종류

Level : 메세지의 난이도(위험 수준)

 

Action : 메세지 기록 위치

 

(1-1). 메세지 종류(Facility)

 

EX) user.notice /var/log/test.log

 

메세지 종류 설 명
authpriv 개인 인증을 요하는 프로그램 유형이 발생한 메세지(EX: su, telnet, ssh)
cron crontab(crond), at(atd) 프로그램이 발생한 메세지(EX: crontab, at)
daemon 시스템 데몬이 생성한 메세지. (EX: telnetd, ftpd)
kern 커널이 발생한 메세지
lpr 프린트 유형의 프로그램이 발생한 메세지
mail 메일시스템이 발생한 메세지.
mark syslogd 데몬에 의해 생성된 일정한 시간간격의 반복적인 메세지
news 유즈넷 뉴스 프로그램 유형이 발생한 메세지
security(auth) 인증 프로그램이 생성한 메세지(EX: login)
syslog syslog 프로그램이 생성한 메세지(EX: syslogd)
user 사용자 프로그램에 의해 생성된 메세지(EX: top, system-config-*)
uucp UUCP(Unix to Unix Copy) 시스템이 발생한 메세지
local0-7 예약된 메세지 종류, 여분으로 남겨둔 유형(EX: local0, local1, local2, ...)
* 모든 메세지(, mark 메세지 종류는 제외)

 

 

(1-2). 메세지 난이도(Level)

 

메세지의 난이도(위험수위)를 가리키며 설정된 난이도 이상을 나타낸다.

 

메세지 난이도 앞에 '='를 사용하는 경우 해당 메세지 난이도의 위험도와 같은 경우를 의미한다.

 

EX) user.notice /var/log/test.log

 

메세지 레벨 설 명
0 emerg (panic) 패닉상태, 모든 사용자에게 전달되어야 할 위험한 상황의 메세지
1 alert 즉각적인 조치를 위해야 상황의 메세지
2 crit 시스템에 문제가 생기는 단계의 메세지
3 error (err) 에러가 발생하는 경우의 메세지
4 warning (warn) 주의를 요하는 경고 메세지
5 notice 특별한 주의를 요하는 에러는 아닌 메세지
6 info 통계, 기본정보 메세지
7 debug 프로그램을 디버깅할 때 생성되는 메세지
8 none facility 뒤에 .none을 붙이면 해당 facility를 제외하겠다는 의미이다. 
예를 들어 mail.none은 메일 관련 메세지를 제외한다는 뜻이다.

 

 

 

(1-3). 메세지 기록 위치(Action)

 

EX) user.notice /var/log/test.log

 

메세지 기록 위치 설 명
/var/log/messages 기록을 남길 특정한 로그 파일 선택(: /var/log/file.log)
/dev/console 기록을 남길 콘솔에 로그 기록 남김(: /dev/console)
user01,root, * 특정한 사용자나 모든 사용자 선택(: user01, *)
@hostA, @172.16.8.254 다른 호스트 선택(: @172.16.8.254)-> 보통 로그 서버 선택

 

logger 명령어 

 

  • 임의의 메세지를 생성시키는 역할을 가진다.
  • 주로 테스트 용도로 사용된다.
  • /etc/syslog.conf 파일에 새로운 내용을 추가하고 설정이 잘 동작하는가를 테스트 할 때 사용.

(명령어 형식)

# logger "System rebooted" (# logger -p user.notice "System rebooted")

 

Default Value: user.notice

 

 

# logger -p user.err "System rebooted" (-p : priority)

# logger -p local0.notice "System Messages"

 

 

 

[참고] 로그 서버에 모든 기록 남기기
 (모든 기록을 로그 서버로 넘기는 경우)
(Log Client)
# mv /etc/syslog.conf /etc/syslog.conf.orig
# vi /etc/syslog.conf
*.* @172.16.9.252
# service syslog restart
 
 
 (모든 기록을 로그 서버로 넘기는 경우)
(Log Client)
# cp /etc/syslog.conf /etc/syslog.conf.orig
# vi /etc/syslog.conf
*.info;mail.none;news.none;authpriv.none;cron.none @172.16.9.252
authpriv.* @172.16.9.252
mail.* @172.16.9.252
cron.* @172.16.9.252
# service syslog restart
 
 
(Log Server)
-> 기본설정
 
 
 
 
 
 
 
(Log Server)
-> 기본 설정
 
 
 
 (일부 기록만 로그 서버로 넘기는 경우)
(Log Client)
# cp /etc/syslog.conf /etc/syslog.conf.orig
# vi /etc/syslog.conf
...... (중략) .....
*.warning @172.16.9.252
# service syslog restart
(Log Server)
# vi /etc/syslog.conf
...... (중략) .....
*.warning /var/log/warn.log
# service syslog restart

 


 

[참고] syslogd 데몬의 포트 번호 확인
# cat /etc/services | grep syslog
syslog 514/udp
syslog-conn 601/tcp # Reliable Syslog Service
syslog-conn 601/udp # Reliable Syslog Service
 
# netstat -anu | grep :514
udp 0 0 0.0.0.0:514 0.0.0.0:* 

 

 

로그 파일 관리

 

-OS 로그        -> logrotate CMD

-Service 로그  -> 각 서비스에서 관리 + 수동 관리

 

logrotate 명령어

/etc/logrotate.conf 파일

 

 

cat /etc/logrotate.conf

 

# see "man logrotate" for details

# rotate log files weekly

weekly

특별히 명시하지 않은 로그 파일에 대해서는 일주일(weekly)마다 rotate 한다.

 

# keep 4 weeks worth of backlogs

rotate 4

최대 4번까지 rotate를 허용한다.(EX: logfile, logfile.1, logfile.2, logfile.3, logfile.4)

 

# create new (empty) log files after rotating old ones

create

로그 파일을 rotate 한 후에 비어 있는 로그 파일을 생성한다.

 

# uncomment this if you want your log files compressed

#compress

로그 파일을 압축하는 옵션이다. 기본값은 활성화 되어 있지 않다. 용량 문제에 지장이 없다면 압축하지 않을 것을 권장한다.

 

# RPM packages drop log rotation information into this directory

include /etc/logrotate.d

대부분의 RPM 패키지로 설치되는 데몬들은 이 디렉토리에 로그 파일 정책 파일이 생성된다. 

각각의 로그 파일을 rotate 시킬수 있도록 하는 설정이다.

# no packages own wtmp -- we'll rotate them here

/var/log/wtmp {

monthly

minsize 1M

create 0664 root utmp

rotate 1

}

로그 파일(wtmp)은 어떤 패키지에 의해서도 설정되지 않기 때문에 따로 설정한다. 

다른 로그 파일들은 /etc/logrotate.d 내의 파일들에 모두 각각 설정하고 있다. 

한달마다 rotate 하며, 최대 1회까지만 rotate 한다. 관련 파일인 /var/log/utmp 파일을 664 퍼미션을 가지고 소유자는 root로 생성한다.

 

# system-specific logs may be also be configured here.

시스템에서 특별하게 지정하고 싶은 로그 파일들이 있다면 여기에 정의한다.

/var/log/lastlog {

monthly

rotate 3

}

한달마다 rotate 하며 최대 3회가지 rotate 한다.

 

반응형