CI/CD and automation · Advanced · 16 min
Run a self-hosted GitHub Actions runner safely
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GitHub Actions runner 2.338.2
You will have an ephemeral GitHub Actions runner on Ubuntu 26.04, running under its own user as a systemd service, limited to one private repository and kept patched.
On this page
GitHub-hosted runners are a fresh virtual machine for every job, thrown away afterwards. A self-hosted runner is a program on a machine you own that asks GitHub for jobs and runs them. It is worth having when the hosted ones do not fit: you need native ARM builds, the job must reach something on a private network (a database, an internal registry, a staging server), or you run enough builds that your own hardware works out cheaper than paid minutes.
The cost is that the safety the hosted runners give you for free becomes your job. Whatever a workflow runs, it runs on your machine, as the runner's user, with whatever that user can reach. A careless or hostile job can leave a backdoor behind for the next job, read secrets, or use the machine to attack your network.
This tutorial sets the runner up so that the damage one job can do stays small. It gets its own unprivileged user, a checked download, a one-hour registration token that never lands in your shell history, ephemeral mode (one job, then it unregisters), a systemd service, a label that workflows must ask for, access to one private repository only, and a patching routine. Commands run on the runner server unless a step says otherwise.
Step 1 · Decide whether you need it, and check the repository is private
Before installing anything, apply the rule that comes first: never attach a self-hosted runner to a public repository. Anyone on GitHub can fork a public repository and open a pull request, and depending on your settings that pull request can run its own workflow code on your machine. GitHub's own security guidance says self-hosted runners should almost never be used for public repositories.
Then be honest about why you want one. Good reasons:
- Architecture. You build ARM images and want them built natively on an ARM server instead of slowly under emulation.
- Network. Deploy or test jobs need to reach hosts that are not on the internet.
- Cost or hardware. You already pay for a machine with spare capacity, or you need a GPU or more memory than the hosted runners offer.
If none of these apply, stay on GitHub-hosted runners. Note that a runner only needs outbound HTTPS on port 443 to GitHub. Do not open any inbound ports for it.
If your office or server connection is on metered or prepaid data, as is common for teams across Africa, count the cost: every job downloads its actions, packages and images through your connection, and the runner itself updates automatically. A self-hosted runner can save hosted minutes and still burn through a data bundle.
Check it worked
Open the repository on GitHub. Next to its name you see the label Private. If it says Public, stop here and use GitHub-hosted runners for that repository. You can also write down, in one sentence, which of the three reasons above applies to you.
Step 2 · Create a dedicated user and folder
The runner must not run as root or as your own account. Give it a user of its own that is not in the sudo group, and never add it to the docker group: membership of that group is equal to root on the machine. The tutorial on giving a service its own system user explains the pattern in more depth.
Use a server or virtual machine that does nothing else. Do not put a runner on a machine that also hosts a production database or holds other people's files.
Create a system user called ghrunner with a home directory, and a folder for the runner owned by it:
sudo useradd --system --create-home --home-dir /home/ghrunner --shell /usr/sbin/nologin ghrunner
sudo install -d -o ghrunner -g ghrunner -m 755 /opt/actions-runnerThe runner updates itself, so it must be able to write to its own folder. That is why /opt/actions-runner belongs to ghrunner rather than root. Mode 755 lets your own account enter the folder and run the setup scripts with sudo, while only ghrunner can change what is inside. On a machine with no other users, that is enough.
Check it worked
id ghrunner
ls -ld /opt/actions-runnerid shows something like uid=998(ghrunner) gid=998(ghrunner) groups=998(ghrunner), with no sudo or docker in the list. ls shows drwxr-xr-x with ghrunner ghrunner as owner and group.
Step 3 · Download the runner and check its checksum
Work in the runner's folder as the ghrunner user. Pick the archive for your processor:
cd /opt/actions-runner
VERSION=2.338.2
case "$(uname -m)" in
x86_64) ARCH=x64 ;;
aarch64) ARCH=arm64 ;;
*) echo "Unsupported processor: $(uname -m)"; ARCH= ;;
esac
[ -n "$ARCH" ] && sudo -u ghrunner curl -fsSLO "https://github.com/actions/runner/releases/download/v${VERSION}/actions-runner-linux-${ARCH}-${VERSION}.tar.gz"If you see Unsupported processor, stop: GitHub publishes Linux runner archives for x64 and arm64 only, and nothing was downloaded. Keep this terminal open for the next commands, because they reuse VERSION and ARCH.
Now open the release page at https://github.com/actions/runner/releases/tag/v2.338.2 in your browser. Its notes list a SHA-256 checksum for each archive. Copy the one for your file (actions-runner-linux-x64-2.338.2.tar.gz or actions-runner-linux-arm64-2.338.2.tar.gz) and paste it in place of PASTE_THE_CHECKSUM_HERE below:
EXPECTED=PASTE_THE_CHECKSUM_HERE
echo "${EXPECTED} actions-runner-linux-${ARCH}-${VERSION}.tar.gz" | sudo -u ghrunner sha256sum -cOnly if that prints OK, unpack it as ghrunner:
sudo -u ghrunner tar -xzf "actions-runner-linux-${ARCH}-${VERSION}.tar.gz"The runner is built on .NET and needs a few system libraries, ICU among them. If a later step complains about a missing library, the archive includes a script that installs them with apt:
sudo ./bin/installdependencies.shCheck it worked
sha256sum -c printed actions-runner-linux-<arch>-2.338.2.tar.gz: OK. If it printed FAILED, delete the archive and download it again before unpacking. After unpacking, ls /opt/actions-runner lists config.sh, run.sh and env.sh, plus bin and externals folders. There is no svc.sh yet: config.sh creates it in the next step.
Step 4 · Register it as an ephemeral runner with a short-lived token
A normal runner stays registered and takes job after job, so anything one job leaves behind (a modified tool, a hidden process, a stolen credential file) is waiting for the next. An ephemeral runner takes exactly one job, then GitHub removes its registration. Combined with a fresh machine for each registration, that is the closest you get to hosted-runner isolation.
On GitHub, in your browser:
- Open the private repository and go to Settings, then Actions, then Runners.
- Click New self-hosted runner and choose Linux and your architecture.
- The page shows commands with a token in them. Copy only the token. It expires after one hour.
Registering at repository level means only this repository can send jobs to the runner. Back on the server, run the configuration as ghrunner. Replace your-org/your-repo with your repository. The command leaves out --token on purpose, so the script asks for it and the token never lands in your shell history:
cd /opt/actions-runner
sudo -u ghrunner ./config.sh \
--url https://github.com/your-org/your-repo \
--name "$(hostname)-ephemeral" \
--labels arm64-build \
--work _work \
--ephemeralPaste the token when it asks for it. If it asks anything else, press Enter to accept the default.
About the flags:
--ephemeralmakes this runner take one job and then unregister.--labels arm64-buildadds a custom label on top of the defaults GitHub gives every self-hosted runner (self-hosted, the OS and the architecture). Name it after what the machine is for;arm64-buildis an example, so replace it with your own.- Do not add
--disableupdate. If you turn automatic updates off, you must update the runner yourself within the window GitHub sets, or GitHub stops sending jobs to it.
Teams that register runners from automation should look at the just-in-time configuration API instead (generate-jitconfig). It creates a single-use runner configuration directly, so no registration token sits on the machine at all.
Check it worked
The script ends with something like Settings Saved. Refresh Settings > Actions > Runners in the repository. Your runner is listed with status Offline (it is registered but not running yet) and the labels self-hosted, Linux, your architecture and arm64-build. Back on the server, ls /opt/actions-runner now also shows svc.sh, which config.sh wrote during registration.
Step 5 · Install it as a systemd service
The svc.sh script that config.sh created writes a systemd unit for the runner. Pass the user so the service runs as ghrunner and not as root:
cd /opt/actions-runner
sudo ./svc.sh install ghrunner
sudo ./svc.sh start
sudo ./svc.sh statusUbuntu's needrestart tool restarts services after library updates. If it restarts the runner in the middle of a job, the job fails. Tell it to leave runner services alone:
echo '$nrconf{override_rc}{qr(^actions\.runner\..+\.service$)} = 0;' | sudo tee /etc/needrestart/conf.d/actions_runner_services.confCheck it worked
svc.sh status shows the unit, named something like actions.runner.your-org-your-repo.<hostname>-ephemeral.service, as active (running). In the repository's Runners page the runner now shows Idle. Confirm the process runs as ghrunner:
ps -o user=,cmd= -C Runner.ListenerThe line starts with ghrunner.
Step 6 · Send it one job and re-arm it
Workflows choose a runner with runs-on. Ask for every label you rely on, so a job never lands on a different self-hosted machine by accident. Add this file to the repository as .github/workflows/self-hosted-smoke.yml on your laptop, then commit and push it:
name: Self-hosted smoke test
on: workflow_dispatch
permissions:
contents: read
jobs:
smoke:
runs-on: [self-hosted, linux, arm64-build]
steps:
- run: |
uname -m
whoamiworkflow_dispatch means the job only runs when someone with write access starts it by hand, and permissions: contents: read keeps the job's GITHUB_TOKEN to the minimum. On GitHub, open Actions, pick Self-hosted smoke test and click Run workflow.
When the job finishes, the ephemeral runner unregisters and its process exits. The service does not restart it, because the unit has no restart rule, so svc.sh status shows it as inactive or failed. That is the point: the machine takes no more jobs until you decide it is clean.
The safest way to re-arm is to rebuild the virtual machine from its image and repeat this tutorial from the dedicated-user step, usually with a script or tool such as cloud-init. If you must reuse the machine, at least remove the old job files and register again with a new token. The next command deletes everything in the runner's work folder; check the path before you run it:
sudo sh -c 'cd /opt/actions-runner && ./svc.sh uninstall'
sudo rm -rf /opt/actions-runner/_workThen get a new token from New self-hosted runner and run the config.sh command and the svc.sh install, start commands from the previous two steps again.
An ephemeral runner should clear its local registration files (.runner and .credentials) when it unregisters. If config.sh instead says the runner is already configured, remove the old local configuration first, then run config.sh again:
cd /opt/actions-runner
sudo -u ghrunner ./config.sh removeIt asks for a token; use a fresh one from the Runners page. If it fails because GitHub no longer knows the runner, delete the leftover files by hand with sudo -u ghrunner rm -f .runner .credentials .credentials_rsaparams and try config.sh again. Reusing a machine does not remove anything a job installed elsewhere on it, which is why rebuilding is better.
Check it worked
In the workflow run's log, the step prints your architecture (aarch64 or x86_64) and ghrunner. The repository's Runners page no longer lists the runner once the job is done. After re-arming, a new entry appears and shows Idle.
Step 7 · Restrict who can use it
A repository-level runner, like the one you made, already serves only that repository. In an organisation, runners are often registered at organisation level instead, and then every repository the runner can serve is a way in. Two settings limit that:
- Runner groups. In the organisation, go to Settings > Actions > Runner groups. Every organisation has a default group; organisations on the Team plan or above can create more. Create a group for this kind of runner, and under Repository access choose Selected repositories and tick only the repositories that need it. Leave public repositories off; groups allow only private repositories by default.
- Register into the group. Add
--runnergroup <group-name>toconfig.shso the runner lands in that group, and target the group in workflows:
runs-on:
group: arm64-builders
labels: [self-hosted, linux, arm64-build]On the repository side, also look at Settings > Actions > General: keep the default GITHUB_TOKEN permissions read-only, and require approval before workflows from outside collaborators run. The tutorial on hardening GitHub Actions workflows covers those settings and the workflow triggers to avoid, such as pull_request_target with a checkout of the pull request's code.
Check it worked
Open the runner group's page. Under Repository access you see only the repositories you picked. In a repository that is not on the list, a job with runs-on: group: arm64-builders waits in the queue and never starts.
Step 8 · Keep the host patched and clean up when you are done
The runner updates itself, but GitHub's docs are clear that the operating system and everything else on the machine are your responsibility. Ubuntu 26.04 servers install security updates automatically through unattended-upgrades. Make sure it is installed and turned on:
sudo apt update
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgradesAnswer Yes to install stable updates automatically. Add a habit as well: once a month, rebuild the runner machine from an updated image, or at least run sudo apt full-upgrade and reboot at a time when no job is running.
If you need many runners, or runners that start and stop with demand, do not scale this by hand. Actions Runner Controller (ARC) is GitHub's Kubernetes operator for self-hosted runners. You install it with Helm, and it creates ephemeral runner pods in autoscaling scale sets, so each job gets a fresh runner without the re-arm routine above.
When you no longer need the runner, remove all of it. The following stops the service and deletes the runner's folder, user and home directory:
sudo sh -c 'cd /opt/actions-runner && ./svc.sh stop && ./svc.sh uninstall'
sudo rm -rf /opt/actions-runner
sudo userdel --remove ghrunner
sudo rm -f /etc/needrestart/conf.d/actions_runner_services.confIf the runner is still registered (it never took a job), also click it in Settings > Actions > Runners and choose Remove runner, and delete .github/workflows/self-hosted-smoke.yml from the repository.
Check it worked
apt-config dump APT::Periodic::Unattended-Upgradeprints APT::Periodic::Unattended-Upgrade "1";. After cleanup, id ghrunner prints no such user, and the repository's Runners page lists no self-hosted runners.
Next steps
- Apply the same least-privilege user pattern to other services with giving a service its own system user and directories.
Related tutorials
- Give a service its own system user and directoriesYou will create a locked system account for a small service, give it a config folder it can read but not change plus state and log folders only it can write, and declare the same thing the packaged way with systemd-sysusers and DynamicUser=.
- How to Create Your First GitHub Actions WorkflowYou will write a workflow file that runs on every push and every pull request, check its syntax on your own computer with actionlint, push it, and read the run on GitHub.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials