How do I keep a record of what an AI agent ran on a server?
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.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
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 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':
$ 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:
$ 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:
$ 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:
$ 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:
$ 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:
$ 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:
$ 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:
$ 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:
$ 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 shows where to look. A login that does not go through Valakes shows up only in the logs above.
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?
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 manpages.ubuntu.com
- sudoers(5) at sudo.ws, log_output, log_input and the I/O log www.sudo.ws
- sudoreplay(8) at sudo.ws www.sudo.ws
- bash(1) on Ubuntu 24.04, the history file and the history builtin manpages.ubuntu.com
- auditctl(8) on Ubuntu 24.04 manpages.ubuntu.com
- audit.rules(7) on Ubuntu 24.04, syscall rules manpages.ubuntu.com
- accton(8) on Ubuntu 24.04, process accounting manpages.ubuntu.com
- lastcomm(1) on Ubuntu 24.04 manpages.ubuntu.com
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.