AppArmor basics on Ubuntu
Check AppArmor profile status and switch a profile between modes.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 9 minutes to read.
In short
AppArmor is the mandatory access control system on Ubuntu and SUSE. Instead of labels, it confines programs with path-based profiles that list which files and capabilities each may use. Profiles run in enforce mode, which blocks violations, or complain mode, which only logs them.
Explained simply
AppArmor is Ubuntu's way to keep programs inside a fence. Each protected program gets a profile, a text file that lists exactly which files it may read or write and what else it may do. Anything not on the list is blocked.
Where SELinux puts labels on every file, AppArmor works with file paths. A rule like /var/log/nginx/*.log w means the program may write the log files in that folder. This makes profiles easy to read, even for beginners.
A profile is in one of two modes. Enforce mode blocks and logs anything outside the rules. Complain mode allows everything but logs what would have been blocked, so you can test a profile without breaking the program.
Programs without a profile run unconfined, which means AppArmor does nothing for them. Ubuntu ships profiles for common services, and snap packages are confined by AppArmor automatically.
Press Play on a diagram to walk through it one step at a time.
- Default on RHEL, Fedora
- Label based: contexts on every object
- Whole-system targeted policy
- Tools: semanage, restorecon, setsebool
- Default on Ubuntu, SUSE
- Path based profiles per program
- Unprofiled programs run unconfined
- Tools: aa-status, aa-enforce, aa-complain
- 1Complain mode
aa-complain: allow but log
- 2Use the program
Exercise every normal feature
- 3aa-logprof
Turn logged events into rules
- 4Enforce mode
aa-enforce and watch for DENIED
- Back to step 1
In depth
Reading aa-status
sudo aa-status (also called apparmor_status) is the first command to run. It prints how many profiles are loaded, which are in enforce mode and which in complain mode, and which running processes are confined by which profile.
One line to watch: processes that are unconfined but have a profile defined. That means the profile was loaded after the program started. A profile only applies when the program is executed, so restart the service to bring it under the profile. The command aa-enabled simply answers Yes if AppArmor is active, and the kernel parameter apparmor=1 is the default on Ubuntu.
- sudo aa-status --enforced counts enforce-mode profiles
- ps auxZ shows the profile each process runs under
- systemctl status apparmor checks that profiles load at boot
What a profile looks like
Profiles live in /etc/apparmor.d. The file name is the program path with slashes turned into dots, so /usr/sbin/tcpdump has the profile usr.sbin.tcpdump. Inside, each rule is a path followed by permissions: r read, w write, m memory map as executable, ix run a program inside the same profile, and k lock. One star matches within a folder, two stars match any depth.
Profiles also control capabilities (capability net_raw,) and networking (network inet tcp,). Shared rules come from include lines that pull in files such as abstractions/base. To add your own rules without editing the packaged file, use the local override file, for example /etc/apparmor.d/local/usr.sbin.tcpdump, which package updates leave alone.
- Includes
- abstractions/base
- local/usr.sbin.myapp
- File rules
- /etc/myapp/* r,
- /var/log/myapp/*.log w,
- /usr/sbin/myapp mr,
- Capabilities
- capability net_bind_service,
- Network
- network inet tcp,
Switching modes and reloading
The apparmor-utils package gives you the mode tools. aa-complain PROFILE switches one profile to complain mode, aa-enforce PROFILE switches it back, and aa-disable PROFILE unloads it and adds a symlink in /etc/apparmor.d/disable/ so it stays off after reboot. You can pass the profile file or the program path.
After editing a profile by hand, reload it with sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.myapp. If the syntax is wrong the parser prints the line number and keeps the old version loaded. To reload everything, run sudo systemctl reload apparmor.
- sudo aa-complain /usr/sbin/tcpdump
- sudo aa-enforce /etc/apparmor.d/usr.sbin.tcpdump
- sudo apparmor_parser -r FILE reloads one profile
Finding and acting on denials
Denials are kernel audit messages. If auditd is installed they go to /var/log/audit/audit.log; otherwise they land in the kernel log, which journalctl -k shows. Search for apparmor="DENIED". A message names the operation (open, exec, mknod), the profile, the path in name=, and the requested_mask, such as r or w.
In complain mode the same messages say apparmor="ALLOWED". The tool aa-logprof reads these log entries and asks, rule by rule, whether to allow each one, then writes the profile for you. aa-genprof PROGRAM builds a new profile from scratch the same way: start it, use the program normally, then let it scan the log.
Edge cases worth knowing
Because rules are path based, a hard link or a copy of a binary at a different path is not covered by the original profile. Bind mounts and symlinks are resolved to the real path before checking, so rules must name that real path.
On Ubuntu 23.10 and later, AppArmor also restricts unprivileged user namespaces, which some sandboxed apps and rootless container tools need; such programs get a profile that grants the userns permission. Docker and Podman apply a default AppArmor profile to containers on Ubuntu as well, which explains some surprising permission errors inside containers.
Learn it in 3 short steps
About 30m each. Track them with a timer in your plan.
- 1List profiles with aa-status~30m
- 2Compare enforce and complain modes~30m
- 3Find denials in the journal~30m
Key points
- aa-status lists loaded profiles and their modes.
- Profiles live in /etc/apparmor.d.
- aa-complain is the AppArmor equivalent of permissive for one program.
- Denials appear in the journal with apparmor="DENIED".
Key terms
Tap a card to flip it. 0/4 known
Examples
sudo aa-status
ls /etc/apparmor.d/
sudo apt install -y apparmor-utils
sudo aa-complain /etc/apparmor.d/usr.sbin.tcpdump
sudo journalctl -k | grep -i apparmor | tail
sudo aa-enforce /etc/apparmor.d/usr.sbin.tcpdumpInspect profiles, switch one to complain mode and back.
Let a confined service read a new folder
You moved the config for myapp to /srv/conf. It is in enforce mode and now fails with Permission denied, even as root. Step 1: find the denial in the kernel log; it shows name="/srv/conf/app.ini" and requested_mask="r". Step 2: add a read rule to the local override file. Step 3: reload the main profile, which includes the local file. Step 4: restart the service and check the log is quiet.
sudo journalctl -k --since '10 min ago' | grep 'apparmor="DENIED"'
echo '/srv/conf/** r,' | sudo tee -a /etc/apparmor.d/local/usr.sbin.myapp
sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.myapp
sudo systemctl restart myapp
sudo journalctl -k --since '1 min ago' | grep -c 'DENIED' # 0Is AppArmor the reason this fails?
A packaged service such as cups-browsed fails after a change and you suspect AppArmor. Step 1: check it has a profile in enforce mode. Step 2: switch only that profile to complain mode and retry. Step 3: if it now works, AppArmor was the cause; read the ALLOWED lines to see which rules are missing. Step 4: add those rules and put the profile back in enforce mode. Never leave it in complain mode as the fix.
sudo aa-status | grep -A3 'enforce mode' | head
sudo aa-complain /usr/sbin/cups-browsed
sudo systemctl restart cups-browsed
sudo journalctl -k | grep 'apparmor="ALLOWED"' | tail -5
# add the rules to /etc/apparmor.d/local/usr.sbin.cups-browsed
sudo aa-enforce /usr/sbin/cups-browsedQuick reference
- sudo aa-status: loaded profiles, modes and confined processes
- Profiles: /etc/apparmor.d/usr.sbin.name; your rules: /etc/apparmor.d/local/usr.sbin.name
- aa-complain logs only; aa-enforce blocks; aa-disable unloads and keeps it off
- sudo apparmor_parser -r FILE reloads one edited profile
- journalctl -k | grep 'apparmor="DENIED"' finds blocked actions
- Restart a service after loading its profile; profiles apply at exec time
- aa-logprof turns logged events into profile rules interactively
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Count how many AppArmor profiles are in enforce mode and how many in complain mode on an Ubuntu machine.
- Exercise 2medium
Put the tcpdump profile in complain mode, capture a few packets to a file in /tmp, look at the log, then return it to enforce mode.
- Exercise 3hard
Write a minimal profile for a script /usr/local/bin/report.sh that may read /etc/hostname and write only to /var/tmp/report.txt, then prove a write elsewhere is denied.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can I run SELinux and AppArmor at the same time?
- Not as the main security module on normal distributions; each system enables one. Ubuntu and SUSE use AppArmor, while RHEL and Fedora use SELinux.
- How do I turn AppArmor off for one program?
- Run aa-disable with its profile. It unloads the profile and adds a link in /etc/apparmor.d/disable so it stays off after reboot. Fixing the profile is usually better.
- Why does root get Permission denied?
- AppArmor rules apply to confined programs no matter which user runs them, including root. Check the kernel log for apparmor="DENIED".
- Does AppArmor protect programs with no profile?
- No. Programs without a profile run unconfined, so only normal Linux permissions apply to them.