Security and running as services
Hour overview
Hours 1 to 6 built and ran the service. This hour makes it safe to leave running. You check the subordinate user IDs that rootless containers depend on, remove every privilege the service does not need, keep secrets out of images and environment variables, decide which images a host will trust, and run the service as a systemd user service with Quadlet, so it starts at boot. Finally you set up updates and a rollback by digest.
Time allocation. Rootless user IDs 10, least privilege 12, secrets 8, image trust and digests 10, Quadlet services 12, auto-update and rollback 8.
Objectives. By the end of the hour you can:
- read the subordinate ID ranges for your user, and explain how they map into a container
- start a container with dropped capabilities, a read-only root file system and no privilege escalation, and prove the capability set is empty
- create a secret, mount it, and keep its value out of the container's environment
- read the image signature policy, and pin an image by digest
- write a Quadlet
.containerfile, run it as a user service, and read its logs - enable auto-update for a tag, keep a digest for a rollback, and test a reboot
Prerequisite. Hours 1 to 6. The Quadlet and reboot steps need a Linux user session with systemd and cgroup v2, such as a Linux VM. Do not run the reboot step on a workstation.
Modules
Rootless internals: subuid and subgid
A rootless container needs more user IDs than your account owns. Podman takes a range of IDs for your user from two files, /etc/subuid and /etc/subgid. Each line gives a user name, a starting ID and a count:
alice:100000:65536
Check your own range:
grep "^$USER:" /etc/subuid /etc/subgid
podman unshare cat /proc/self/uid_map
Expected: one line in each file for your user, with a start and a count. The uid_map output maps container ID 0 to your own ID, and a range of container IDs to the subordinate range. Container user 10001 from the sample image falls in that second range.
If the first command prints nothing, rootless containers fail. Ask an administrator to add a range, then run podman system migrate so Podman reads it. The newuidmap and newgidmap helpers need the uidmap package.
Common errors.
cannot find UID/GID for user: the user has no entry in/etc/subuidor/etc/subgid. Add the range, then runpodman system migrate.newuidmap: ... Operation not permitted: theuidmaphelper is missing or not set up. Install the package, then retry.
Production note. Record each service's user and its ID range in the runbook. Two services that share a range can see each other's files when they map to the same IDs.
Least privilege at run time
A container should have only the privileges the service needs. Start the sample with every privilege removed that it does not use:
podman run -d --name hard -p 127.0.0.1:8017:8000 \
--user 10001:10001 \
--cap-drop=ALL \
--read-only --tmpfs /tmp \
--security-opt no-new-privileges \
--pids-limit 50 --memory 256m \
-v hardvol:/data \
localhost/podman-app:dev
curl -s http://127.0.0.1:8017/whoami
curl -s http://127.0.0.1:8017/
Expected: whoami prints "uid": 10001. The first / prints visits: 1. The service works even though its root file system is read-only, because the only writable paths are the /data volume and the /tmp tmpfs.
Prove that the capability set is empty:
podman exec hard grep -E '^Cap(Eff|Prm)' /proc/self/status
Expected: CapEff and CapPrm are all zeros. The process has no Linux capabilities.
Prove that the root file system is read-only:
podman exec hard sh -c 'touch /app/test-file'
podman exec hard sh -c 'touch /data/test-file && echo "data is writable"'
Expected: the first command fails with Read-only file system. The second succeeds, because /data is a volume.
Clean up:
podman rm -f hard
What each flag does.
--usersets the process user, even if the image says otherwise.--cap-drop=ALLremoves every Linux capability. Add one back with--cap-addonly when the service needs it, such asNET_BIND_SERVICEfor a low port.--read-onlymakes the root file system read-only. Give writable paths explicitly with volumes or--tmpfs.--security-opt no-new-privilegesstops a process from gaining privileges through setuid programs.
--privileged to fix an error. It gives the container all capabilities and access to host devices. Find the capability or the path that the service actually needs.Common errors.
Read-only file systemfor a path the service writes: give that path a volume or--tmpfs.Permission deniedon a volume: check the ownership (hour 4, module 4).
Production note. Write down which capabilities and paths each service needs. Review the list when the service changes, and remove the flags that are no longer needed.
Secrets
A secret is a value that should not appear in an image, a file in version control, the process environment or podman inspect. Podman stores secrets on the host and mounts them into the container as files.
Create a secret from a file, mount it, and remove the local copy:
printf 'demo-password-123' > db_password.txt
podman secret create db_password db_password.txt
podman secret ls
podman run -d --name sec -p 127.0.0.1:8018:8000 --secret db_password localhost/podman-app:dev
podman exec sec cat /run/secrets/db_password
rm db_password.txt
Expected: secret ls lists the name db_password, and does not print the value. cat prints demo-password-123, because the file is mounted at /run/secrets/db_password inside the container.
Check that the value is not in the environment or the configuration:
podman inspect --format '{{json .Config.Env}}' sec | grep -c demo-password-123
Expected: 0. The value is not in the environment list.
Clean up:
podman rm -f sec
podman secret rm db_password
cat to prove the mount. A real application reads the file at the path it expects, once at start-up, and keeps the value in memory.Common errors.
no such secret: the name is wrong, or the secret was removed. Checkpodman secret ls.- The application reads an environment variable and finds nothing: change it to read the file under
/run/secrets.
Production note. Rotate a secret by creating a new one with a new name, moving the service to it, then removing the old one. Keep the host's secret store protected by host permissions, and limit who can run podman secret.
Image trust and digest pinning
Two controls decide which images a host runs. The signature policy says which images are accepted. The digest says exactly which image is accepted.
Read the policy file. On most systems it is at /etc/containers/policy.json:
cat /etc/containers/policy.json
Expected: JSON with a default entry. Look at it closely. If the default says insecureAcceptAnything, the host accepts any image from any registry. A production host should require signatures from the registries it trusts. Changing the policy is an administrator task, and it can stop pulls, so test it on a lab host first.
Pin by digest. A tag can move. A digest does not. Pull an image and read its digest:
podman pull docker.io/library/alpine:3.20
podman images --digests docker.io/library/alpine
Copy the sha256: value from the output, then run by digest:
podman run --rm docker.io/library/alpine@sha256:<digest-from-the-list> echo "pinned run"
Expected: pinned run is printed. The name now refers to one exact image. If the publisher moves the 3.20 tag tomorrow, this reference still runs the same content.
Common errors.
- Pull fails with a policy error: the policy rejects the image. Check the registry in the policy file, and ask the administrator if the registry is trusted.
- Digest not found: you copied the wrong value. Copy the full
sha256:string from theimages --digestsoutput.
Production note. Deploy by digest. Keep the tag in the build pipeline, so people can read the version, and promote the digest from one environment to the next.
Quadlet: containers as systemd services
Quadlet turns a unit file into a systemd service. You describe the container in a .container file, and systemd creates, starts and restarts it. Rootless Quadlet files go in ~/.config/containers/systemd/.
Create the file with your editor, or with the command below:
mkdir -p ~/.config/containers/systemd
cat > ~/.config/containers/systemd/web.container <<'EOF'
[Unit]
Description=Podman course sample service
[Container]
Image=localhost/podman-app:dev
ContainerName=sample-web
PublishPort=127.0.0.1:8019:8000
Environment=GREETING=from-quadlet
Volume=sample-data:/data
User=10001:10001
DropCapability=ALL
ReadOnly=true
NoNewPrivileges=true
Memory=256m
[Service]
Restart=always
[Install]
WantedBy=default.target
EOF
Load the new file and start the service:
systemctl --user daemon-reload
systemctl --user start web.service
systemctl --user status web.service --no-pager
curl -s http://127.0.0.1:8019/
Expected: status reports the service as active (running). The curl prints "message": "from-quadlet". The file web.container produces the unit web.service.
Read the logs through systemd, and the container through Podman:
journalctl --user -u web.service -n 20 --no-pager
podman ps --filter name=sample-web
Expected: the journal shows the service start and the container's output. podman ps shows sample-web running.
To see the unit that Quadlet generates without starting anything, use the generator in dry-run mode:
/usr/lib/systemd/system-generators/podman-system-generator --user --dryrun
Expected: the generator prints the service units it would create from your files. The path to the generator can differ by distribution.
Start at boot. A user service starts when you log in. To keep it running after logout and start it at boot, enable lingering for your user:
loginctl enable-linger "$USER"
systemctl --user enable web.service
Expected: enable-linger returns without output. Hour 7, module 6 tests the reboot.
Rules. Quadlet needs cgroup v2. A unit cannot set the systemd User= option to run as another user. User= in the [Container] section is different: it sets the user the container runs as.
podman generate systemd also produces units, from a running container. Podman's documentation marks it as deprecated in favour of Quadlet, because a Quadlet file describes the desired state and can be kept in version control.Common errors.
web.servicenot found after you add the file: runsystemctl --user daemon-reloadfirst.- The service fails with an image not found: the image
localhost/podman-app:devis not on this user's storage. Build it as this user. Failed to connect to bus: there is no user session. Log in as the user, or check that systemd runs in the VM.
Production note. Keep the .container files in version control, and review each change like code. Use systemctl --user restart web.service to apply a change, and check the health route after it.
Auto-update, rollback and reboot test
Auto-update keeps a service on the latest build of a tag. Add one line to the Quadlet file, AutoUpdate=registry, which tells Podman to check the registry for a new image behind the tag:
sed -i 's/^ReadOnly=true$/ReadOnly=true\nAutoUpdate=registry/' ~/.config/containers/systemd/web.container
grep -n AutoUpdate ~/.config/containers/systemd/web.container
systemctl --user daemon-reload
podman auto-update --dry-run
Expected: grep shows the new line. auto-update --dry-run lists the containers it would check and whether each has a newer image. It changes nothing.
Enable the timer that Podman ships, so checks run on a schedule:
systemctl --user enable --now podman-auto-update.timer
systemctl --user list-timers --no-pager
Expected: the timer is listed with its next run time. On most installs the timer's name is podman-auto-update.timer.
Rollback by digest. Auto-update works with tags, not with digests. An image pinned by digest does not move, so auto-update has nothing to pull. For a rollback, change the Image= line to the digest you approved, reload, and restart:
podman images --digests localhost/podman-app
Copy the digest of the approved build into Image=localhost/podman-app@sha256:<digest>, then:
systemctl --user daemon-reload
systemctl --user restart web.service
curl -s http://127.0.0.1:8019/health
Expected: the health route answers from the pinned image. Keep the tag for services that should follow a build, and the digest for services that must not change until you decide.
Reboot test. Run this only on a lab VM, not on a workstation or a shared host. Reboot, log in again if needed, and check that the service came back:
sudo reboot
After the reboot:
systemctl --user status web.service --no-pager
curl -s http://127.0.0.1:8019/health
Expected: the service is active (running) and the health route answers. If it is not, journalctl --user -u web.service shows why.
Common errors.
auto-updatechanges nothing: the image is pinned by digest, or the tag has no new build.- The service is missing after reboot: lingering is not enabled, or the unit is not enabled. Check
loginctl show-user "$USER" --property=Lingerandsystemctl --user is-enabled web.service.
Production note. Roll out by digest: test the new build, record its digest, then pin the service to it. Keep the previous digest in the runbook.
Cheat sheet
| Task | Command |
|---|---|
| Show subordinate ID ranges | grep "^$USER:" /etc/subuid /etc/subgid |
| Show the container ID map | podman unshare cat /proc/self/uid_map |
| Apply new ID ranges | podman system migrate |
| Drop all capabilities | podman run --cap-drop=ALL IMAGE |
| Read-only root with a scratch tmpfs | podman run --read-only --tmpfs /tmp IMAGE |
| No privilege gain | podman run --security-opt no-new-privileges IMAGE |
| Create a secret | podman secret create NAME FILE |
| Mount a secret | podman run --secret NAME IMAGE |
| List secrets (names only) | podman secret ls |
| Show the signature policy | cat /etc/containers/policy.json |
| Show digests | podman images --digests IMAGE |
| Reload user units | systemctl --user daemon-reload |
| Start, status, logs | systemctl --user start web.service, systemctl --user status web.service, journalctl --user -u web.service |
| Keep services running after logout | loginctl enable-linger "$USER" |
| Check auto-update without changing anything | podman auto-update --dry-run |
| Enable the scheduled auto-update timer | systemctl --user enable --now podman-auto-update.timer |
Capstone lab: a hardened service that runs at boot
Goal. Run the sample as a Quadlet service with least privilege, a secret, a pinned image and a health check, then prove it works after a service restart. Run the reboot step only on a lab VM.
printf 'demo-password-123' > db_password.txt
podman secret create db_password db_password.txt
rm db_password.txt
# Add to ~/.config/containers/systemd/web.container under [Container]:
# Secret=db_password
# HealthCmd=wget -qO- http://127.0.0.1:8000/health
# HealthInterval=10s
systemctl --user daemon-reload
systemctl --user restart web.service
sleep 15
curl -s http://127.0.0.1:8019/health
podman inspect --format '{{.State.Health.Status}}' sample-web
podman exec sample-web cat /run/secrets/db_password
Expected. The health route prints {"status": "ok"}. The health status is healthy. The cat prints the secret, and the environment does not contain it.
Verify. systemctl --user is-active web.service prints active. loginctl show-user "$USER" --property=Linger prints Linger=yes.
Clean up. systemctl --user disable --now web.service, remove ~/.config/containers/systemd/web.container, systemctl --user daemon-reload, podman secret rm db_password, and podman volume rm sample-data.