# How do I keep a record of what an AI agent ran on a server?

Shell history misses commands sent over SSH, and the account can empty it. See what sshd and sudo log instead, and what no log covers.

Source: https://valakes.com/learn/agent-audit-trail/

Last updated: 2026-11-29

Do not count on shell history. Give the agent its own account and key, log sshd at VERBOSE so each login shows the key fingerprint, and let sudo record the input and output of anything the agent runs as root.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 29 November 2026.

Do not rely on shell history. A command sent as `ssh host 'command'` does not reach it, and the account can empty the file. Use the logs that sshd and sudo write as root: sshd logs every login with the fingerprint of the key, and sudo can log each command and what it printed. Neither sees a command that needs no sudo.

This is security advice, so everything below ran on the system named in the check line. The server logs through rsyslog to `/var/log/auth.log`, as a stock Ubuntu host does. On it, `deploy` is your account and `agent` belongs to the AI agent, with a key of its own. [The article on giving an agent SSH access](https://valakes.com/learn/ai-agents-on-servers/) shows a sudo log file for the agent account. This one adds the logins that sshd records and the output of each sudo command.

## What shell history misses

An agent that opens a shell and types two lines leaves them in the history file. Then it runs a command the way tools usually do, as `ssh host 'command'`:

```console
$ printf 'uptime\nexit\n' | ssh -tt -q app-agent > /dev/null
$ ssh app-agent 'cat ~/.bash_history'
uptime
exit
$ ssh app-agent 'hostname; id -un'
app
agent
$ ssh app-agent 'wc -l < ~/.bash_history'
2
```

The bash manual says a shell copies its history to `HISTFILE` when it exits. The one-off command `hostname; id -un` did not add a line. The file still has the two lines of the first session. The account also owns the file, so it can empty it:

```console
$ ssh app-agent ': > ~/.bash_history; wc -l < ~/.bash_history'
0
```

## What sshd logs for each login

The default `LogLevel` of sshd is `INFO`. This server uses `VERBOSE`. Its log has an `Accepted publickey` line with the key fingerprint for each login, a line with the file and line number of the matching key, and one `Starting session` line for each session:

```console
$ ssh app 'sudo sshd -T | grep -i ^loglevel'
loglevel VERBOSE
$ ssh-keygen -lf ~/.ssh/agent_key.pub
256 SHA256:/01/ukvKictugJevXn/21Ai/cTg3yhivlEHSJbfFat0 agent@learn (ED25519)
$ ssh app "sudo grep -ho '[A]ccepted publickey.*' /var/log/auth.log | sed -E 's/ from [0-9.]+ port [0-9]+//' | sort -u"
Accepted publickey for agent ssh2: ED25519 SHA256:/01/ukvKictugJevXn/21Ai/cTg3yhivlEHSJbfFat0
Accepted publickey for deploy ssh2: ED25519 SHA256:BY+9j8cSHC7cmo3k7lPht/4XtWFYJgN/gy/C1A0NaFw
$ ssh app "sudo grep -hm1 -o '[A]ccepted key.*agent.*' /var/log/auth.log"
Accepted key ED25519 SHA256:/01/ukvKictugJevXn/21Ai/cTg3yhivlEHSJbfFat0 found at /home/agent/.ssh/authorized_keys:1
```

The `sed` only cuts the address and port, which the real line also has. The fingerprint after `ED25519` is the one `ssh-keygen -lf` prints for the agent's public key, so the line tells you which key opened the session, and the second line says which entry of `authorized_keys` matched. A login by the agent's key on any other account, or by another key on the agent's account, would stand out here.

The session lines say that something ran, and not what:

```console
$ ssh app "sudo grep -ho '[S]tarting session: .*' /var/log/auth.log | sed -E 's/ from [0-9.]+ port [0-9]+ id [0-9]+//' | grep agent | sort -u"
Starting session: command for agent
Starting session: shell on pts/0 for agent
$ ssh app "sudo grep -c -e [u]ptime -e [h]ostname /var/log/auth.log; true"
0
```

A session of kind `command` is a one-off command, and the count shows that neither `uptime` nor `hostname` appears in the log. The `sshd_config(5)` manual says that `DEBUG` levels violate the privacy of users and are not recommended, so do not turn them up to get commands.

## What sudo logs

sudo logs every command it runs for a user, and `log_output` makes it keep the output as well. The rule of this agent has one line for that and one for the command:

```console
$ ssh app 'sudo cat /etc/sudoers.d/agent'
Defaults:agent log_output
agent ALL=(root) NOPASSWD: /usr/bin/cat /var/log/myapp/app.log
$ ssh app-agent 'sudo cat /var/log/myapp/app.log'
2026-11-29 09:58:14 INFO  GET /orders 200
2026-11-29 09:59:40 ERROR db connection timeout after 5000 ms
$ ssh app-agent 'sudo cat /etc/shadow'
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
```

The read of `app.log` is allowed by the rule and the read of `/etc/shadow` is not. Both are in the log of the host, which sudo writes through syslog unless you set `logfile`:

```console
$ ssh app "sudo grep -h 'COMMAND=' /var/log/auth.log | grep '[a]gent :' | sed -E 's/^[^ ]+ app //'"
sudo:    agent : PWD=/home/agent ; USER=root ; TSID=000001 ; COMMAND=/usr/bin/cat /var/log/myapp/app.log
sudo:    agent : command not allowed ; PWD=/home/agent ; USER=root ; COMMAND=/usr/bin/cat /etc/shadow
```

The `TSID` of the allowed command is the name of its recording. `sudoreplay` lists the recordings and plays one back:

```console
$ ssh app 'sudo sudoreplay -l' | sed -E 's/^.* 20[0-9]{2} : //'
agent : HOST=app ; CWD=/home/agent ; USER=root ; TSID=000001 ; COMMAND=/usr/bin/cat /var/log/myapp/app.log
$ ssh app 'sudo sudoreplay -m 1 000001'
Replaying sudo session: /usr/bin/cat /var/log/myapp/app.log2026-11-29 09:58:14 INFO  GET /orders 200
2026-11-29 09:59:40 ERROR db connection timeout after 5000 ms
```

The title line has no line break, so the first line of the recording follows it. The recording shows what the command printed, which the log line does not. It can hold anything the command printed, secrets included, so the folder belongs to root alone:

```console
$ ssh app-agent 'ls /var/log/sudo-io'
ls: cannot open directory '/var/log/sudo-io': Permission denied
$ ssh app-agent 'echo x >> /var/log/auth.log'
bash: line 1: /var/log/auth.log: Permission denied
```

The agent can neither read the recordings nor change the log.

## What none of this records

A command that needs no sudo leaves nothing in these logs. The agent creates and removes a file, and the log has no trace of it:

```console
$ ssh app-agent 'touch ~/report.txt; rm ~/report.txt'
$ ssh app "sudo grep -c '[r]eport.txt' /var/log/auth.log; true"
0
```

Two other tools cover that gap on the host, and I did not run them here. The Linux audit system writes an event whenever a system call that you name happens (see `auditctl(8)` and `audit.rules(7)`). Process accounting writes a record for each process that ends, and `lastcomm(1)` prints the commands of earlier runs (see `accton(8)`).

## Mistakes people make

You let the agent log in as you. The fingerprint and the account then say nothing about who ran what.

You read the shell history after an incident and find nothing. It never held the commands.

You give the agent unrestricted sudo and trust the logs on the same machine. An account with root can edit them.

You never read the log. Look at it after the first session, while you can still change the setup.

You keep the recordings where the agent can read them.

## In Valakes

Valakes keeps its own record of what an agent did through it: every call, approval, command and transfer, with the agent, the host and the time. An agent has no tool to read it, and Verify checks the chain of entries. The Help says the log is tamper-evident, not tamper-proof, and the guide on [reviewing what an agent did](https://valakes.com/guides/review-what-an-agent-did/) shows where to look. A login that does not go through Valakes shows up only in the logs above.

## In Valakes

Valakes keeps an audit log of every call, approval, command and transfer of an agent, with the agent, the host and the time, and Verify checks that no entry was edited, reordered or cut from the end. The Help calls the log tamper-evident, not tamper-proof. [Guide: How do I see what an AI agent ran on my servers?](https://valakes.com/guides/review-what-an-agent-did/)

[Read the docs: Agents](https://docs.valakes.com/agents/)

## Questions

### Why does the agent get its own account and key?

The login line names the account and the fingerprint of the key. With a separate account and key for the agent, those two facts say whose session it was. If the agent used your account, the line would look the same as yours.

### Can the agent change these logs?

Not from an account without root. The log files belong to root and the agent was refused when it tried to write to one. An agent with unrestricted sudo can edit them, so send a copy of the logs to another machine that the agent cannot reach.

### Does sudo log the password of the agent?

The agent in this article has no password, because sudo does not ask for one. For a program that does ask, sudoers(5) warns that a typed password can end up in the I/O log, so keep that folder as closed as a password file.

## References

- [sshd_config(5) on Ubuntu 24.04, LogLevel](https://manpages.ubuntu.com/manpages/noble/man5/sshd_config.5.html)
- [sudoers(5) at sudo.ws, log_output, log_input and the I/O log](https://www.sudo.ws/docs/man/sudoers.man/)
- [sudoreplay(8) at sudo.ws](https://www.sudo.ws/docs/man/sudoreplay.man/)
- [bash(1) on Ubuntu 24.04, the history file and the history builtin](https://manpages.ubuntu.com/manpages/noble/man1/bash.1.html)
- [auditctl(8) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man8/auditctl.8.html)
- [audit.rules(7) on Ubuntu 24.04, syscall rules](https://manpages.ubuntu.com/manpages/noble/man7/audit.rules.7.html)
- [accton(8) on Ubuntu 24.04, process accounting](https://manpages.ubuntu.com/manpages/noble/man8/accton.8.html)
- [lastcomm(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/lastcomm.1.html)

## Related articles

- [How do I give an AI agent SSH access with the least damage?](https://valakes.com/learn/ai-agents-on-servers/): Give the agent its own account on the server, with no sudo, no password in the prompt and nothing secret in its reach. Let it read first, allow root for one exact command at most, make every change wait for a person, and keep a record of what ran.
- [How do I keep an AI agent from deleting a production database?](https://valakes.com/learn/ai-agent-production-database/): Let the agent connect as a database role that has SELECT on the tables it needs and nothing else, so DROP, DELETE and TRUNCATE fail with a permission error. Keep the production password out of every file and variable the agent can read, give it a copy of the data to work on, and restore a backup once before the agent connects to production.
- [How do I read logs on a Linux server?](https://valakes.com/learn/read-server-logs/): Use tail -f to watch a log file live, grep -C 3 to see a match with the lines around it, and zgrep for the rotated copies that are compressed. For a service that logs to systemd, run journalctl -u name --since "1 hour ago". You need sudo or the right group for the journal.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
