<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://tech.uvoo.io/index.php?action=history&amp;feed=atom&amp;title=Ssh_audit_bashrc</id>
	<title>Ssh audit bashrc - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://tech.uvoo.io/index.php?action=history&amp;feed=atom&amp;title=Ssh_audit_bashrc"/>
	<link rel="alternate" type="text/html" href="https://tech.uvoo.io/index.php?title=Ssh_audit_bashrc&amp;action=history"/>
	<updated>2026-05-10T16:54:47Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.2</generator>
	<entry>
		<id>https://tech.uvoo.io/index.php?title=Ssh_audit_bashrc&amp;diff=5652&amp;oldid=prev</id>
		<title>Busk: Created page with &quot;Yes, you absolutely can log all commands for a specific user inside an LXD container without giving it privileged access.  The best way to do this is by modifying the user's s...&quot;</title>
		<link rel="alternate" type="text/html" href="https://tech.uvoo.io/index.php?title=Ssh_audit_bashrc&amp;diff=5652&amp;oldid=prev"/>
		<updated>2025-10-17T18:26:14Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Yes, you absolutely can log all commands for a specific user inside an LXD container without giving it privileged access.  The best way to do this is by modifying the user&amp;#039;s s...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Yes, you absolutely can log all commands for a specific user inside an LXD container without giving it privileged access.&lt;br /&gt;
&lt;br /&gt;
The best way to do this is by modifying the user's shell configuration files within the container. This approach doesn't require any special container privileges because it operates at the user and shell level, not the kernel level like `auditd`.&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
### Using the User's `.bashrc` File&lt;br /&gt;
&lt;br /&gt;
This method involves adding a logging command to the specific user's `~/.bashrc` file. Every time that user opens a new terminal or runs a command, it will be logged.&lt;br /&gt;
&lt;br /&gt;
Here are the steps to follow **inside the LXD container**:&lt;br /&gt;
&lt;br /&gt;
1.  **Access the Container Shell**&lt;br /&gt;
    First, get a shell inside the container you want to monitor.&lt;br /&gt;
&lt;br /&gt;
    ```bash&lt;br /&gt;
    lxc exec your-container-name -- bash&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
2.  **Switch to the Target User**&lt;br /&gt;
    If you are not already logged in as the user you want to monitor, switch to that user. Let's say the user is named `testuser`.&lt;br /&gt;
&lt;br /&gt;
    ```bash&lt;br /&gt;
    su - testuser&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
3.  **Edit the `.bashrc` File**&lt;br /&gt;
    Open the user's `.bashrc` file with a text editor like `nano`.&lt;br /&gt;
&lt;br /&gt;
    ```bash&lt;br /&gt;
    nano ~/.bashrc&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
4.  **Add the Logging Command**&lt;br /&gt;
    Scroll to the very end of the file and add the following line. This command uses `PROMPT_COMMAND` to execute the `logger` utility before each new command prompt is displayed.&lt;br /&gt;
&lt;br /&gt;
    ```bash&lt;br /&gt;
    export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.info &amp;quot;USER: $(whoami) PWD: $(pwd) CMD: $(history 1 | sed &amp;quot;s/^[ ]*[0-9]\+  //&amp;quot; )&amp;quot;'&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
    This line will log the **username**, their **current directory**, and the **command they just ran**.&lt;br /&gt;
&lt;br /&gt;
5.  **Configure Log Storage (as root)**&lt;br /&gt;
    You'll need to tell the system where to store these logs. Exit from the user's session (`exit`) to return to your root shell within the container.&lt;br /&gt;
&lt;br /&gt;
    Create a new configuration file for `rsyslog`:&lt;br /&gt;
&lt;br /&gt;
    ```bash&lt;br /&gt;
    nano /etc/rsyslog.d/50-user-commands.conf&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
    Add the following line to this new file. This tells `rsyslog` to send any logs from the `local6` facility to a specific file.&lt;br /&gt;
&lt;br /&gt;
    ```&lt;br /&gt;
    local6.* /var/log/user_commands.log&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
6.  **Restart `rsyslog`**&lt;br /&gt;
    Apply the changes by restarting the `rsyslog` service.&lt;br /&gt;
&lt;br /&gt;
    ```bash&lt;br /&gt;
    systemctl restart rsyslog&lt;br /&gt;
    ```&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
### How It Works and How to View Logs&lt;br /&gt;
&lt;br /&gt;
From now on, whenever `testuser` executes a command, it will be automatically logged to `/var/log/user_commands.log` inside the container.&lt;br /&gt;
&lt;br /&gt;
To see the logs in real-time, you can use the `tail` command from within the container's root shell:&lt;br /&gt;
&lt;br /&gt;
```bash&lt;br /&gt;
tail -f /var/log/user_commands.log&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
The output will look something like this:&lt;br /&gt;
&lt;br /&gt;
```&lt;br /&gt;
Oct 17 12:18:01 container-name testuser: USER: testuser PWD: /home/testuser CMD: ls -l&lt;br /&gt;
Oct 17 12:18:05 container-name testuser: USER: testuser PWD: /home/testuser CMD: cd /tmp&lt;br /&gt;
Oct 17 12:18:09 container-name testuser: USER: testuser PWD: /tmp CMD: echo &amp;quot;hello world&amp;quot;&lt;br /&gt;
```&lt;br /&gt;
&lt;br /&gt;
#### Important Considerations ⚠️&lt;br /&gt;
&lt;br /&gt;
  * **Bypassable**: A knowledgeable user could potentially bypass this logging by editing their own `.bashrc` file, using a different shell, or manually unsetting the `PROMPT_COMMAND` variable.&lt;br /&gt;
  * **Bash Specific**: This method works for the `bash` shell. If the user uses a different shell like `zsh`, you would need to modify the corresponding configuration file (e.g., `~/.zshrc`).&lt;/div&gt;</summary>
		<author><name>Busk</name></author>
	</entry>
</feed>