Skip to content

How to Use Claude Code to Manage Linux Servers

Use Claude Code safely on Linux servers: a working folder, CLAUDE.md, permission rules, plan mode, backup-first changes and read-only cron reports.

Steps
8
Category
AI & LLMs
Covers
ai, Automation, Claude Code
On this page 8 steps

Claude Code is usually described as a coding assistant, but it is just as useful on a Linux server. It reads logs, checks services, explains configs and writes the fix, all from the terminal. That also makes it risky: an AI agent with shell access on a production box can do real damage if you set it up carelessly.

This guide shows a safe way to use Claude Code for day-to-day server administration. You will give it a dedicated working folder, describe the server in a CLAUDE.md file, lock down what it may run with permission rules, and build a backup-first habit. At the end, you will run a read-only health report from cron.

Last tested: November 2026 with Claude Code 2.1.329 on Ubuntu 24.04 LTS. The same steps work on Debian, RHEL, Rocky Linux and AlmaLinux.

What Claude Code Should (and Shouldn’t) Do on a Server

The best results come from treating Claude Code like a capable junior admin: let it investigate and propose, and keep the final say on anything that changes the system.

Good fitKeep a human in the loopKeep manual
Reading logs and finding the cause of an errorEditing configs in /etcFirewall and SSH changes on a remote-only server
Health checks: disk, memory, failed unitsRestarting or reloading servicesDatabase DROP, TRUNCATE and mass DELETE
Explaining unfamiliar configs and cron jobsInstalling or upgrading packagesAnything involving secrets or private keys
Writing scripts, systemd units and logrotate rulesDeleting files to free spaceKernel upgrades and reboots

A wrong firewall rule or a broken sshd_config can lock you out of a remote server. Those changes are quick to type yourself and expensive to get wrong, so they are not worth delegating.

Prerequisites

  • A Linux server you can reach over SSH
  • Claude Code installed for a regular user with sudo, not root. If you haven’t done that yet, follow our guide to install Claude Code on Linux.
  • A Claude subscription or a Claude Console (API) account

Running Claude Code as a normal user means every privileged command needs sudo, which shows up clearly in the permission prompt. That makes risky commands easy to spot.

Step 1 of 8Create a Working Folder for Server Tasks

Claude Code treats the folder you start it in as its project. It loads the instructions and settings stored there, and it asks before touching files outside it. Create one folder for server work instead of starting Claude Code from / or /etc:

mkdir -p ~/server-ops/.claude
cd ~/server-ops

Start every server session from this folder. Claude Code also keeps its conversation history per folder, so claude --continue brings back your last server session.

Step 2 of 8Describe the Server in CLAUDE.md

Claude Code reads CLAUDE.md from the working folder at the start of every session. Use it to record what runs on the server and your house rules, so you don’t have to repeat them every time. Create ~/server-ops/CLAUDE.md and adapt this example:


# Server: web01 (production)

## What runs here
- Ubuntu 24.04, Nginx + PHP-FPM 8.3, MariaDB 10.11
- Sites: /var/www/example.com (WordPress)
- Backups: nightly to /backup at 02:00 (cron, root)

## Rules
- This is PRODUCTION. Ask before restarting or reloading any service.
- Never restart services between 09:00 and 18:00 IST (peak traffic).
- Before editing any file under /etc, copy it to /root/ops-backups/<date>/ first.
- Always run `nginx -t` before reloading Nginx.
- Never run DROP, TRUNCATE or DELETE without WHERE on the database.
- Do not read or print .env files, private keys or wp-config.php passwords.

## Useful commands
- Logs: journalctl -u nginx -u php8.3-fpm --since "1 hour ago"
- Health: systemctl --failed; df -h; free -m

Keep it short and factual. The Rules section matters most: it is where you write down the things a new admin would only learn the hard way. You can also run /init inside Claude Code to have it draft the file, then edit it yourself.

Note: CLAUDE.md is guidance, not enforcement. Claude follows it closely, but a rule like “never run DROP” is only guaranteed if you also block the command with a permission rule (next step).

Step 3 of 8Lock Down Commands with Permission Rules

Permission rules are enforced by Claude Code itself, whatever the model decides. Commands on the allow list run without asking, commands on the deny list are always refused, and everything else asks you first. Save the rules in ~/server-ops/.claude/settings.json:


{
  "permissions": {
    "allow": [
      "Bash(df *)",
      "Bash(free *)",
      "Bash(uptime)",
      "Bash(systemctl status *)",
      "Bash(systemctl --failed)",
      "Bash(journalctl *)",
      "Bash(nginx -t)"
    ],
    "deny": [
      "Bash(rm -rf *)",
      "Bash(systemctl stop *)",
      "Bash(reboot*)",
      "Bash(shutdown*)",
      "Read(./.env)",
      "Read(/etc/shadow)"
    ]
  }
}

The allow list only holds read-only commands, so routine checks don’t interrupt you. The deny list blocks the commands you never want an agent to run on its own. In our test, a denied rm was refused and the file stayed in place.

Important: Claude Code ignores the allow rules in a project’s settings file until you trust the folder. Start claude interactively in ~/server-ops once and accept the trust prompt. Until you do, it prints a warning like this and asks about every command:

Ignoring 3 permissions.allow entries from .claude/settings.json: this workspace has not been trusted. Run Claude Code interactively here once and accept the trust dialog

Deny rules apply either way. Inside a session, run /permissions to see which rules are active.

