Lesson 3 of 7 in Security basics for DevOps

Least privilege for people, jobs and containers

Core11 min

Least privilege means every person, program and container gets the smallest set of permissions it needs to do its job and nothing more. When something is compromised or makes a mistake, the damage stays inside that small set.

Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.

Read these first: Shift left and think like a defender

The problem this solves

The quickest way to make an error message disappear is to grant more access: run as root, use an admin role, open the permissions wide. It works, so it stays. Months later a deploy job that only needs to read one storage bucket can delete every bucket in the account.

The cost shows up when something goes wrong. A bug in your app, a stolen token or a bad script can do whatever its identity is allowed to do. A narrower identity turns a disaster into a contained, boring failure.

How it works

A policy sits between an identity and what it may touch
A policy sits between an identity and what it may touchAn identity such as a person or job is checked against a policy, which allows access to one resource and denies the rest.1 asks2 allowed3 deniedIdentityperson, job or containerPolicyexplicit allow listOne resourcee.g. read one bucketEverything elsedenied by default
  • People and clients
  • Security
  • Data

An identity such as a person or job is checked against a policy, which allows access to one resource and denies the rest.

  1. Identity to Policy: 1 asks
  2. Policy to One resource: 2 allowed
  3. Policy to Everything else: 3 denied

Every system has the same shape. An identity asks to do something, a policy decides, and the resource either responds or refuses. Least privilege is about writing the policy as an allow list for exactly the actions and resources the job needs, with everything else denied by default.

It applies at three layers. In cloud accounts it is the IAM policy attached to a role. On a Linux host it is the user a service runs as and the files that user can touch. In a container it is the user inside the container, plus which kernel capabilities and writable paths it keeps.

A practical way to reach a narrow policy is to start small and widen it when a real error tells you the exact missing action. Starting wide and hoping to tighten later rarely happens.

Try it yourself

These steps need Docker for the container parts. The cloud policy is only read and never applied, so no account is needed. Use a Linux or macOS shell, or WSL.

  1. 1
    See who a container runs as by default
    docker run --rm alpine:3.22 id

    The output starts with uid=0(root). Most images run as root unless the image author said otherwise.

  2. 2
    Drop to a normal user and remove extras
    docker run --rm --user 1000:1000 --read-only --cap-drop ALL alpine:3.22 sh -c 'id; touch /should-fail'

    --user picks a non-root user, --read-only makes the root filesystem unwritable, and --cap-drop ALL removes the special kernel powers root normally gets. The touch is expected to fail with a read-only filesystem error.

  3. 3
    Bake a non-root user into an image
    docker build -t nonroot-demo - <<'EOF'
    FROM alpine:3.22
    RUN addgroup -S app && adduser -S -G app app
    USER app
    CMD ["id"]
    EOF
    docker run --rm nonroot-demo

    The USER line makes every later instruction and the running container use app. Remove the image with docker rmi nonroot-demo when you finish.

  4. 4
    Read a narrow cloud policy
    cat <<'EOF'
    {
      "Version": "2012-10-17",
      "Statement": [{
        "Effect": "Allow",
        "Action": ["s3:GetObject"],
        "Resource": "arn:aws:s3:::<bucket-name>/reports/*"
      }]
    }
    EOF

    This allows one action on objects under one prefix of one bucket. Compare it with a policy that uses "Action": "*" and "Resource": "*", which allows everything. It is printed only so you can read it.

What breaks, and where to look

  • After adding USER app, the container fails with EACCES: permission denied or Permission denied when it writes a file.

    WHY IT HAPPENS
    Files copied into the image are owned by root. The new non-root user cannot write to directories or files it does not own.
    CHECK FIRST
    Run docker run --rm <image> ls -ln /app to see numeric owners. Give the user ownership with COPY --chown=app:app or a chown in the Dockerfile, and write only to a dedicated data directory.
  • A job fails with User: arn:aws:iam::<account-id>:user/<name> is not authorized to perform: <action> on resource: <arn>.

    WHY IT HAPPENS
    The policy is doing its job. The identity tried an action that no statement allows, either because the policy is too narrow or because the job does something you did not expect.
    CHECK FIRST
    Read the action and resource named in the message. Decide whether the job really needs it, then add exactly that action on exactly that resource, not a wildcard.
  • A service works only after someone ran chmod 777 on a directory or sudo on the command.

    WHY IT HAPPENS
    The real mismatch is a wrong owner, group or user. Widening permissions hides it and lets every user on the host read and change those files.
    CHECK FIRST
    Use ls -l <path> and id <user> to compare the file owner and the service user, then fix ownership instead of opening the mode.

When not to use this

  • On a personal laptop experiment, a root container is fine. Keep the discipline for anything shared, deployed or holding real credentials.
  • Non-root does not make a container safe by itself. It limits the damage, and you still need patched images, narrow network access and secrets kept out of the image.

Check yourself

What is the idea behind least privilege?

Each identity gets only the permissions it needs for its job, so a mistake or a compromise can only do limited damage.

Why does a container that worked as root fail after you add USER app?

It tries to write to files owned by root. Fix ownership of the paths it needs instead of going back to root.

When a policy denies an action, what should you do?

Check whether the job really needs that action, then allow that exact action on that exact resource, not a wildcard.

Lesson 88 of 92 in the DevOps path

Search

Search cheat sheets and projects