On this page
Key takeaways
- The fix is old and plain: a separate user, a separate key and an SSH setting that lets this key start one fixed command and nothing else.
- Here the key can only run
/usr/local/bin/deploy.sh. The assistant triggers a deploy; it cannot open a shell. - Removing the key file ends this access at once, and nothing else on the server changes.
- The restriction limits what the key can run, not what the deployed code does.
An assistant that deploys for you needs access to the server, and your normal SSH key gives it everything you can do. This guide gives it a separate key that can run exactly one script, so a wrong command or a bad prompt has a small reach.
The risk and the idea
If you hand an assistant your own SSH key, it can read every file on the server, delete data and change the firewall. It will not do so on purpose, but a misread instruction is enough. The fix is old and plain: a separate user, a separate key and an SSH setting that lets this key start one fixed command and nothing else.
Here the key can only run /usr/local/bin/deploy.sh. The assistant triggers a deploy; it cannot open a shell.
Before you start
This guide assumes the server from the first-hour setup: Ubuntu 24.04, a sudo user called ops and SSH keys only. Replace SERVER_IP with your server’s address.
- your app is a git clone in
/srv/appthat can run git pull (for a private repository, add a read-only deploy key for the user deploy) - the app runs as a systemd service called app (change the name in the commands if yours differs)
- Claude Code, or another coding assistant, installed on your own computer
The steps
Create a key for this one purpose
Run this on your computer. The key has no passphrase because an unattended tool cannot type one; the limits you set on the server in the next steps are what protect it.
ssh-keygen -t ed25519 -f ~/.ssh/deploy_myserver -C "deploy-myserver"
cat ~/.ssh/deploy_myserver.pubUse a different key for each server so you can remove one without touching the others.
Create the deploy user
Connect as ops. The user has no password at all, so it can never log in with one, and it owns only the app folder.
sudo adduser --disabled-password --gecos "" deploy
sudo chown -R deploy:deploy /srv/appWrite the deploy script
The script is owned by root, so the deploy user can run it but not change it. It pulls the new code, restarts the service and prints whether it is active. --ff-only makes git refuse anything but a plain fast-forward.
sudo tee /usr/local/bin/deploy.sh > /dev/null <<'EOF'
#!/bin/sh
set -eu
cd /srv/app
git pull --ff-only
sudo /usr/bin/systemctl restart app
systemctl is-active app
EOF
sudo chown root:root /usr/local/bin/deploy.sh
sudo chmod 755 /usr/local/bin/deploy.shAllow exactly one privileged command
Restarting a service needs root. This rule lets deploy run that one command without a password and nothing else. visudo -cf checks the syntax; a broken sudoers file can lock you out of sudo, so do not skip it.
echo 'deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app' | sudo tee /etc/sudoers.d/deploy
sudo chmod 440 /etc/sudoers.d/deploy
sudo visudo -cf /etc/sudoers.d/deployInstall the key with a forced command
Replace the part after the closing quote with the single line printed by cat on your computer in the first step. restrict switches off port forwarding, agent forwarding and terminals for this key, and command= makes every login run the script, whatever the client asks for. It must stay one line.
sudo install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
echo 'restrict,command="/usr/local/bin/deploy.sh" ssh-ed25519 AAAA...your-public-key... deploy-myserver' | sudo tee /home/deploy/.ssh/authorized_keys
sudo chown deploy:deploy /home/deploy/.ssh/authorized_keys
sudo chmod 600 /home/deploy/.ssh/authorized_keysTry it from your computer
The first command runs a deploy. The second asks for whoami, but the server ignores the request and runs the deploy script again: that is the restriction working.
ssh -i ~/.ssh/deploy_myserver deploy@SERVER_IP
ssh -i ~/.ssh/deploy_myserver deploy@SERVER_IP whoamiSee what the key did
Every login of the deploy user is in the SSH log. Look at it after an assistant session.
sudo journalctl -u ssh --since "1 hour ago" | grep deployTake the access away again
Removing the key file ends this access at once, and nothing else on the server changes. Do it when the project is finished or when you are unsure what an assistant has done.
sudo rm /home/deploy/.ssh/authorized_keysLet an assistant set up the deploy path
The setup itself needs an administrator login, so give the assistant the ops user for this one session and not the deploy key. Paste the prompt below. It makes the assistant show a plan first, check every step and stop when a check fails; the plan is your moment to read what will happen.
I have a Cloudvix server at SERVER_IP running Ubuntu 24.04. I can log in as ops (SSH key, sudo). My app is a git clone in /srv/app and runs as the systemd service app. Set up a restricted deploy path for me. Connect as ops.
Rules:
1. Before you change anything, show me a numbered plan and wait for my yes.
2. After every step, run a check command and show me its output.
3. If a check fails, stop and tell me. Do not try to work around it.
Plan to propose: create the user deploy without a password; give deploy ownership of /srv/app; write /usr/local/bin/deploy.sh, owned by root with mode 755, that runs git pull --ff-only in /srv/app, restarts the service with sudo /usr/bin/systemctl restart app and prints systemctl is-active app; add /etc/sudoers.d/deploy allowing exactly that one systemctl command without a password and validate it with visudo -cf; write /home/deploy/.ssh/authorized_keys with one line that starts with restrict,command="/usr/local/bin/deploy.sh" followed by the public key I will paste; run the deploy once from my computer and show me the output.
Do not add deploy to the sudo or docker groups, do not touch other users’ keys, and never print a private key. At the end, list what you changed and how to undo each change.Tell the assistant how to deploy
Afterwards, give the assistant the deploy key and put these lines in the notes file it reads at the start of a session (CLAUDE.md for Claude Code). The server enforces the restriction either way; the notes make the assistant ask you first and report back.
If the assistant should also order the server, give it a Cloudvix API key instead of your account login: keys have limits (up to €500 a month and 25 servers). Tell it to show you the price and wait for your yes before it orders.
## Deploying
- Deploy only with: ssh -i ~/.ssh/deploy_myserver deploy@SERVER_IP
- That command runs the deploy script on the server and takes no arguments.
- Never open any other SSH session to this server and never use another key.
- Before deploying: run the tests, show me a summary of the changes and wait for my yes.
- After deploying: open the site or its health URL and tell me what you saw.
- If the deploy fails, stop and show me the output. Do not retry in a loop.If something goes wrong
Permission denied (publickey) for deploy
Check that /home/deploy/.ssh is mode 700 and authorized_keys mode 600, both owned by deploy, that the key is on one single line and that the quotes around command= are straight ones.
The script prints that a password is required for sudo
The sudoers line must name the exact command and arguments. Check the path with command -v systemctl and match it.
git pull fails inside the script
The user deploy cannot read the repository. Give that user its own read-only deploy key on the repository host.
The assistant asks for a shell or tries other commands over SSH
Every attempt runs the same script. That is the design; tell it the notes above.
Good to know
The SSH options come from the sshd manual (authorized_keys) and the sudoers rule from the sudo documentation. The name of the permission settings differs by assistant: read its documentation and keep the default of asking before it runs a command.
The restriction limits what the key can run, not what the deployed code does. If the assistant pushes bad code, the script deploys it. Keep reviewing the changes before you say yes, and keep the previous commit at hand: reverting it and deploying again is the rollback.
Where to go next
If your app runs in containers, the Docker guide shows how to install Docker; the deploy script would then call docker compose instead. The first-hour guide covers the server basics this guide relies on. Links are at the end of this page.
Runs Cloudvix and is responsible for the guides published here.