로그 파일이 너무 커지는 것을 방지하고, 일정 기간 동안 보관하기 위해 logrotate를 설정하는 방법을 설명합니다.
🛠 1. logrotate 설치 여부 확인
logrotate가 설치되어 있는지 확인하려면 아래 명령어를 실행하세요.
which logrotate
설치되어 있지 않다면, 다음 명령어로 설치합니다.
sudo yum install logrotate -y
📝 2. 로그 로테이션 설정 파일 생성
로그 로테이션 설정 파일은 /etc/logrotate.d/ 디렉터리에 개별 파일로 저장됩니다.
다음 명령어를 실행하여 설정 파일을 생성합니다.
sudo nano /etc/logrotate.d/daemon
✅ 아래 내용을 입력하세요.
/var/log/daemon.log {
daily # Rotate logs daily
rotate 7 # Keep the last 7 rotated log files
compress # Compress old log files using gzip (.gz)
delaycompress # Do not compress the most recent rotated file
missingok # Do not show an error if the log file is missing
notifempty # Do not rotate the log file if it is empty
create 0640 root adm # Set file permissions and ownership after rotation
postrotate
systemctl restart rsyslog >/dev/null 2>&1 || true # Restart rsyslog after rotation
endscript
}
🔄 3. 설정 테스트
설정이 올바르게 적용되었는지 확인하려면 다음 명령어를 실행하세요.
sudo logrotate -d /etc/logrotate.d/daemon
🔍 정상적인 출력 예시:
rotating pattern: /var/log/daemon.log after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/daemon.log
log does not need rotating (log has been already rotated)
이 메시지가 나오면 설정이 정상적으로 적용된 것입니다.
🚀 4. 로그 강제 회전 테스트
로그가 정상적으로 회전되는지 확인하려면 강제로 실행할 수 있습니다.
sudo logrotate -f /etc/logrotate.d/daemon
이후 로그 파일이 회전되었는지 확인합니다.
ls -lh /var/log/daemon*
🔍 정상적인 출력 예시:
-rw-r----- 1 root adm 5.6K Feb 14 12:00 daemon.log
-rw-r----- 1 root adm 4.3K Feb 13 12:00 daemon.log.1
-rw-r----- 1 root adm 3.1K Feb 12 12:00 daemon.log.2.gz
...
.log.1, .log.2.gz 형식으로 회전되었다면 정상적으로 동작하는 것입니다.
⏳ 5. 자동 실행 확인
CentOS 7에서는 logrotate가 cron.daily를 통해 자동 실행됩니다.
아래 명령어로 스케줄이 설정되어 있는지 확인하세요.
ls -l /etc/cron.daily/logrotate
또는 logrotate가 자동 실행되고 있는지 크론 로그를 확인할 수 있습니다.
grep logrotate /var/log/cron
만약 자동 실행이 설정되지 않았다면, crontab에 추가합니다.
sudo crontab -e
다음 줄을 추가하세요.
0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf
이 설정은 매일 자정(00:00) 에 logrotate를 실행하도록 합니다.
🎯 최종 정리
✔ logrotate를 설치하고 /etc/logrotate.d/daemon에 설정 파일을 생성
✔ 로그가 매일 회전(daily) 하고 최근 7개 보관하도록 설정
✔ logrotate -d로 설정 테스트 후, logrotate -f로 강제 실행 테스트
✔ 크론 로그 확인 (grep logrotate /var/log/cron)
이제 자동으로 /var/log/daemon.log가 관리될 거야! 🚀😎
추가로 궁금한 점 있으면 편하게 물어봐!