리눅스 Logrotate 개념과 설정

공정위 광고 문구 Ver 3.1
공정위 광고 문구 Ver 3.2(모바일 전용)

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

  • 리눅스 커널 버전 확인 명령어 및 배포판 버전 확인 방법

    리눅스를 운영할 때 리눅스 커널 버전 확인과 배포판 버전을 확인해야 하는 경우가 있습니다. ✅ 리눅스 커널 버전 확인 명령어 리눅스에서 커널 버전을 확인하는 명령어는 uname 명령어를 사용해서 확인할 수 있습니다. 1. uname 명령어 uname -a는 모든 결과를 보여 줍니다. 2. /proc/version 에서 확인 3. hostnamectl ✅ 리눅스 배포판 버전 확인 리눅스의 배포판 버전을…

  • [Linux] mod_ssl.so Apache 모듈 설치하기

    아파치를 사용하는 리눅스 서버에 SSL을 적용하려고 할 때 아피치 모듈 중 mod_ssl.so가 설치되어 있어야 합니다. ✅ mod_ssl.so 설치 ✅ 서버 방화벽 포트 443 열어두기 SSL 포트인 443 포트가 방화벽에서 열려있는 상태로 활성화 되어 있어야 합니다.

  • 리눅스 디렉토리 구조 및 파일 종류🔝

    리눅스 운영체제는 계층적 파일 시스템을 사용합니다. 위에서 아래 또는 위와 아래로 구성되어 있습니다. 리눅스 디렉토리 구조는 파일 시스템의 시작 지점인 루트 디렉토리(/)부터 시작됩니다. Linux 파일 시스템 계층 구조는 리눅스를 포함한 유닉스 유형 운영 체제의 디렉토리 구조와 내용을 결정하는 일련의 지침인 파일 시스템 계층 구조 표준의 적용을 받습니다. 계층 구조 표준은 리눅스 재단(영어: Linux Foundation)에서…

  • 리눅스 touch 명령어

    touch 명령어는 파일의 날짜 시간 정보를 변경하는 명령어입니다. 아무 옵션 없이 사용할 경우 파일의 최근 사용 시간, 변경 시간이 서버의 현재 시간으로 변경됩니다. 파일이 없는 파일명을 입력할 경우 새로운 크기가 0인 빈 파일이 생성됩니다. ✅ touch 명령어 기본 사용 방법 ✅ touch 명령어 명령어 옵션 ✅ 사용 예시 최근 사용 시간(Access), 변경 시간(Modify)이…

  • Linux: 아파치 재시작/시작/중지 명령어[CentOS 7]

    리눅스 웹서버를 운영하다 보면 CentOS에서 아파치 재시작을 하는 경우가 많습니다. 재시작 및 시작, 중지 명령어에 대해 알아 보겠습니다. 아파치는 HTTP 아파치 서버로 불리는 HTTP 웹서버입니다. 쉬운 웹서버 구축과 기능적인 측면에서 우수하기 때문에 많은 중소기업 및 대형사이트에서 사용합니다. 1. apache 버전 확인 2. apache 상태 확인 3. apache 시작 4. 아파치 중지 4. 아파치…

  • Zend OPcache 개념과 설치 및 설정

    OPcache는 PHP script 를 바이트코드(bytecode)로 컴파일한 후에 공유 메모리에 저장하여 성능을 향상 시키는 모듈입니다. 서버의 성능을 향상시키는 PHP 가속기(PHP accelerators) 중 하나인 Zend Opcache 는 PHP 5.5부터 기본 모듈로 내장되었습니다. ✅ OPcache의 주요 기능 ✅ Zend OPcache 설치 PHP 8.3을 이미 설치 해서 서버에 Zend OPcach가 이미 설치되어 있는 상태임을 확인했습니다. ✅ Zend…

답글 남기기

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

Prove your humanity: 0   +   10   =