dmesg(diagnostic message) 는 kernel 의 ring buffer 를 출력하는 명령어로 부팅시에 인식한 장치등 시스템 진단에 필요한 유용한 정보를 제공합니다.


부팅이후에도 특정 사용자의 su 전환 실패, IO 장치 오류등 운영에 필요한 정보를 출력하므로 익혀 두는 게 필요합니다.


kernel log 출력

옵션없이 사용하면 커널의 로그를 출력합니다.

$ sudo dmesg
BASH

journalctl -k 명령도 dmesg 와 동일하게 kernel 의 ring buffer 를 출력합니다.


dmesg 메시지는 양이 많기 때문에 원하는 내용을 검색하려면 pipe 로 연결한 후에 grep 을 실행해 줍니다.

$ sudo dmesg | grep -i nvidia
BASH


스크롤하면서 천천히 보려면 less 나 more 같은 pager 를 연결해 주면 됩니다.

$ sudo dmesg | less
BASH


level 별 출력

너무 많은 정보가 제공될 경우 level 을 지정해서 원하는 레벨의 로그만 출력할 수 있습니다. 다음은 error 레벨의 커널 로그 메시지만 출력합니다.

$ sudo  dmesg --level err
BASH


지원하는 로그 레벨은 --help 옵션을 주고 실행하면 확인할 수 있습니다.

$ sudo  dmesg --help

Supported log levels (priorities):
   emerg - system is unusable
   alert - action must be taken immediately
    crit - critical conditions
     err - error conditions
    warn - warning conditions
  notice - normal but significant condition
    info - informational
   debug - debug-level messages

For more details see dmesg(1).
BASH


facility 별 출력

로깅시 어떤 프로그램이 logging 을 남겼는지 확인하기 위해 facility 라고 부르는 분류 코드가 있으며 dmesg 에는 -f 옵션으로 facility 를 정해서 출력할 수 있습니다. 다음은 인증 관련한 오류만 출력합니다.

$ sudo  dmesg -f auth
BASH


syslog 에서 생성된 로그를 보려면 facility 를 syslog 로 지정합니다.

$ sudo  dmesg -f syslog 
BASH


모든 facility 를 보려면 마찬가지로 --help 옵션을 주고 실행하면 됩니다.

$ sudo  dmesg --help

Supported log facilities:
    kern - kernel messages
    user - random user-level messages
    mail - mail system
  daemon - system daemons
    auth - security/authorization messages
  syslog - messages generated internally by syslogd
     lpr - line printer subsystem
    news - network news subsystem
BASH

변경 사항 대기

dmesg 를 실행하면 현재 로그를 출력하고 종료합니다. 새로운 로그가 추가되었는지 확인하려면 반복해서 dmesg 를 실행해야 합니다. -w 옵션을 추가하면 tail -f 처럼 대기하면서 메시지가 추가되면 콘솔에 출력합니다.

$ sudo  dmesg -w
BASH


가독성 개선

아래 옵션은 모두 linux 커널 3.5 이상이 필요합니다.


-T 옵션을 추가하면 출력 결과에 로그가 생성된 시각에 대한 timestamp 를 표시합니다.

$ sudo  dmesg -T
BASH


-H 를 추가하면 사람이 좀 더 읽기 편하게 출력해 줍니다.

$ sudo  dmesg -H
BASH


-L 옵션을 사용하면 color 로 출력합니다.

$ sudo  dmesg -L
BASH


위 옵션을 모두 같이 사용할 수도 있습니다.

$ sudo  dmesg -THL
BASH


같이 보기