log 관리

yoni·2023년 4월 13일

Linux

목록 보기
4/16

journal log

> 옵션없이 journalctl 을 실행하면 systemd 의 로그를 볼 수 있다.
journalctl 

> -n 옵션:  최근 10 개 메시지만 표시
journalctl -n 10

> -f 옵션: 마지막 로그 내용을 보여준 후에 이후에 변경되는 로그 파일의 내용을 계속 출력
journalctl -f

> -u 옵션 : crond 관련 journal log만 출력
journalctl -u crond

> service name 어떻게 알까? 특정 daemon에 대한 정보 확인
systemctl -t service

> 2023-04-13 13:01:00 부터 now까지의 journal log만 보고 싶다.
(기존 syslog, rsyslog는 이런 기능이 없다.)
journalctl --since "2023-04-13 13:01:00" --until now


> -p 옵션: priorities (level 상태) 
즉, 특정 레벨에 해당하는 log를 보고자 할 때 사용 (level이 올라갈수록 emergency한 것)


> error level에 해당하는 log들만 보자.
journalctl -p 3

journal log를 영구적으로 저장하는 법

journal log는 휘발성으로 재부팅하면 날라가게 된다.
영구적으로 어떻게 저장할까?

[root@station14 log]# mkdir -p /var/log/journal/
[root@station14 log]# systemctl restart systemd-journald

> 영구적으로 저장하는거니까 이 위치에 더이상 journal로그가 필요 없다.
[root@station14 log]# ls /run/log/journal
ls: cannot access /run/log/journal: No such file or directory

> mkdir한 곳 파일에 영구적으로 journal로그가 저장됨
[root@station14 log]# ll /var/log/journal/
total 0
drwxr-xr-x. 2 root root 28 Apr 13 14:05 2544d59d80f44d02b6ae0264364abe2e

Detecing Log Modification with Log Sealing

sealing이 손상됨 == 누군가 log에 접근함.
sealing 기능 사용하려면 journal log는 영구적으로 저장되어 있어야 한다.

[root@station14 log]# journalctl --setup-keys --interval=60m
Generating seed...
Generating key pair...
Generating sealing key...
Failed to set file attributes: Operation not supported

The new key pair has been generated. The secret sealing key has been written to
the following local file. This key file is automatically updated when the
sealing key is advanced. It should not be used on multiple hosts.

	/var/log/journal/2544d59d80f44d02b6ae0264364abe2e/fss

Please write down the following secret verification key. It should be stored
at a safe location and should not be saved locally on disk.

	c45c92-f2860d-6d83c8-6d67fe/72065-d693a400 	--sealing key (빨간 key값)을 통해 검증할 수 있다. 해당 sealing key는 안전하게 보관해두자.

The sealing key is automatically changed every 1h.

The keys have been generated for host station14.example.com/2544d59d80f44d02b6ae0264364abe2e.

To transfer the verification key to your phone please scan the QR code below:


> 검증
[root@station14 log]# journalctl --verify --verify-key=c45c92-f2860d-6d83c8-6d67fe/72065-d693a400
PASS: /var/log/journal/2544d59d80f44d02b6ae0264364abe2e/system.journal  

syslog

udp 프로토콜 사용 -> log손상 발생

rsyslog

[root@station14 log]# vi /etc/rsyslog.conf

     46 #### RULES ####
     47 
     48 # Log all kernel messages to the console.
     49 # Logging much else clutters up the screen.
     50 #kern.*                                                 /dev/console          # kernel log messa        ge save to console
     51 
     52 # Log anything (except mail) of level info or higher.
     53 # Don't log private authentication messages!
     54 *.info;mail.none;authpriv.none;cron.none                /var/log/messages     # mail, auth, cron         log to save here
     55 
     56 # The authpriv file has restricted access.
     57 authpriv.*                                              /var/log/secure       #about authentific        ation
     58 
     59 # Log all the mail messages in one place.
     60 mail.*                                                  -/var/log/maillog
     61 
     62 *.*                                                     /var/log/testlog.txt
     63 # Log cron stuff
     64 cron.*                                                  /var/log/cron
     65 
     66 # Everybody gets emergency messages
     67 *.emerg                                                 :omusrmsg:*   #output module (om) + usrm        sg (module name)
     68 
     69 # Save news errors of level crit and higher in a special file.
     70 uucp,news.crit                                          /var/log/spooler
     71 
     72 # Save boot messages also to boot.log
     73 local7.*                                      

[root@station14 log]# systemctl restart rsyslog
[root@station14 log]# ll /var/log/testlog.txt 
-rw-------. 1 root root 5004 Apr 13 14:52 /var/log/testlog.txt

Logrotate

로그 파일의 순환- 로그 파일의 크기가 과도하게 커지지 않도록 제한

  • logrotate 유틸리티를 사용하여 행해짐, cron에 의해 하루에 한 번씩 실행됨
  • logrotate는 /etc/logrotate.conf로 한 번에 처리방식을 지정하거나
  • /etc/logrotate.d에서 각 로그 파일별 개별로 설정해 줄 수도 있다.

[root@station14 log]# vi /etc/logrotate.conf 
      1 # see "man logrotate" for details
      2 # rotate log files weekly
      3 weekly
      4 
      5 # keep 4 weeks worth of backlogs
      6 rotate 4    --weekly이므로, 4주마다 오래된거 자르고 새로 추가
      7 
      8 # create new (empty) log files after rotating old ones
      9 create		--기간지난거 삭제하고 새로 만들겠다.
     10 
     11 # use date as a suffix of the rotated file
     12 dateext		--log 파일 이름 뒤에 붙이겠다.
     13 
     14 # uncomment this if you want your log files compressed
     15 #compress	--압축하지 않겠다.
     16 
     17 # RPM packages drop log rotation information into this directory
     18 include /etc/logrotate.d
     19 
     20 # no packages own wtmp and btmp -- we'll rotate them here
     21 /var/log/wtmp {
     22     monthly
     23     create 0664 root utmp
     24         minsize 1M
     25     rotate 1
     26 }
     27 
     28 /var/log/btmp {
     29     missingok	--btmp 파일이 없어도 error메시지 보내지말고 rotation시켜라
     30     monthly
     31     create 0600 root utmp
     32     rotate 1
     33 }
     34
     35 /var/log/testlog.txt { 		--추가
     36     weekly
     37     rotate 2
     38     create 0600 root hdmi
     39     size +100
     40     compress
     41 }
     42 
     43 # system-specific logs may be also be configured here.
     

> 매일 crone을 통해 logrotate 수행
[root@station14 log]# cat -n /etc/cron.daily/logrotate
     1	#!/bin/sh
     2	
     3	/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
     4	EXITVALUE=$?
     5	if [ $EXITVALUE != 0 ]; then
     6	    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
     7	fi
     8	exit 0
     
> 수동으로 logrotate 실행해보자.
[root@station14 log]# logrotate /etc/logrotate.conf
[root@station14 log]# ll /var/log/testlog*
-rw-------. 1 root hdmi    0 Apr 13 15:07 /var/log/testlog.txt
-rw-------. 1 root root 1333 Apr 13 15:01 /var/log/testlog.txt-20230413.gz
   
    

국내타임서버리스트

http://time.ewha.or.kr/domestic.html

profile
study log

0개의 댓글