Skip to contentcloudvix
cloudvix
GuideDevelopers & AI

Deploy from Claude Code to your own server over SSH, safely

Denys DunaievskyiFounder, Cloudvix
  • 12 min read
  • Published
  • LevelIntermediate
Guides
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/app that 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.

On your computerbash
ssh-keygen -t ed25519 -f ~/.ssh/deploy_myserver -C "deploy-myserver"
cat ~/.ssh/deploy_myserver.pub
Tip

Use 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.

On the serverbash
sudo adduser --disabled-password --gecos "" deploy
sudo chown -R deploy:deploy /srv/app

Write 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.

On the server · /usr/local/bin/deploy.shbash
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.sh

Allow 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.

On the server · /etc/sudoers.d/deploybash
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/deploy

Install 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.

On the server · /home/deploy/.ssh/authorized_keysbash
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_keys

Try 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.

On your computerbash
ssh -i ~/.ssh/deploy_myserver deploy@SERVER_IP
ssh -i ~/.ssh/deploy_myserver deploy@SERVER_IP whoami

See what the key did

Every login of the deploy user is in the SSH log. Look at it after an assistant session.

On the serverbash
sudo journalctl -u ssh --since "1 hour ago" | grep deploy

Take 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.

On the serverbash
sudo rm /home/deploy/.ssh/authorized_keys

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.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server