# How do I give an AI agent SSH access with the least damage?

Give an AI coding agent its own account, no password in the prompt, sudo for one command, read commands first, approval for writes and a record of what ran.

Source: https://valakes.com/learn/ai-agents-on-servers/

Last updated: 2026-11-29

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.

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

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:

```console
$ 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](https://valakes.com/learn/ssh-keys/) 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](https://valakes.com/learn/ai-agent-production-database/) 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:

```console
$ 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](https://valakes.com/learn/file-permissions/) shows, then look again:

```console
$ 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](https://valakes.com/learn/sudo-for-one-command/) 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/claude-code-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](https://valakes.com/learn/auto-approve-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.

```console
$ 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](https://valakes.com/learn/agent-audit-trail/) 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](https://valakes.com/learn/keep-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](https://valakes.com/guides/let-claude-code-run-commands-without-a-password/), and [the guide on reviewing what an agent did](https://valakes.com/guides/review-what-an-agent-did/) covers the log.

## In Valakes

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?](https://valakes.com/guides/let-claude-code-run-commands-without-a-password/)

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

## 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](https://www.sudo.ws/docs/man/sudoers.man/)
- [sudo(8) at sudo.ws, the option -n](https://www.sudo.ws/docs/man/sudo.man/)
- [passwd(1) on Ubuntu 24.04, the option -S and the account status L](https://manpages.ubuntu.com/manpages/noble/man1/passwd.1.html)
- [find(1) on Ubuntu 24.04, the test -readable](https://manpages.ubuntu.com/manpages/noble/man1/find.1.html)
- [OpenSSH manual pages](https://www.openssh.org/manual.html)

## Related articles

- [Can Claude Code SSH into a server?](https://valakes.com/learn/claude-code-ssh/): Yes. Claude Code has a shell, so it can run ssh host 'command' from your machine. It can also run on the server itself, and the desktop app sets that up with its SSH sessions. What the agent can reach, and where your key ends up, depends on the setup you choose.
- [Which commands are safe to auto-approve for an AI agent on a server?](https://valakes.com/learn/auto-approve-agent-commands/): Only exact commands that read and cannot start another program. find, sed, awk, a redirection and ssh look like reads and are not, so a rule that allows them by name allows what they can do.
- [How do I keep a record of what an AI agent ran on a server?](https://valakes.com/learn/agent-audit-trail/): 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.
- [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 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.
- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I change permissions recursively on Linux?](https://valakes.com/learn/file-permissions/): Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
- [How do I keep a command running after I close SSH?](https://valakes.com/learn/keep-commands-running/): Start it inside tmux (tmux new -s name, then Ctrl-b and d to leave). If tmux is not there, put nohup in front of the command and an ampersand after it. When a terminal closes, the programs started in it get a hangup signal and stop, and both tools get around that.