Step 4 of 8Choose the Right Permission Mode

The permission mode controls how much Claude Code may do without asking. Set it when you start a session with --permission-mode, or press Shift+Tab during a session to cycle through the modes:

ModeBehaviourUse on a server?
manualAsks before every edit and command not on the allow listYes. The safe default for production
planInvestigates and proposes a plan, but makes no changesYes. Ideal for troubleshooting
acceptEditsApplies file edits without asking; commands still askOnly in your own ops folder, not for /etc
bypassPermissionsRuns everything without askingNo. Claude Code itself recommends it only for sandboxes with no internet access

For a new problem, start in plan mode. Claude can read logs and configs freely, then tells you what it would change and why:

cd ~/server-ops
claude --permission-mode plan

When you agree with the plan, switch to manual mode and let it carry out the steps, approving each command as it comes.

Step 5 of 8Run Your First Server Session

Start with read-only questions to see how Claude Code works on your server. Some prompts that work well:

  • Give me a health summary of this server: disk, memory, load, failed services and anything unusual in the last hour of logs.
  • Nginx returned 502 errors around 14:20 today. Find out why from the logs and explain the cause.
  • List all cron jobs and systemd timers for every user and explain what each one does.
  • Which directories under /var use the most space, and what is safe to clean up? Don’t delete anything.
  • Review /etc/nginx/sites-enabled/example.com for security and performance issues.

Read each command before you approve it. If Claude wants to run something you don’t understand, ask it to explain first. That alone teaches you a lot about your own server.

Step 6 of 8Make Every Change Reversible

Before you let Claude Code change anything, make sure you can undo it. A simple routine that works well:

  • Back up first. The CLAUDE.md rule above makes Claude copy each file to a dated folder before editing. Check that it does, and remind it if it doesn’t.
  • Validate before you reload. Run nginx -t, apachectl configtest, sshd -t or php-fpm -t before any reload or restart.
  • Dump before database changes. Take a mysqldump of the affected tables before any UPDATE or DELETE.
  • Verify afterwards. Ask Claude to confirm the result from outside, for example with curl -I against the site, not just by checking that the service started.
sudo mkdir -p /root/ops-backups/$(date +%F)
sudo cp -a /etc/nginx/nginx.conf /root/ops-backups/$(date +%F)/

Claude Code can rewind its own file edits from a session, but it cannot undo the effects of commands, such as a package removal or a database query. Your backups cover that gap.

Step 7 of 8Test Risky Commands in a Container First

For package installs, upgrades or unfamiliar scripts, ask Claude Code to try the steps in a throwaway Docker container that matches your distribution before running them on the server:

docker run --rm -it ubuntu:24.04 bash

For example: Test the commands to install PHP 8.4 in an ubuntu:24.04 container first, show me the output, then give me the steps for this server. Errors such as missing dependencies or package conflicts show up in the container, where they cost nothing. We test every command in our tutorials the same way.

Step 8 of 8Automate Read-Only Reports

The -p (print) option runs one request without the interactive interface. Combined with --allowedTools, you can let it run specific read-only commands with nobody there to approve them. This produced a short health report in our test:

claude -p "Check disk usage and failed systemd units. Report problems in at most 5 short lines." --allowedTools "Bash(df *)" "Bash(systemctl --failed)" < /dev/null

Any command not on that list is refused, so the report can’t change anything. Wrap it in a script and run it from cron:


#!/bin/bash
# /usr/local/bin/claude-health.sh - daily read-only health report
cd /home/admin/server-ops || exit 1
claude -p "Check disk usage and failed systemd units. Report problems in at most 5 short lines, or say 'All OK'." \
  --allowedTools "Bash(df *)" "Bash(systemctl --failed)" \
  < /dev/null > /var/log/claude-health.log 2>&1

Cron jobs can’t open a browser to sign in, so authenticate them first. With a Claude subscription, run claude setup-token and follow its instructions; with a Console account, set the ANTHROPIC_API_KEY environment variable. Keep automated runs strictly read-only. Changes should always go through an interactive session where you approve them.

Security Tips for Using Claude Code on Servers

  • Never run it as root. Use a normal user with sudo, so privileged commands stand out in every prompt.
  • Never use --dangerously-skip-permissions on a real server. It removes every safety check.
  • Keep secrets out of the conversation. Deny reads of .env files and keys, and don’t paste passwords into prompts. Everything you send goes to the model.
  • Be careful on remote-only servers. Make firewall and SSH changes yourself, with a second session open, so a mistake can’t lock you out.
  • Keep Claude Code updated. Run claude update regularly. Permission handling improves with each release.

Extend It with MCP Servers

The Model Context Protocol lets Claude Code use extra tools, such as your monitoring system, a database client or GitHub. That is where it becomes really useful for operations: check Grafana for when the CPU spike started, then find the matching log entries. See our list of the best MCP servers for developers and sysadmins to get started, and add each one with the same care as a new sudo rule.

Conclusion

Claude Code can take much of the routine work out of running Linux servers, from reading logs to writing configs. Used safely, it works inside its own folder, knows the server from CLAUDE.md, is fenced in by permission rules, and makes no change you can’t undo. Start with read-only tasks and plan mode, add allow rules as you gain confidence, and keep the risky changes in your own hands.

Deciding between AI terminal tools? Read our comparison of Claude Code vs Codex CLI vs Antigravity CLI.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top