Self-Hosted GitHub Actions Runners on Linux
GitHub’s hosted runners cover most needs, and RHEL images recently joined the list. Self-hosting is for the cases they do not: a GPU, a specific kernel, a private network, or more machine than the hosted tiers offer.
The security model is the part to settle first, because it determines whether this is a reasonable idea at all.
Read this before installing anything
A self-hosted runner executes whatever a workflow tells it to. That is its function. Which means:
Never attach a self-hosted runner to a public repository. A pull request from a fork can modify the workflow and run arbitrary code on your machine. GitHub documents this explicitly. Because a default runner is persistent, that code can also tamper with later jobs: plant something in a cache, modify a tool on PATH, read secrets a subsequent job uses.
The safe configurations are:
- A private repository where everyone who can open a PR is already trusted
- An ephemeral runner in a container or VM, destroyed after each job
- Both
# the user to create, which is not your account and not root
sudo useradd -m -s /bin/bash gh-runner
Our security best practices guide covers the general posture, and our capabilities guide covers granting narrow privileges if a workflow genuinely needs them.
Installing
sudo -iu gh-runner
mkdir -p ~/actions-runner && cd ~/actions-runner
# check the current version and checksum on GitHub's download page
curl -o runner.tar.gz -L \
https://github.com/actions/runner/releases/download/v2.XXX.X/actions-runner-linux-x64-2.XXX.X.tar.gz
# verify before extracting
echo "<published-sha256> runner.tar.gz" | sha256sum -c
tar xzf runner.tar.gz
Verify the checksum. GitHub publishes one on the release page. Our checksums guide covers why a hash from the same page proves integrity rather than origin, which is the right level of assurance here.
# register, using a token from the repository or organisation settings
./config.sh \
--url https://github.com/youruser/yourrepo \
--token ABCDEF... \
--name "$(hostname)-gpu" \
--labels self-hosted,linux,x64,gpu,cuda \
--work _work \
--unattended
# dependencies the runner needs
sudo ./bin/installdependencies.sh
The registration token is short-lived, which is why it is generated in the web UI each time rather than stored.
As a systemd service
The bundled svc.sh works and writes a unit you did not review. Writing your own is better:
# /etc/systemd/system/gh-runner.service
[Unit]
Description=GitHub Actions runner
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=gh-runner
Group=gh-runner
WorkingDirectory=/home/gh-runner/actions-runner
ExecStart=/home/gh-runner/actions-runner/run.sh
Restart=always
RestartSec=10
KillSignal=SIGTERM
TimeoutStopSec=5min
# modest hardening, without breaking builds
NoNewPrivileges=yes
PrivateTmp=yes
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictSUIDSGID=yes
# bound the damage a runaway build can do
MemoryMax=16G
CPUQuota=800%
TasksMax=8192
[Install]
WantedBy=multi-user.target
sudo systemctl enable --now gh-runner
journalctl -u gh-runner -f
TimeoutStopSec=5min gives a running job time to finish on shutdown rather than being cut off, per our timeout guide.
MemoryMax and CPUQuota matter more than they look. A build with a runaway parallel compile will otherwise take the machine down, and our cgroups guide covers the mechanism.
Note what is not here: ProtectSystem=strict and ProtectHome break builds that write where builds write. Our service hardening guide covers the full set, and CI is a case where the useful subset is smaller than usual.
Ephemeral runners
The configuration that fixes the persistence problem.
./config.sh \
--url https://github.com/org/repo \
--token ABCDEF... \
--ephemeral \
--labels self-hosted,linux,ephemeral \
--unattended
--ephemeral makes the runner accept one job and then deregister. Each job starts clean, so nothing a job leaves behind affects the next one.
The cost is that something must register a new runner for the next job. In a container, that something is the container start:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y --no-install-recommends \
curl ca-certificates git jq libicu74 sudo \
&& rm -rf /var/lib/apt/lists/*
RUN useradd -m runner
USER runner
WORKDIR /home/runner
# install the runner here
COPY --chown=runner entrypoint.sh /home/runner/
ENTRYPOINT ["/home/runner/entrypoint.sh"]
# entrypoint registers, runs one job, exits; the container is discarded
#!/usr/bin/env bash
set -euo pipefail
token=$(curl -sX POST -H "Authorization: Bearer $GH_PAT" \
"https://api.github.com/repos/$REPO/actions/runners/registration-token" | jq -r .token)
./config.sh --url "https://github.com/$REPO" --token "$token" \
--ephemeral --unattended --labels self-hosted,ephemeral
exec ./run.sh
podman run --rm -e GH_PAT -e REPO=org/repo my-runner:latest
For anything beyond a couple of runners, Actions Runner Controller on Kubernetes manages this properly, scaling ephemeral runners per queued job. Our k3s guide covers a small cluster to run it on.
Targeting it from a workflow
jobs:
build:
runs-on: [self-hosted, linux, gpu]
steps:
- uses: actions/checkout@v4
- run: nvidia-smi
- run: make test
Labels match as a conjunction. A job requesting self-hosted, linux and gpu runs only on a runner holding all three.
# mix hosted and self-hosted in one workflow
jobs:
lint:
runs-on: ubuntu-latest # hosted, cheap, untrusted-safe
gpu-test:
runs-on: [self-hosted, gpu] # yours
needs: lint
That split is the sensible shape: hosted runners for everything that does not need your hardware, self-hosted only for the steps that do.
The GPU case
The most common reason to self-host, and it needs the driver in place on the host.
# on the host
nvidia-smi
rocm-smi
Our ROCm and CUDA guide covers getting those working, and our NVIDIA drivers guide covers the driver.
# the runner user needs access to the device nodes
ls -l /dev/nvidia* /dev/dri/renderD128
sudo usermod -aG video,render gh-runner
id gh-runner
Group membership is what grants access to the render node, as our /dev guide covers. A job that reports no GPU while nvidia-smi works for you is almost always this.
# if jobs run in containers, the device must be passed through
jobs:
train:
runs-on: [self-hosted, gpu]
container:
image: nvidia/cuda:12.6.0-runtime-ubuntu24.04
options: --gpus all
Disk, which is what actually breaks
du -sh ~/actions-runner/_work/*
df -h /home
The work directory is not cleaned automatically. One subdirectory per repository, holding checkouts, caches and build output, growing indefinitely until the disk fills and every job starts failing for reasons that look unrelated.
# a cleanup timer
sudo tee /etc/systemd/system/gh-runner-clean.service >/dev/null <<'EOF'
[Unit]
Description=Prune GitHub runner work directory
[Service]
Type=oneshot
User=gh-runner
ExecStart=/usr/bin/find /home/gh-runner/actions-runner/_work -mindepth 2 -maxdepth 2 \
-type d -mtime +7 -exec rm -rf {} +
EOF
sudo tee /etc/systemd/system/gh-runner-clean.timer >/dev/null <<'EOF'
[Unit]
Description=Prune GitHub runner work weekly
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target
EOF
sudo systemctl enable --now gh-runner-clean.timer
Add a container prune too, if jobs use Docker or Podman:
podman system prune -af --filter 'until=168h'
Our timers guide covers the unit and our disk usage guide covers finding what grew.
Secrets
steps:
- run: ./deploy.sh
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
A secret passed to a self-hosted runner is on your machine, in the environment of a process, and in anything the job writes. On a persistent runner it may also be in a cache or a log a later job can read.
Which is another argument for ephemeral runners, and for keeping deployment secrets out of CI entirely where a workload identity arrangement is available instead. Our secrets management guide covers the file-based alternatives.
# check what the runner is leaving around
sudo -u gh-runner env | grep -iE 'token|key|secret'
grep -rl 'ghp_\|github_pat_' ~/actions-runner/_work/ 2>/dev/null
Checking on it
systemctl status gh-runner
journalctl -u gh-runner --since '1 hour ago'
# is it connected, per the repository settings page
# Settings > Actions > Runners
# what is it doing right now
systemd-cgtop -m | head
ps -u gh-runner -o pid,etime,pcpu,rss,args --sort=-pcpu | head
A runner showing offline in the UI while the service is running is usually network: the runner holds a long-lived connection out to GitHub, and a firewall or proxy dropping idle connections breaks it. It needs outbound HTTPS and nothing inbound, which is one of the better properties of this design.
Frequently Asked Questions
Why run a self-hosted Actions runner instead of a hosted one?
For hardware or access a hosted runner cannot provide: a GPU, a specific kernel version, more RAM or cores than the hosted tiers offer, an Arm or other architecture, or a private network containing the thing you are testing against. Cost at high volume is a secondary reason.
Is it safe to attach a self-hosted runner to a public repository?
No, and GitHub documents this. A pull request from a fork can run arbitrary code on the runner, and because the runner is persistent that code can tamper with later jobs. Self-hosted runners belong on private repositories, or in ephemeral containers that are destroyed after each job.
What is an ephemeral runner?
A runner configured to accept one job and then deregister itself, so each job starts from a clean state. It removes the persistence that makes a compromised job dangerous, and it is the configuration to use for anything where untrusted code might run.
Should the runner service run as root?
No. Create a dedicated unprivileged user for it, since the runner executes whatever a workflow tells it to. If the workflow needs privileged operations, grant them narrowly through specific sudo rules or capabilities rather than running the whole runner as root.
How do I use a specific label to target my runner?
Assign labels when registering the runner, then set runs-on in the workflow to a list containing self-hosted plus your labels. Matching is a conjunction, so a job requesting self-hosted and gpu only runs on a runner that has both.
Where does the runner keep its work and how do I clean it up?
In a work directory under the runner installation, with one subdirectory per repository. It is not cleaned automatically, so caches and checkouts accumulate until the disk fills. Either prune it on a timer or use ephemeral runners in containers so the state goes away with the job.