# Cheat Sheet #day30 - dmesg

## dmesg Cheatsheet

### Basic Usage

* **Display the kernel ring buffer messages**:
    
    ```sh
    dmesg
    ```
    

### Filtering Output

* **Filter messages using** `grep`:
    
    ```sh
    dmesg | grep <pattern>
    ```
    
* **Show messages with timestamps**:
    
    ```sh
    dmesg -T
    ```
    

### Controlling Output

* **Clear the ring buffer** (requires root):
    
    ```sh
    sudo dmesg -C
    ```
    
* **Limit the number of lines displayed**:
    
    ```sh
    dmesg | tail -n <number_of_lines>
    ```
    
* **Show messages in real-time**:
    
    ```sh
    dmesg -w
    ```
    

### Specific Message Types

* **Show only kernel messages**:
    
    ```sh
    dmesg -k
    ```
    
* **Show only userspace messages**:
    
    ```sh
    dmesg -u
    ```
    
* **Show messages by facility** (e.g., `auth`, `daemon`, `kern`, etc.):
    
    ```sh
    dmesg -f <facility>
    ```
    
* **Show messages by level** (e.g., `emerg`, `alert`, `crit`, `err`, etc.):
    
    ```sh
    dmesg -l <level>
    ```
    

### Time and Date Formatting

* **Show human-readable timestamps**:
    
    ```sh
    dmesg -H
    ```
    
* **Show messages with nanosecond timestamps**:
    
    ```sh
    dmesg -L
    ```
    

### Kernel Log Buffer Size

* **Display the size of the kernel log buffer**:
    
    ```sh
    dmesg -s <buffer_size>
    ```
    

### Advanced Usage

* **Decode** `devicetree` boot messages:
    
    ```sh
    dmesg -D
    ```
    
* **Display raw message buffer data**:
    
    ```sh
    dmesg -r
    ```
    

### Redirecting Output

* **Save output to a file**:
    
    ```sh
    dmesg > /path/to/file
    ```
    
* **Append output to a file**:
    
    ```sh
    dmesg >> /path/to/file
    ```
    

### Example Commands

* **Filter for USB-related messages**:
    
    ```sh
    dmesg | grep -i usb
    ```
    
* **Show only error messages**:
    
    ```sh
    dmesg -l err
    ```
    
* **Follow new kernel messages in real-time**:
    
    ```sh
    dmesg -wH
    ```
    
* **Clear the ring buffer and display messages**:
    
    ```sh
    sudo dmesg -C && dmesg
    ```
    

---

This cheatsheet provides a quick reference to the most commonly used `dmesg` commands and options. `dmesg` is a powerful tool for system administrators and developers to troubleshoot and monitor system activity by accessing kernel messages.
