리눅스 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

  • 리눅스 wget 명령어

    wget 명령어는 웹의 다운로드 링크를 리눅스 커멘드라인에서 실행해 다운로드를 할 수 있는 명령어입니다. HTTP, HTTPS, FTP 프로토콜을 이용해서 웹 서버에서 다운로드가 가능합니다. 목차✅ 리눅스 Logrotate란1. 로그의 필요성2. 일반적인 리눅스 로그 저장 기간✅ 리눅스 Logrotate 설정1. /etc/logrotate.conf2. /etc/logrotate.d/✅ logrotate 명령어 및 옵션(환경)wget 설치하기 레드헷 계열에서 사용하는 패키지 매니저인 yum으로 wget 설치를 진행합니다. 기본 다운로드…

  • 리눅스 YUM 개념과 기본 명령어

    YUM(Yellow dog Updater, Modified)은 레드햇(RPM) 계열 리눅스 배포판에서 사용되는 패키지 설치 및 관리 도구입니다. 기존의 RPM 방식은 패키지 설치 시 의존성 문제로 사용자가 필요한 도구를 일일이 설치해야 하는 번거로움이 있었습니다. YUM은 이러한 의존성 문제를 자동으로 해결해 주며, 필요한 패키지와 관련된 의존 패키지를 함께 설치하여 오류를 최소화합니다. 의존성이 존재할 때 관련된 다른 패키지들을 자동으로…

  • 리눅스 top 명령어(CPU, 메모리 점유율 확인)

    리눅스 top 명령어는 구동 중인 OS의 상태를 보여주는 명령어이며 table of processes의 뜻을 가지고 있습니다. 총 메모리 사용량, CPU 사용량 등을 보여주며 각 프로세스의 구체적인 메모리와 CPU 사용량을 개별적으로 확인할 수 있습니다. 목차✅ 리눅스 Logrotate란1. 로그의 필요성2. 일반적인 리눅스 로그 저장 기간✅ 리눅스 Logrotate 설정1. /etc/logrotate.conf2. /etc/logrotate.d/✅ logrotate 명령어 및 옵션(환경)Ⅰ. 리눅스 top…

  • Varnish Cache란 무엇인가? | Varnish 설치 및 기본 설정하기

    Varnish Cache는 동적 웹사이트와 API에 적합한 HTTP 가속기입니다. 이는 스퀴드와 같은 클라이언트 사이드 캐시 역할을 하는 다른 웹 가속기와 달리 Nginx, 아파치와 같은 원본 서버 대신 HTTP 가속기로 설계 되었습니다. 또한, Varnish는 HTTP에 특화되어 있어 FTP, SMTP 및 다른 통신 프로토콜을 지원하는 프록시 서버와는 다릅니다. 목차✅ 리눅스 Logrotate란1. 로그의 필요성2. 일반적인 리눅스 로그…

  • 리눅스 host 명령어 | DNS 조회 명령어

    host 명령어는 DNS(Domain Name System) 조회를 할 때 사용하는 명령어입니다. 호스트 이름에 해당하는 IP 주소를 찾거나, 반대로 IP 주소를 호스트 이름으로 변환하는 데 사용됩니다. 네트워크 연결 문제를 해결하거나 호스트 이름과 IP 주소 간의 매핑을 확인하는 데 유용합니다. 목차✅ 리눅스 Logrotate란1. 로그의 필요성2. 일반적인 리눅스 로그 저장 기간✅ 리눅스 Logrotate 설정1. /etc/logrotate.conf2. /etc/logrotate.d/✅ logrotate…

  • 리눅스 프롬프트(Prompt)란?

    리눅스 셸에서 프롬프트는 CLI(커멘드 라인 인터페이스)의 명령줄 대기 모드를 뜻 합니다. 커맨드 프롬프트(command prompt) 또는 셸 프롬프트(shell prompt)로 불립니다. Command Prompt는 명령을 입력/발행할 수 있는 터미널 에뮬레이터(CLI)의 입력 필드입니다. 참조: 내 Linux 명령 Prompt는 무엇을 알려줍니까? 목차✅ 리눅스 Logrotate란1. 로그의 필요성2. 일반적인 리눅스 로그 저장 기간✅ 리눅스 Logrotate 설정1. /etc/logrotate.conf2. /etc/logrotate.d/✅ logrotate 명령어…

답글 남기기

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

Prove your humanity: 3   +   1   =