How do I give an AI agent SSH access with the least damage?
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.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
Create a separate account for the agent and let it log in with a key of its own. Give that account no sudo and nothing secret to read. Let the agent run read commands first, allow root for one exact command only if a task needs it, approve every change yourself, and keep a record of what ran. The agent can do what its account can do, so most of the work is the account.
This is security advice. The commands below ran on the system named in the check line, and what is said about sudo follows the sudoers manual. What an agent tool does on your laptop depends on the tool, so the last section covers only Valakes, from its Help.
Start with an account that can do little
Make the account and see what it is and is not:
$ sudo useradd -m -s /bin/bash agent
$ id agent
uid=1002(agent) gid=1002(agent) groups=1002(agent)
$ sudo passwd -S agent | cut -d' ' -f1,2
agent L
$ sudo -l -U agent
User agent is not allowed to run sudo on dev.
$ sudo -u agent cat /etc/shadow
cat: /etc/shadow: Permission denied
agent L means the password is locked. The account has no password that anyone could type into a prompt or into the chat. It logs in with a key, which you create as in the SSH keys article and put in this account’s authorized_keys and nowhere else. It may not use sudo, and it cannot read the password file. The system refuses these, so you do not depend on the agent behaving.
Keep the password out of the prompt
A password typed into a prompt or a chat message, or left in a file the agent can read, becomes part of what the agent sees. Do not put one there, and do not paste a database password into the first message. If a task needs a login to a database, make a database account that can do only that task; the article on agents and production databases shows such a role.
Read first, and know what reading hands over
The first job you give an agent should be a read: df -h, ps, a log. A read is not harmless. It returns whatever the account may open, and the Valakes Help says an external agent sends what it reads, command output included, to its own model provider. So look at what the account can open:
$ sudo -u agent find /srv /home -readable -type f -name '.env*' 2>/dev/null
/srv/app/.env
An application file with a database password that every user can read is the usual find, and the agent is a user. Close it as the permissions article shows, then look again:
$ sudo chmod 600 /srv/app/.env
$ sudo -u agent find /srv /home -readable -type f -name '.env*' 2>/dev/null
Nothing is listed now. The Valakes Help makes the same point about its Read-only level: cat ~/.ssh/id_ed25519, grep -r password /etc and journalctl all pass as reads, and each hands over what the SSH account can read. A list of allowed commands does not know which files matter. The account does.
Allow root for one command, if you must
When a task needs root, write a rule that names one command with its arguments and nothing else. The sudo article explains the format, why wildcards and editors break it, and why to check the file with visudo -cf. Here is such a rule, with touch standing in for a restart. Its first line turns on a log file for this account, which the last section reads:
$ printf '%s\n' 'Defaults:agent logfile=/var/log/sudo-agent.log, loglinelen=0' 'agent ALL=(root) NOPASSWD: /usr/bin/touch /run/maintenance' > /tmp/agent-maint
$ sudo visudo -cf /tmp/agent-maint
/tmp/agent-maint: parsed OK
$ sudo install -m 440 -o root -g root /tmp/agent-maint /etc/sudoers.d/agent-maint
Test it as the agent. The -n option stops sudo from asking for a password, so a command outside the rule fails at once:
$ sudo -u agent sudo -n touch /run/maintenance
$ stat -c '%U %n' /run/maintenance
root /run/maintenance
$ sudo -u agent sudo -n cat /etc/shadow
sudo: a password is required
The allowed command ran as root. The other one was refused. What to do when Claude Code needs sudo covers the moment an agent asks for more.
Let a person approve every change
Reads may run on their own. Anything that writes, installs, restarts or deletes should wait until a person has seen the exact command and said yes. Look for that setting in whatever runs the agent and turn it on before the first session. The pause costs seconds, and it is the only point where someone reads a command before it runs. Which commands can skip it is the subject of auto-approving agent commands.
Keep a record of what ran
Afterwards you want to say what the agent did. The rule above turned on a file log for sudo, and both the allowed and the refused command of the agent are lines in it. Without the logfile option sudo logs through syslog instead, as its manual says.
$ sudo sed -E 's/^[A-Z][a-z]{2} [ 0-9]+ [0-9:]+ //' /var/log/sudo-agent.log
: agent : TTY=pts/1 ; PWD=/home/dev ; USER=root ; COMMAND=/usr/bin/touch /run/maintenance
: agent : a password is required ; TTY=pts/1 ; PWD=/home/dev ; USER=root ; COMMAND=/usr/bin/cat /etc/shadow
The sed only removes the time at the start of each line. This log covers sudo and nothing else the agent ran, so keep the agent’s own transcript too and read both after a session. The audit trail article adds what sshd records and how to keep the output of a command.
Mistakes people make
You give the agent your own account. Your keys, your history and your sudo rights are then in its reach, and you cannot tell its commands from yours.
You write NOPASSWD: ALL so that nothing asks. That is root without any of the checks above.
You treat Read-only as a safe mode on production. The Help says it is not safe on its own and gives the advice of this article: a separate account with no sudo and no secrets.
You leave a job running after you stop the agent. A program started with nohup or in tmux keeps going when its session ends, and the Valakes kill switch does not undo it. Keeping commands running shows how such jobs survive.
In Valakes
Valakes has settings for most of this, and its Help states where they stop. Every host starts with agent access Off. You choose Read-only, Approve writes or Full for each host, and a command that needs you waits in an approval card with the exact command, the host and the directory. Approve runs it once. For a saved password the agent writes {{cred:name}} and Valakes answers the prompt, so the agent never sees the value. An agent cannot read the audit log. You can: it lists every call, approval and command, and Verify checks its chain. The Help calls the log tamper-evident, not tamper-proof. Masking replaces only values saved in the vault, so a password in a .env file stays visible to an agent that can read the file, which is why the account matters. Connecting Claude Code is in the guide, and the guide on reviewing what an agent did covers the log.
The guide shows how to connect Claude Code and set the level per host. Guide: How do I let Claude Code work on my server without giving it the password?
Questions
Is a read-only agent safe on a production server?
Not on its own. Read commands such as cat and grep hand over everything the account can read, and an external agent sends what it reads to its model provider. Give the agent a separate account that cannot write anything that matters, has no sudo and holds no keys or secrets in its home folder.
Should the agent log in with my own SSH key?
No. Create a key and an account for the agent. Its actions then show up under its own name, and you can end its access by removing that one key, without touching yours.
Can I give the agent root?
Not as a general right. If one task needs root, write a sudo rule for that exact command with its exact arguments, as in the article on sudo for one command, and keep the rest of the account without it.
Does keeping the password out of the prompt keep every secret away from the agent?
No. It keeps that one password out. A secret in a file the account can read, such as a .env file, can still reach the agent when it reads that file.
References
- sudoers(5) at sudo.ws, command specs, Defaults entries, logfile and loglinelen www.sudo.ws
- sudo(8) at sudo.ws, the option -n www.sudo.ws
- passwd(1) on Ubuntu 24.04, the option -S and the account status L manpages.ubuntu.com
- find(1) on Ubuntu 24.04, the test -readable manpages.ubuntu.com
- OpenSSH manual pages www.openssh.org
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.