Skip to main content

Linux and servers · Advanced · 18 min

Confine a program with AppArmor, and how SELinux compares

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · AppArmor 5.0.2

You will write, check and enforce an AppArmor profile for a small report script on Ubuntu 26.04, read the denials it logs, and know how the same job looks with SELinux on RHEL-family servers.

On this page

Linux file permissions ask one question: is this user allowed to touch this file? A program running as root passes that test for almost everything. AppArmor adds a second question that the kernel asks on every file open, program start and socket: is this program allowed to do it? The answer comes from a profile, a text file listing what the program needs. Anything not listed is refused, even for root.

In this tutorial you confine a small root script called disk-report. It writes a disk usage report and pings a monitoring URL. You will get a first draft from AppArmor's tools, replace it with a profile you write and understand line by line, check it with the parser before loading it, run it in complain mode and then enforce mode, and fix a denial from the log. Then you look at why Ubuntu ships dozens of near-empty profiles for desktop apps, and how SELinux on Red Hat, Rocky and Alma Linux does the same job differently.

Ubuntu 26.04 ships AppArmor 5.0. The profiles here use the 5.0 rule format and parse with apparmor_parser 5.0.2 from the 26.04 updates archive.

Step 1 · Check that AppArmor is running

AppArmor is installed and enabled on Ubuntu by default. Install the command-line tools, which are a separate package:

Bash
sudo apt-get install -y apparmor apparmor-utils

Ask the kernel whether AppArmor is on, then list what is loaded:

Bash
aa-enabled
sudo aa-status

aa-enabled prints Yes. aa-status prints how many profiles are loaded, then a count for each mode (enforce, complain, unconfined and others), then the processes that have a profile. On a fresh 26.04 server most profiles are in unconfined mode (the step on user namespaces below explains why) and a few dozen are in enforce mode.

Profiles live as plain files in /etc/apparmor.d:

Bash
ls /etc/apparmor.d
ls /etc/apparmor.d/abstractions

Three folders matter here. abstractions/ holds reusable rule sets (base, bash, nameservice and so on) that profiles include. tunables/ holds variables such as @{PROC} and @{HOME}. local/ holds your own additions to packaged profiles, which survive package upgrades.

On some low-cost VPS plans built on OpenVZ or LXC rather than full virtual machines, aa-enabled says No or the guest cannot load profiles at all. If that is your case, ask your provider for a KVM-based plan before you rely on AppArmor.

Check it worked

aa-enabled prints Yes, aa-status shows a non-zero number of profiles in enforce mode, and ls /etc/apparmor.d/abstractions lists base and bash.

Step 2 · Create the program to confine

The script writes a report to /var/lib/disk-report/report.txt and, if /etc/disk-report/ping-url exists, calls that URL so a monitor knows the job ran. Create its folders and the script:

Bash
sudo install -d -m 755 /etc/disk-report /var/lib/disk-report
sudo tee /usr/local/bin/disk-report > /dev/null <<'SCRIPT'
#!/usr/bin/bash
# Write a small disk usage report, then ping a monitor URL if one is set.
set -euo pipefail
out=/var/lib/disk-report/report.txt
{
  date -u +%Y-%m-%dT%H:%M:%SZ
  df -h --output=target,pcent /
} > "$out"
if [[ -r /etc/disk-report/ping-url ]]; then
  curl -q -fsS --max-time 5 -o /dev/null "$(cat /etc/disk-report/ping-url)"
fi
echo "report written to $out"
SCRIPT
sudo chmod 755 /usr/local/bin/disk-report

curl -q tells curl to skip ~/.curlrc, so the profile does not need to allow reading root's home folder.

You need something for it to ping. In a second terminal on the server, start a stand-in monitor that answers on port 9100 and leave it running:

Bash
mkdir -p ~/apparmor-demo && cd ~/apparmor-demo && python3 -m http.server 9100 --bind 127.0.0.1

Back in the first terminal, point the script at it and run it once, unconfined:

Bash
echo 'http://127.0.0.1:9100/' | sudo tee /etc/disk-report/ping-url
sudo disk-report
cat /var/lib/disk-report/report.txt

Check it worked

The script prints report written to /var/lib/disk-report/report.txt, the report holds a timestamp and a Use% line for /, and the second terminal shows a "GET / HTTP/1.1" 200 line.

Step 3 · Let the tools draft a profile

aa-autodep writes a skeleton profile for a program: the abstractions it guesses from the interpreter, and permission to read the program itself. --no-reload writes the file without loading it:

Bash
sudo aa-autodep --no-reload disk-report
sudo cat /etc/apparmor.d/usr.local.bin.disk-report

