리눅스 Logrotate 개념과 설정

리눅스 Logrotate는 “로그를 회전하다.”라는 뜻에서 알 수 있듯이 로그와 Rotate(회전하다[동사])의 합성어입니다. 리눅스 설치 시 기본으로 설치되어 있습니다.

로테이션(Rotation)은 사람을 일정한 순서에 따라 교대하는 일, 야구 경기에서 선발 투수를 차례로 기용하는 일 등의 예에서 볼 수 있듯이 반복교대의 개념을 가지고 있습니다.
리눅스에서 로그로테이션(Logrotation) > 로그 회전이라는 명사로도 언급됩니다.
rpm -qa | grep logrotate
logrotate-3.18.0-7.el9.x86_64
rsyslog-logrotate-8.2102.0-105.el9.x86_64

Logrotate가 설치되어 있지 않다면 명령어를 통해서 설치할 수 있습니다.

yum -y install logrotate
dnf -y install logrotate

✅ 리눅스 Logrotate란

리눅스 로그회전(Logrotation)은 리눅스의 로그 정보를 저장하는 가장 기본적인 파일입니다. 로그를 그대로 방치하게 되면 파티션 디스크의 용량이 가득 차거나 웹사이트에 404 에러가 발생하는 등의 문제가 발생할 수 있습니다.

1. 로그의 필요성

  • 운영
  • 시스템 장애가 발생했을 경우 장애 해결을 위해 로그를 확인
  • 시스템의 성능 개선
  • 보안
  • 허가 되지 않은 봇이나 해킹 시도 등에 대한 감사
  • 침해 사고 발생 시 원인과 경로 및 피해 상황 파악

2. 일반적인 리눅스 로그 저장 기간

보통 리눅스 뿐만 아니라 로그의 저장 기간은 길게 3년, 1년, 6개월, 3개월 단위로 설정할 수 있으며, 짧게는 최소 1개월(4주) 단위로 로테이션 할 수 있습니다. 디스크 용량이 크지 않더라도 1개월 단위로 저장하는 것이 최소 선택이라고 할 수 있습니다. 그 외에 디스크 저장 공간이 여유롭지 않을 경우 그 이하로 설정할 수 있습니다.

✅ 리눅스 Logrotate 설정

Logrotate는 기본 설정 파일 /etc/logrotate.conf와 /etc/logrotate.d/ 폴더 내에 프로세스 설정 파일로 나뉘게 됩니다.

  • /usr/sbin/logrotate: 데몬 프로그램
  • /etc/logrotate.conf: 데몬 설정 파일
  • /etc/logrotate.d/: 로그로테이트 프로세스 설정 파일
  • /etc/cron.daily/logrotate : 로그로테이트 작업 내역에 대한 히스토리 로그

1. /etc/logrotate.conf

  • 기본 설정 값입니다.
# see "man logrotate" for details

# global options do not affect preceding include directives

# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

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

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may be also be configured here.
  • weekly
  • rotate log files에 대한 yearly, monthly, weekly, daily로 주기 옵션을 설정할 수 있습니다.
  • rotate
  • 로그 파일의 갯수가 설정된 숫자 이상일 경우 가장 오래된 로그 파일을 삭제합니다.
  • ex) rotate 7 : log파일이 7개 이상 되면 삭제
  • create
  • 생성되는 로그 파일의 [권한], [유저], [그룹]을 설정할 수 있습니다.
  • ex) create 644 root root
  • compress
  • 로테이트 되는 로그 파일을 gzip으로 압축합니다.
  • nocompress gzip 압축 없이 설정
  • dateext
  • 로그 백업 파일 이름에 날짜가 들어가도록 설정
  • include /etc/logrotate.d
  • 로테이트 설정 값이 있는 디렉토리입니다.
  • maxage [숫자]
  • maxage 30 :  log파일이 30일 이상 되면 삭제
  • size : 지정된 용량보다 클 경우 rotate 실행
  • ex) size +100k

2. /etc/logrotate.d/

/etc/logrotate.d/ 폴더의 기본 구성은 다음과 같습니다.(록키 리눅스 기준)

✅ logrotate 명령어 및 옵션(환경)

# 기본 명령어
logrotate -옵션 [파일명]
# 명령어 위치
/usr/sbin/logrotate
# 리눅스 logrotate 디버깅 실행 : logrotate -d
logrotate -d /etc/logrotate.d/httpd 
WARNING: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/httpd
Reading state from file: /var/lib/logrotate/logrotate.status
error: error opening state file /var/lib/logrotate/logrotate.status: No such file or directory
Allocating hash table for state file, size 64 entries

Handling 1 logs

