Sub topic 6
AppArmor and SELinux defaults
This chapter assumes you are comfortable with a Linux shell, with file ownership and permission bits, and with systemd services. You should also know why a server runs a firewall. The previous chapters covered package managers and repositories. This one covers the access-control layer that sits underneath your services, and how its default differs between Debian-family systems and Rocky Linux.
Discretionary and mandatory access control
Standard Linux permissions (owner, group, mode bits) are discretionary access control (DAC). The owner of a file decides who can read it, and any process running as that user inherits the user's rights. If an attacker takes over a web server process running as www-data, they can read everything www-data can read.
Mandatory access control (MAC) adds a second check that the kernel enforces by policy. The file owner cannot change that policy, and neither can the process. The kernel runs the MAC check after the normal permission check, so MAC can only deny access that DAC would have allowed. It never grants more.
Two MAC systems matter here:
- AppArmor is the default on Ubuntu Server and Debian.
- SELinux is the default on Rocky Linux, where it is enforcing out of the box.
Both are Linux Security Modules (LSMs). A kernel normally runs one major LSM at a time, so a host runs one or the other.
How the two models differ
The core difference is what the policy attaches to.
- AppArmor is path-based. A profile says "the program at
/usr/sbin/nginxmay read/var/www/** r". The policy refers to file paths. - SELinux is label-based. Every process and every file carries a security label stored in the filesystem's extended attributes. The policy says "processes labeled
httpd_tmay read files labeledhttpd_sys_content_t". The path does not matter. The label does.
| Aspect | AppArmor | SELinux |
|---|---|---|
| Default on | Ubuntu Server, Debian | Rocky Linux |
| Policy refers to | File paths | Labels on files, processes, ports |
| Policy location | `/etc/apparmor.d/` | Compiled policy modules plus `/etc/selinux/` |
| Modes | enforce, complain | enforcing, permissive, disabled |
| Default coverage | Selected programs have profiles; others run unconfined | Targeted policy confines most network-facing daemons |
| Learning curve | Lower | Higher |
| Granularity | Files, capabilities, some network rules | Files, ports, users, booleans, fine-grained classes |
The path-based model is easier to read and write. It also has a known weakness: a hard link or a bind mount can present the same data under a different path. Labels follow the data itself, which is why SELinux can enforce stricter isolation. The price is that labels must be right, and wrong labels are the most common source of SELinux problems.
Checking what is active
The first command to run on an unfamiliar host tells you which system you are dealing with.
# Rocky Linux: SELinux status
getenforce
# Ubuntu / Debian: AppArmor status
sudo aa-statusgetenforce prints Enforcing, Permissive, or Disabled. aa-status lists loaded profiles and splits them into those in enforce mode, those in complain mode, and processes that have a profile but are not confined. On a minimal Ubuntu install you see a short list of profiles. A fuller report comes from sestatus on Rocky, which also shows the loaded policy name (targeted) and the mode set in the config file.
You can also read the active LSM list from the kernel:
cat /sys/kernel/security/lsmThe output is a comma-separated list such as lockdown,capability,yama,apparmor on Ubuntu or lockdown,capability,yama,selinux,bpf on Rocky. Seeing apparmor or selinux in that list tells you which system the kernel has loaded.
SELinux on Rocky Linux
Rocky ships SELinux in enforcing mode with the targeted policy. Under this policy, most network-facing services run in a confined domain such as httpd_t or sshd_t. Everything else runs in the unconfined_t domain and is barely restricted.
You can see labels with the -Z option:
ls -Z /var/www/html
ps -eZ | grep nginxThe three modes
- Enforcing blocks and logs violations.
- Permissive logs violations but allows them. Use it to see what a policy would block.
- Disabled loads no policy.
sudo setenforce 0 switches to permissive until reboot. sudo setenforce 1 switches back. The persistent setting lives in /etc/selinux/config as SELINUX=enforcing.
Fixing the common denials
Most SELinux problems on a new server fall into three groups: wrong file labels, a blocked port, or a boolean that is off. Each has a standard fix.
Wrong file label. Suppose nginx serves content from /srv/web, and you get 403 Forbidden even though the Unix permissions are fine. The directory has the generic label var_t, not a web content label. Define the rule and apply it:
sudo dnf install policycoreutils-python-utils
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/websemanage fcontext records the rule in the policy store so that it survives a relabel. restorecon applies the stored rule to existing files. Using chcon alone changes the label, but the change is lost at the next relabel.
A related gotcha: mv keeps the label the file had at its old location, while cp creates a new file that inherits the label of the destination directory. Files moved into a web root can therefore keep a label that httpd is not allowed to read. Run restorecon -Rv on the destination after moving.
Blocked port. A confined service may bind only to ports whose type the policy allows. To run nginx on 8081, add the port to the HTTP port type:
sudo semanage port -a -t http_port_t -p tcp 8081Use semanage port -l | grep http_port_t to list the current allowed ports first. If the port is already defined under another type, use -m instead of -a.
Boolean. Booleans are on/off switches for optional policy behavior. For example, a reverse proxy that connects to a backend application needs one:
sudo setsebool -P httpd_can_network_connect on
getsebool -a | grep httpdThe -P flag makes the setting persistent across reboots.
Reading denials
SELinux denials are called AVC (access vector cache) denials and go to the audit log. Search for recent ones:
sudo ausearch -m AVC -ts recentThe setroubleshoot-server package adds sealert, which translates a denial into plain language and a suggested command. audit2why explains why a given denial occurred. Treat suggestions from audit2allow with care: generating a custom module from every denial teaches the policy to allow the very behavior you wanted blocked.
AppArmor on Ubuntu Server and Debian
AppArmor is enabled in the Ubuntu kernel, and the apparmor service loads profiles at boot. Debian has enabled it by default since Debian 10. The key difference from Rocky is coverage. Profiles ship for selected programs. Many common daemons, including nginx from the distribution packages, run unconfined unless you add a profile. You get a smaller default protection surface, but also fewer surprises.
Profiles live in /etc/apparmor.d/. The file name usually mirrors the program path, so /usr/sbin/mysqld is described in usr.sbin.mysqld.
The two modes
- Enforce blocks and logs violations.
- Complain logs violations but allows them. This corresponds to SELinux permissive mode, but it applies per profile and not to the whole system.
The tools come from the apparmor-utils package:
sudo apt install apparmor-utils
sudo aa-complain /etc/apparmor.d/usr.sbin.mysqld
sudo aa-enforce /etc/apparmor.d/usr.sbin.mysqld
sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqldapparmor_parser -r reloads a profile after you edit the file. Without the reload, the kernel keeps the old version.
Reading denials
Denials appear in the kernel log with apparmor="DENIED":
sudo journalctl -k | grep 'apparmor="DENIED"'A typical case is moving a database data directory. MySQL or MariaDB on Ubuntu refuses to start from /data/mysql because the profile allows only /var/lib/mysql/. The fix is to add the new path to a local override file, /etc/apparmor.d/local/usr.sbin.mysqld, using a rule such as /data/mysql/** rwk,, and then reload the profile. Local override files survive package upgrades. Edits to the main profile do not.
For new programs, aa-genprof builds a profile interactively by watching the program run, and aa-logprof updates a profile from logged denials. Review the generated rules before you accept them.
Containers
Both systems also confine containers, with a different default in each family.
- On Ubuntu and Debian, Docker applies the
docker-defaultAppArmor profile to containers. - On Rocky, Podman and Docker confine containers with SELinux types such as
container_t, supplied by thecontainer-selinuxpackage.
The SELinux version has a well-known gotcha. A bind-mounted host directory keeps its host label, so the container process gets "permission denied" even when the Unix permissions are correct. Add :z (shared among containers) or :Z (private to this container) to the volume option so the runtime relabels it:
podman run --rm -v /srv/web:/usr/share/nginx/html:Z docker.io/library/nginx:stableUse :Z carefully on directories that other programs also use, since it changes the label for them as well.
Troubleshooting flow
The same sequence works on either system. Confirm the access control layer is the cause before you change anything.
What this means for choosing a distribution
- Rocky Linux gives you strong default confinement of network daemons, and you pay with an administration cost. Anyone running services on it needs working SELinux skills. Teams that already operate under compliance regimes, such as those that reference STIG or CIS benchmarks, often expect SELinux enforcing.
- Ubuntu Server and Debian give you a lighter default that rarely blocks custom layouts. The trade-off is that many daemons are unprotected until you write or install profiles.
- Switching systems on the same distribution is possible but unsupported by default. SELinux can be installed on Debian, and its policy needs substantial work. AppArmor is not available on Rocky's kernel. Pick the distribution whose default fits your team.
Exercises
Exercise
On a Rocky Linux server, nginx returns 403 for files you copied into a new directory /srv/site. Unix permissions are correct and getenforce prints Enforcing. Give the two commands that fix this persistently.
Show answer
Define the label rule with sudo semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?", then apply it with sudo restorecon -Rv /srv/site. The first command records the rule in the policy so it survives a relabel. The second sets the label on the existing files.
Exercise
MariaDB on Ubuntu will not start after you move its data directory to /data/mysql, and the kernel log contains apparmor="DENIED". Where do you add the fix, and what must you run afterwards?
Show answer
Add a rule such as /data/mysql/** rwk, (plus /data/mysql/ r,) to /etc/apparmor.d/local/usr.sbin.mysqld, which survives package upgrades. Then reload the profile with sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqld and restart the service.
Exercise
A teammate suggests running sudo setenforce 0 to fix a Rocky server problem and leaving it that way. Name two reasons this is a poor fix and one correct use of the command.
Show answer
It removes confinement for every service, and the setting reverts at reboot unless the config file is also changed, so the fix is either incomplete or silently permanent. It also hides the actual denial, which you would leave unresolved. A correct use is as a short test: set permissive mode, check whether the problem disappears (which confirms SELinux is the cause), read the AVC log, fix the label, boolean, or port, and set enforcing again.