The file is named after the program's path with / turned into ., and reads:

Text
abi <abi/5.0>,

include <tunables/global>

/usr/local/bin/disk-report flags=(complain) {
  include <abstractions/base>
  include <abstractions/bash>

  /usr/bin/bash ix,
  /usr/local/bin/disk-report r,

}

flags=(complain) means the profile logs what it would refuse instead of refusing it. /usr/bin/bash ix lets the script start its interpreter and run it under the same profile.

aa-genprof builds on this draft interactively. It loads the skeleton in complain mode, asks you to use the program in another terminal, then reads the log and offers a rule for each thing the program did:

Bash
sudo aa-genprof disk-report

While it waits, run sudo disk-report in another terminal, then press S to scan the log. For each event it shows the program, the path or socket and a suggested rule, and you choose Allow, Deny, or for a program start, Inherit (ix). When there are no more events, press F to finish and save. The result works, but it is a list of everything that happened in one run, sometimes with paths broader or narrower than you want. Treat it as notes for the profile you write next, not the final profile.

Check it worked

/etc/apparmor.d/usr.local.bin.disk-report exists and contains flags=(complain), include <abstractions/bash> and /usr/bin/bash ix,.

Step 4 · Write the profile by hand

Replace the draft with a full profile. This overwrites the file aa-autodep wrote:

Bash
sudo tee /etc/apparmor.d/usr.local.bin.disk-report > /dev/null <<'PROFILE'
abi <abi/5.0>,
include <tunables/global>

profile disk-report /usr/local/bin/disk-report flags=(complain) {
  include <abstractions/base>
  include <abstractions/bash>
  include <abstractions/nameservice-strict>
  include <abstractions/openssl>

  # The script and the programs it starts, all under this profile
  /usr/local/bin/disk-report r,
  /usr/bin/bash ix,
  /usr/bin/date ix,
  /usr/bin/df ix,
  /usr/bin/cat ix,
  /usr/bin/curl ix,

  # What it reads and writes
  @{PROC}/@{pid}/mountinfo r,
  /etc/disk-report/ r,
  /etc/disk-report/ping-url r,
  /var/lib/disk-report/ r,
  /var/lib/disk-report/report.txt w,

  # The ping to the monitor
  network inet stream,
  network inet6 stream,

  # Never, even if a later rule would allow it
  deny /etc/shadow r,

  include if exists <local/disk-report>
}
PROFILE

