Knowing how to clean dmesg can make troubleshooting easier when you want to focus on new kernel messages instead of scrolling through old output. On Linux, dmesg reads messages from the kernel ring buffer, which contains information about hardware, drivers, devices, and system events. You can clear that buffer, display and clear it in one step, or filter the output without deleting anything.
Before clearing messages, understand what the command does—and what it does not do. Clearing the kernel ring buffer does not necessarily remove copies stored by system logging services, and it does not fix the problem that caused a message. This guide explains safe ways to clean dmesg, verify the result, troubleshoot permission errors, and preserve useful information before you clear it.
What Does “Clean Dmesg” Mean?
The term “clean dmesg” usually means clearing the current kernel ring buffer so that old messages no longer appear in a normal dmesg listing. The ring buffer is a limited area of memory used by the Linux kernel to record messages about system activity. It can include information about hardware detection, USB devices, disk errors, driver initialization, and boot events.
To view the current messages, run:
dmesg
On some Linux systems, regular users cannot read every kernel message. If the command returns a permission error or shows limited output, try:
sudo dmesg
Clearing the buffer is different from deleting a log file. The buffer is held in memory, and its contents may be copied to system logs by services such as systemd-journald or traditional syslog tools. The exact logging behavior depends on your distribution and configuration.
How to Clear the Dmesg Ring Buffer
The standard command for clearing the kernel ring buffer is dmesg -C. On systems where clearing is restricted to administrators, run it with sudo:
sudo dmesg -C
The command may produce no output when it succeeds. You can check whether the buffer is empty by running:
sudo dmesg
If no messages appear, the current ring buffer has been cleared. New kernel messages will begin to appear as the system continues running. Clearing the buffer does not turn off kernel logging, disable drivers, or stop messages from being generated.
Depending on your version of the dmesg utility, the long option may also be available:
sudo dmesg --clear
For portability, check the command’s help or manual page if an option is not recognized:
dmesg --help
man dmesg
Print and Clear Dmesg in One Command
If you want to save or review the existing messages and then empty the ring buffer, use the read-and-clear option:
sudo dmesg -c
This option prints the messages currently in the buffer and clears them afterward. It is useful when you want to capture old output before starting a fresh troubleshooting test. Because the output may be lengthy, you can save it to a file:
sudo dmesg -c > dmesg-before-test.txt
That command redirects the displayed messages into a text file in the current directory. If you need to retain the file for later analysis, choose an appropriate location and access permissions. Kernel logs can contain device details, system information, and other data you may not want to share publicly.
Use -c carefully: unlike simply viewing the buffer, it also clears the messages it reads. If you only want to inspect output, use dmesg or save a non-clearing snapshot instead:
sudo dmesg > dmesg-snapshot.txt
How to Clear Dmesg on Different Linux Distributions
The dmesg command is available on most Linux distributions, including Ubuntu, Debian, Fedora, Arch Linux, and many server-focused systems. The basic clearing syntax is generally the same, but permission policies and logging services can differ.
-
Ubuntu and Debian: Try
sudo dmesg -C. On systems using systemd, older kernel messages may also be available in the journal. -
Fedora: Use
sudo dmesg -Cif the installed version and system policy allow it. Fedora commonly uses systemd’s journal for persistent or runtime log access. -
Arch Linux: Use
sudo dmesg -Cand consult the localdmesgmanual if the option behaves differently. - Minimal containers or embedded systems: The command may be missing, restricted, or provided by a limited utility package. Check the environment’s documentation and permissions.
If dmesg -C is rejected, do not assume that your system is broken. The command may be restricted by kernel settings, container permissions, security policy, or the version of the utility installed. Check the error message, verify that you are using the correct host or virtual machine, and consult your distribution’s documentation before changing system-wide security settings.
How to Filter Dmesg Without Clearing It
You may not need to clear the ring buffer at all. Filtering lets you focus on relevant messages while preserving the original data. For example, search for a word such as “error”:
sudo dmesg | grep -i error
To look for messages related to a specific device or driver, replace the search term:
sudo dmesg | grep -i usb
You can also show human-readable timestamps with:
sudo dmesg -T
Human-readable timestamps can make output easier to interpret, although the conversion may not be exact in every situation. To follow new kernel messages as they arrive, use:
sudo dmesg -w
Follow mode is useful during testing. For example, start it before plugging in a USB device, connecting a peripheral, or reproducing a hardware issue. New messages will appear as the kernel generates them. Press Ctrl+C to stop following the buffer; this does not clear it.
On versions that support it, you can combine options to follow messages with readable timestamps:
sudo dmesg -wT
If this option combination is unsupported, check dmesg --help for the syntax available on your system.
Does Clearing Dmesg Delete System Logs?
No. Clearing the ring buffer with dmesg -C affects the current kernel message buffer. It does not guarantee that the same messages have been deleted from other logging locations. On a system using systemd, kernel messages may be accessible through journalctl. For example:
sudo journalctl -k
To view kernel messages from the current boot, use:
sudo journalctl -k -b
Journal retention and storage settings determine how long those records remain available. Traditional logging configurations may also write kernel messages to files such as /var/log/kern.log or /var/log/syslog, depending on the distribution. Clearing the ring buffer does not automatically remove those files or journal entries.
Deleting or altering system logs is a separate administrative task and can affect troubleshooting, auditing, and incident investigations. If your goal is to manage disk usage, use your logging service’s documented retention or vacuum settings rather than assuming that clearing dmesg will remove stored logs.
Why Is Dmesg Permission Denied?
Linux may restrict access to kernel messages because they can reveal sensitive information about the system. If you see “Operation not permitted” or “Permission denied,” first try the command with administrator privileges:
sudo dmesg
For clearing, try:
sudo dmesg -C
If the command still fails, the system may have kernel restrictions in place, or you may be running inside a container that does not have permission to manage the host’s kernel ring buffer. A container generally cannot clear the host buffer unless it has been given specific privileges. Avoid weakening security controls simply to make the command work, especially on shared or production systems.
Some Linux configurations use the kernel.dmesg_restrict setting to limit access. You can inspect its current value with:
sysctl kernel.dmesg_restrict
Do not change this setting unless you understand the security implications and have authorization to administer the machine. In many cases, using sudo or asking the system administrator is the safer solution.
Best Practices Before Cleaning Dmesg
Kernel messages are often most useful when they are still available. Before clearing the buffer, consider whether the output could help diagnose a hardware, driver, boot, or performance issue. A short preparation step can prevent the loss of evidence you may need later.
-
Save relevant output: Use
sudo dmesg > dmesg-backup.txtbefore clearing the buffer. - Record the time and symptoms: Note what you were doing and when the problem occurred.
- Filter before deleting: Search for a device name, driver, warning, or error if you only need a smaller view.
- Clear only when useful: A fresh buffer can make it easier to identify messages generated during a specific test.
- Protect saved logs: Review files before sharing them, since log output may expose system or device details.
A practical troubleshooting sequence is to save the current output, clear the ring buffer, reproduce the issue, and then inspect the new messages. For example:
sudo dmesg > dmesg-before.txt
sudo dmesg -C
Reproduce the problem, then run:
sudo dmesg
This approach separates new kernel activity from earlier messages while keeping a copy of the original output for comparison.
Common Dmesg Clearing Problems
The buffer is not empty after clearing. New messages may have been generated immediately by the kernel, or the command may not have completed successfully. Run sudo dmesg again and check the timestamps and command output.
Old messages still appear in journalctl. This is expected when messages were copied into the system journal. Clearing the ring buffer does not erase journal history.
The command says “invalid option.” Your version may not support the option or may use different syntax. Check dmesg --help and the manual page.
The command works on a host but not in a container. Container security boundaries often prevent access to host kernel functions. Run the command on the host or use an authorized administrative method.
Messages return after reboot. A reboot starts a new kernel session and generates new messages. Depending on system configuration, previous messages may also have been preserved in a journal or another log destination.
Conclusion
To clean the current Linux kernel message buffer, run sudo dmesg -C. To print its contents and clear them in one step, use sudo dmesg -c. If you only need a cleaner view, filtering with grep or following new messages with dmesg -w may be a better choice.
Remember that cleaning the ring buffer does not necessarily delete messages stored by journald or other logging services. Save useful output before clearing it, especially when investigating an error. With the right command and a little preparation, you can start a focused troubleshooting session without confusing the kernel buffer with the system’s longer-term logs.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.