rotating pattern: /var/log/httpd/*log  1048576 bytes (no old logs will be kept)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
Creating new state
  Now: 2024-01-13 17:28
  Last rotated at 2024-01-13 17:00
  log does not need rotating (log size is below the 'size' threshold)
considering log /var/log/httpd/error_log
Creating new state
  Now: 2024-01-13 17:28
  Last rotated at 2024-01-13 17:00
  log does not need rotating (log size is below the 'size' threshold)
not running postrotate script, since no logs were rotated
  • 로그로테이트 실행
  • logrotate -f [파일명]
logrotate -v /etc/logrotate.d/httpd 

reading config file /etc/logrotate.d/httpd
Reading state from file: /var/lib/logrotate/logrotate.status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /var/log/httpd/*log  1048576 bytes (no old logs will be kept)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
  Now: 2024-01-13 17:31
  Last rotated at 2024-01-13 17:30
  log does not need rotating (log size is below the 'size' threshold)
considering log /var/log/httpd/error_log
  Now: 2024-01-13 17:31
  Last rotated at 2024-01-13 17:30
  log does not need rotating (log size is below the 'size' threshold)
not running postrotate script, since no logs were rotated
set default create context to unconfined_u:object_r:logrotate_var_lib_t:s0
리눅스Linux(회색 배경의 나무 큐브)

Similar Posts

  • [Ubuntu] 우분투 방화벽 UFW 설정

    우분투 설치 후 기본 방화벽은 UFW(Uncomplicated Firewall)입니다. iptables의 방화벽 설정보다 간소하게 개발된 ufw는 IPv4 또는 IPv6 호스트 기반 방화벽을 사용자 친화적으로 사용할 수 있는 것이 우분투 방화벽의 사용 목적입니다. Ⅰ. 기본 구문 ✅ UFW 활성화 / 비활성화 / 상태 확인 🔲 활성화 🔲 상태 확인 활성화 후에 상태 값을 확인합니다. verbose 옵션 사용 시…

  • 리눅스 Rsync 명령어

    Rsync (Remote Sync) 는 네트워크를 통해 파일과 디렉터리를 효율적으로 동기화하는 백업 방식입니다. 전체 데이터를 매번 복사하는 대신 변경된 부분만 증분 방식으로 전송하여 빠르고 안정적인 동기화를 지원합니다. 또한 다양한 옵션을 통해 압축, 암호화, 권한 유지, 삭제 동기화 등 세밀한 제어가 가능하며, 백업과 서버 간 데이터 전송에 널리 사용됩니다. 특히 네트워크 대역폭을 절약하고, 재 전송…

  • Apache MPM Module: Prefork, Worker, Event 3가지 방식 비교 및 Event Module 최적화 설정

    Apache MPM Module은 Apache HTTP Server의 중요한 부분으로, 서버의 요청 처리 방식을 결정합니다. MPM(Multi-Processing Module)은 서버의 성능과 확장성에 큰 영향을 미치는 역할을 합니다. ✅ MPM Module(Multi-Processing Module) MPM 모듈은 서버가 요청을 처리하는 방식을 정의하며, 서버의 성능, 안정성, 확장성에 영향을 미칩니다. 주요 MPM 모듈로는 Prefork, Worker, Event가 있습니다. 확장성과 성능이 중요한 사이트는 Worker MPM을…

  • yum 명령어 | 사용법 | 옵션 | yum-utils | 패키지 관리와 저장소 관리🔝

    레드햇 계열의 리눅스 배포판에서 사용하는 YUM 명령어는 패키지 관리(설치, 업데이트, 제거)를 하는데 사용하는 명령어입니다. yum은 기존 패키지 시스템인 RPM의 의존성 문제를 해결하기 위해 제공되는 도구이며, RPM에서 수행할 수 있는 많은 작업을 수행할 수 있습니다. ✅ YUM 명령어 yum 명령어의 기본 사용 방법은 다음과 같습니다. ✅ YUM 명령어 옵션 ✅ YUM 패키지 관리: 확인 및…

  • [Linux] Let’s Encrypt 와일드카드 인증서 설치(Certbot, Feat. Cloudflare)

    그동안 let’s Encrypt 일반 인증서를 Certbot 설치 후 사용하다가 Let’s Encrypt 와일드카드 인증서를 설치해서 사용해야 겠다는 생각을 하게 되었습니다. 현재 서버는 LightSail Alma Linux apache로 구동되는 서버인데 차후 상황을 봐서 Open Lightspeed 서버로 변경을 고려중이지만 한참 뒤가 될 것 같습니다. 현재는 apache 기준으로 Let’s Encrypt 와일드카드 인증서를 설치합니다. 와일드카드 SSL 설치 조건 중 하나가 Certbot의…

  • [Linux] lsof 명령어: 실행 중 파일 정보 출력

    lsof 명령어는 lisf open files의 약자로 리눅스 시스템에서 열려 있는 파일의 정보를 출력해 주는 명령어입니다. lsof 명령어는 파일을 포함하여 디렉터리, 네트워크 소켓, 파이프 등 프로세스가 사용 중인 다양한 리소스도 조회할 수 있습니다. ✅ lsof 명령어 기본 구문 명령어만 입력 했을 때는 현재 시스템에서 열린 모든 파일과 관련된 프로세스 정보를 출력합니다. ✅ lsof 명령어…

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

Prove your humanity: 2   +   2   =