{"id":677,"date":"2016-07-06T20:20:48","date_gmt":"2016-07-06T14:50:48","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=677"},"modified":"2026-09-07T17:22:27","modified_gmt":"2026-09-07T11:52:27","slug":"enable-command-logging-on-linux-hosts","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/enable-command-logging-on-linux-hosts\/","title":{"rendered":"Enable command logging on Linux hosts"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Command logging can be useful when you need to track commands executed by users on a Linux server. It can help administrators review user activity and investigate unexpected or abnormal actions performed on the system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide explains how to enable command logging using Bash and <code>rsyslog<\/code>, and store the logged commands in a separate log file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before configuring command logging, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Root or <code>sudo<\/code> access to the Linux server.<\/li>\n\n\n\n<li>Bash shell access.<\/li>\n\n\n\n<li><code>rsyslog<\/code> installed and running.<\/li>\n\n\n\n<li>Basic knowledge of Linux configuration files and services.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Configure Bash Command Logging<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For CentOS\/RHEL-based systems, edit:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/etc\/bashrc<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following line:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug \"$(whoami) [$$]: $(history 1 | sed \"s\/^[ ]*[0-9]\\+[ ]*\/\/\" ) [$RETRN_VAL]\"'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This configures Bash to send the executed command information to the system logger.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Ubuntu Server, the Bash configuration file is<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/etc\/bash.bashrc<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the same <code>PROMPT_COMMAND<\/code> line to this file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure the Syslog Format<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following configuration to the syslog configuration file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">local6.*                \/var\/log\/cmdlog.log<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This tells the logging service to store messages received through the <code>local6<\/code> facility in:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/log\/cmdlog.log<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Restart rsyslog<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Restart the <code>rsyslog<\/code> service to apply the configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For CentOS\/RHEL:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/etc\/init.d\/rsyslog restart<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For Ubuntu Server:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">service rsyslog restart<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Verify Command Logging<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After opening a new shell session, execute a test command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">whoami<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then check the command log:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cat \/var\/log\/cmdlog.log<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The executed command should be recorded in the log file along with information such as the username, process ID, and command return value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enabling Bash command logging provides administrators with an additional way to track activity on Linux hosts. By using <code>PROMPT_COMMAND<\/code> with <code>logger<\/code> and configuring <code>rsyslog<\/code>, executed commands can be stored separately in <code>\/var\/log\/cmdlog.log<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can be useful for troubleshooting, auditing, and investigating unexpected activity on Linux servers.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Security Note:<\/strong> Command logging should be implemented with appropriate access controls because command logs may contain sensitive information. Bash history and this logging method are not a complete security audit solution.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Where are command logs stored?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With the configuration in this guide, command logs are stored in:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/var\/log\/cmdlog.log<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Which file should be modified on Ubuntu?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On Ubuntu Server, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/etc\/bash.bashrc<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On CentOS\/RHEL-based systems, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/etc\/bashrc<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Why is rsyslog restarted after making the configuration change?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Restarting <code>rsyslog<\/code> ensures that the new <code>local6<\/code> logging configuration is loaded and applied.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Can command logging help identify abnormal user activity?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. The logs can help administrators review commands executed during user sessions and investigate unexpected activity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Atop Installation on Linux Host<\/strong> \u2013 Learn how to install and use Atop for monitoring Linux system and process activity.<br><a href=\"https:\/\/pheonixsolutions.com\/blog\/atop-installation-linux-host\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noreferrer noopener\">Read the article<\/a><\/li>\n\n\n\n<li><strong>Install Multiple Versions of Node.js on Linux<\/strong> \u2013 Learn how to install and manage multiple Node.js versions on a Linux server.<br><a href=\"https:\/\/pheonixsolutions.com\/blog\/install-multiple-version-node-linux\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noreferrer noopener\">Read the article<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Command logging can be useful when you need to track commands executed by users on a Linux server. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1024,1040],"tags":[],"class_list":["post-677","post","type-post","status-publish","format-standard","hentry","category-security","category-linux","psol-cat-security"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-aV","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=677"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions"}],"predecessor-version":[{"id":11474,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions\/11474"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}