What each part does:

  • profile disk-report /usr/local/bin/disk-report gives the profile a short name for logs and commands, and attaches it to that path. Whenever the kernel starts that file, the profile applies.
  • The abstractions cover the common ground: base for shared libraries and /dev/null, bash for the shell's own files, nameservice-strict for /etc/hosts and host name lookups, openssl for the config file curl's TLS library reads at start-up.
  • ix means "may execute, and the new program inherits this profile". The alternatives are px (switch to that program's own profile, which must exist) and ux (run it unconfined, which undoes the point of a profile). Anything the script runs that is not listed here fails with Permission denied.
  • File rules end in letters: r read, w write, a append, m map as executable, k lock. A trailing / means the folder itself, which programs need to list it. @{PROC} and @{pid} come from tunables/global.
  • network inet stream allows IPv4 TCP sockets. With no network rule at all, curl could not open a socket.
  • A deny rule wins over any allow rule and is not logged, which keeps noisy, expected refusals out of the log.
  • include if exists <local/disk-report> reads your site additions from /etc/apparmor.d/local/disk-report if that file is there.

Now ask the parser to compile the profile without loading it into the kernel. -Q skips the kernel load and -K skips the profile cache:

Bash
sudo apparmor_parser -Q -K /etc/apparmor.d/usr.local.bin.disk-report && echo "profile parses"
apparmor_parser -N /etc/apparmor.d/usr.local.bin.disk-report

-N prints the profile name, disk-report. To see how much the abstractions pull in, print the profile with every include expanded and count the lines:

Bash
sudo apparmor_parser -p /etc/apparmor.d/usr.local.bin.disk-report | wc -l

That is several hundred lines from a profile you wrote in about thirty. A mistake gives a line number. For example, a rule with a missing comma produces something like:

Text
AppArmor parser error for /etc/apparmor.d/usr.local.bin.disk-report in profile /etc/apparmor.d/usr.local.bin.disk-report at line 26: syntax error, unexpected TOK_NETWORK, expecting TOK_END_OF_RULE

The line number points at the next rule after the missing comma, because that is where the parser noticed.

Check it worked

The parser prints profile parses with no errors, -N prints disk-report, and the expanded profile is several hundred lines long.

Step 5 · Run it in complain mode, then enforce

Load the profile into the kernel. It still says flags=(complain), so nothing is refused yet:

Bash
sudo apparmor_parser -r /etc/apparmor.d/usr.local.bin.disk-report
sudo aa-status | grep -A3 'complain mode'
sudo disk-report

-r replaces the profile if it is already loaded and adds it if not. aa-status lists disk-report under the profiles in complain mode, and the script runs as before. Now look for anything the profile did not cover:

Bash
sudo journalctl -k --since '-5 min' -g 'profile="disk-report"' --no-pager

In complain mode, missing rules show up as apparmor="ALLOWED" lines. If the command prints nothing, the profile covers everything that run did. If it prints lines, add the paths they name (the next step shows how to read them) and parse and reload again.

When a run logs nothing, switch to enforce mode. aa-enforce removes the complain flag from the file and reloads it:

Bash
sudo aa-enforce /etc/apparmor.d/usr.local.bin.disk-report
sudo aa-status | grep -A3 'enforce mode' | head -n 5
sudo disk-report

The file now has the flags=(complain) part removed. Check the enforced profile still parses the same way:

Bash
sudo apparmor_parser -Q -K /etc/apparmor.d/usr.local.bin.disk-report && echo "profile parses"
grep '^profile' /etc/apparmor.d/usr.local.bin.disk-report

Check it worked

grep prints profile disk-report /usr/local/bin/disk-report { with no flags, aa-status lists disk-report under enforce mode, and sudo disk-report still prints report written to ... while the second terminal logs another 200.

Step 6 · Read a denial and fix it

A profile in enforce mode refuses anything new the program tries. Add a line to the script that prints the server's uptime into the report, which the profile knows nothing about:

Bash
sudo tee /usr/local/bin/disk-report > /dev/null <<'SCRIPT'
#!/usr/bin/bash
# Write a small disk usage report, then ping a monitor URL if one is set.
set -euo pipefail
out=/var/lib/disk-report/report.txt
{
  date -u +%Y-%m-%dT%H:%M:%SZ
  df -h --output=target,pcent /
  uptime
} > "$out"
if [[ -r /etc/disk-report/ping-url ]]; then
  curl -q -fsS --max-time 5 -o /dev/null "$(cat /etc/disk-report/ping-url)"
fi
echo "report written to $out"
SCRIPT

Run it and read the kernel log:

Bash
sudo disk-report
sudo journalctl -k --since '-5 min' -g 'apparmor="DENIED"' --no-pager

The script stops with /usr/bin/uptime: Permission denied, and the log has a line something like this:

Text
audit: type=1400 audit(1764158400.512:318): apparmor="DENIED" operation="exec" class="file" profile="disk-report" name="/usr/bin/uptime" pid=4127 comm="disk-report" requested_mask="x" denied_mask="x" fsuid=0 ouid=0

Read it left to right:

  • apparmor="DENIED": the kernel refused it. In complain mode this says ALLOWED.
  • operation="exec" and class="file": the program tried to start a file. Other values you will meet are open, mknod (create a file), connect and create for sockets.
  • profile="disk-report": which profile refused it.
  • name="/usr/bin/uptime": the path it wanted.
  • comm="disk-report": the process that asked.
  • requested_mask="x": the permission it needed, using the same letters as profile rules. c means create, w write, r read.

Fix it in the local override file, so the main profile stays as you reviewed it:

Bash
sudo tee /etc/apparmor.d/local/disk-report > /dev/null <<'LOCAL'
/usr/bin/uptime ix,
@{PROC}/uptime r,
@{PROC}/loadavg r,
LOCAL
sudo apparmor_parser -Q -K /etc/apparmor.d/usr.local.bin.disk-report && echo "profile parses"

uptime reads /proc/uptime and /proc/loadavg, so those go in too. Load the change and run again:

Bash
sudo apparmor_parser -r /etc/apparmor.d/usr.local.bin.disk-report
sudo disk-report
sudo journalctl -k --since '-2 min' -g 'apparmor="DENIED"' --no-pager

If new DENIED lines appear (for example, uptime counting logged-in users), add one rule per line and repeat. Fix one denial at a time: each fix can let the program get further and reveal the next one. sudo aa-logprof does the same loop interactively, offering a rule for each new log line, and writes the answers into the profile.

Check it worked

sudo disk-report prints report written to ..., the report ends with an up ... line, and the last journalctl command prints no DENIED lines for disk-report.

Step 7 · See why Ubuntu ships profiles that allow everything

Since Ubuntu 24.04, an unprivileged program cannot use the full powers of a user namespace unless an AppArmor profile allows it. User namespaces let a normal user act as root inside a sandbox, and they have been the entry point for many kernel privilege-escalation bugs. Check the setting:

Bash
sysctl kernel.apparmor_restrict_unprivileged_userns
unshare --user --map-root-user id

The sysctl prints = 1. Creating the namespace still works, because the kernel moves it into a special profile instead of refusing it. But that profile takes away every capability, so unshare cannot even set up the root mapping and stops with something like unshare: write failed /proc/self/uid_map: Operation not permitted, and id never runs. Look at that special profile:

Bash
sed -n '1,25p' /etc/apparmor.d/unprivileged_userns

It denies every capability with audit deny capability, and forces child programs to stay under it.

Browsers, Electron apps and container tools need user namespaces for their own sandboxes. So the apparmor package ships a short profile for each of them that leaves the app unconfined but adds the userns rule. Count them and look at one:

Bash
grep -l -d skip '^\s*userns,' /etc/apparmor.d/* | wc -l
cat /etc/apparmor.d/chrome

On 26.04 about 90 profiles carry the rule. The chrome one is a few lines: a profile line with flags=(unconfined), the userns, rule and an include for local additions. If an app you install by hand fails with a sandbox or namespace error, the fix is a profile like this for its path, not turning the sysctl off for the whole server.

Check it worked

The count is a number in the tens, and /etc/apparmor.d/chrome shows flags=(unconfined) and userns, inside one profile block.

Step 8 · Map what you did to SELinux

Red Hat Enterprise Linux, Rocky Linux, AlmaLinux and Fedora use SELinux instead of AppArmor. Both are mandatory access control built on the same kernel security hooks, so the job is the same. The model is different:

  • Paths compared with labels. AppArmor rules name paths. SELinux gives every file, process and port a label (a context such as system_u:object_r:httpd_sys_content_t:s0), stored in the file's extended attributes, and rules say which process types may touch which file types. Move a file in SELinux and it keeps its old label; in AppArmor it gets whatever rules match its new path. ls -Z and ps -Z show labels.
  • Opt-in compared with everywhere. AppArmor only confines programs that have a profile. SELinux's default targeted policy confines most system services out of the box, and unlisted programs run as unconfined_t.
  • Modes. AppArmor's complain and enforce are per profile. SELinux has a system-wide mode, enforcing or permissive (getenforce, setenforce, and SELINUX= in /etc/selinux/config), and semanage permissive -a <type> makes one domain permissive, which is closest to complain mode.
  • Reading denials. AppArmor writes apparmor="DENIED" lines; SELinux writes type=AVC lines with denied { read }, read with ausearch -m AVC.
  • Fixing denials. Where you added a path rule, the SELinux answer is usually one of three: restorecon to put back the expected label on a file, setsebool -P to turn on a switch the policy already has (such as letting a web server make network connections), or semanage fcontext to label a new folder. audit2allow turns AVC lines into a policy module, the SELinux cousin of aa-logprof. Read what it generates before you install it, because it allows exactly what failed, including things that should fail.

The habit that carries over: never turn the system off to make an app work. Find the denial, understand it, and allow the one thing that is needed.

Check it worked

For each command you ran in this tutorial, you can name the SELinux counterpart: aa-status and getenforce, complain mode and permissive mode, the DENIED line and the AVC line, a profile rule and a label or boolean, aa-logprof and audit2allow.

Step 9 · Remove the demo

Unload the profile from the kernel first, while its file still exists:

Bash
sudo apparmor_parser -R /etc/apparmor.d/usr.local.bin.disk-report

Then delete the profile, the override, the script and its folders. Stop the stand-in monitor in the second terminal with Ctrl+C:

Bash
sudo rm /etc/apparmor.d/usr.local.bin.disk-report /etc/apparmor.d/local/disk-report
sudo rm /usr/local/bin/disk-report
sudo rm -r /etc/disk-report /var/lib/disk-report
rm -r ~/apparmor-demo

Check it worked

sudo aa-status | grep disk-report prints nothing, and ls /etc/apparmor.d | grep disk prints nothing.

Next steps

  • Add systemd's own sandbox options to a service, which work alongside an AppArmor profile: /tutorials/sandbox-a-systemd-service-with-security-options
  • Find other gaps on the server, including which services run without a profile: /tutorials/audit-a-server-with-lynis
  • Search the kernel log and journal faster when you chase denials: /tutorials/search-the-systemd-journal-with-journalctl
  • Lock down the way in before you lock down programs: /tutorials/harden-ssh-on-ubuntu-26-04

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.