Use it when relabelling after moving data, adding custom directories or ports.
Labels
SELinux labels are stored in each file's extended attributes and assigned by policy rules (file contexts), at creation or by restorecon. Ports and other objects have labels too.
1. matchpathcon <path> vs ls -Z <path> — differ?
2. restorecon -Rv <path>.
3. Ports: semanage port -l | grep <type>.
4. Add port: semanage port -a -t http_port_t -p tcp 8081.
5. Retest.
policy file-context rules → labels assigned → domains access by type
For SELinux → Labels, continue with the first unproven Topic check: Record exact host, distribution/kernel, boot ID, time window, workload, identity and the authoritative configuration/policy source for Labels. If that boundary is healthy, advance to the adjacent check: Read back the effective runtime state and precedence rather than assuming an edited file, package state or service status is active. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Operational evidence path Verify boundary
policy file-context mapping → filesystem label → process/object access → restore/relabel behavior → AVC/workload
Healthy evidence: persistent mappings produce the expected labels, restorecon is idempotent and the workload succeeds in enforcing mode
Step 2 Verification and output interpretation Verify boundary
Verification chain. policy file-context mapping → filesystem label → process/object access → restore/relabel behavior → AVC/workload
Step 3 Commands Verify boundary
semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?'
restorecon -Rv /srv/www
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: temporary chcon hides a wrong mapping, path regex is too broad/narrow, relabel changes state unexpectedly, filesystem lacks labels or workload still fails after correct labeling
Step 5 Common mistakes Narrow the fault
- Using
chcon(not persistent across relabel).
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
Default labels come from semanage fcontext rules; restorecon applies them. Ports: semanage port -l. A full relabel (/.autorelabel) fixes widespread label problems.
matchpathcon <path>— default context for a pathgetenforce / sestatus— mode and policyls -Z <path>— file contextps -eZ | grep <proc>— process contextausearch -m AVC -ts recent— recent denialssemanage fcontext -l | grep <path>— context rules for a path
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
Label remediation is complete only when the persistent mapping is correct and a restore/relabel reproduces the intended context before the original workload is retested.
In depth
SELinux file labels: rules, relabelling and persistence
File labels come from file context rules in the policy, which map path patterns to types. The semanage-fcontext(8) manual page describes local rules:
semanage fcontext -a -t <type> "<regex>"adds one. The pattern is a full path or a PCRE regular expression.-llists rules, and-Clists only local customisations.-ddeletes and-mmodifies.-emakes one tree use the same labels as another, for example/home1labelled like/home.- After adding a rule, run
restoreconto apply it to existing files.
The restorecon(8) manual page adds:
- Normally only the type is changed.
-Fresets the whole context. -ntogether with-vshows what would change, without changing anything.- Files with a "customizable" type are only reset with
-F.
Labelling tools and their effect
| Tool | Effect | Survives a relabel? |
|---|---|---|
semanage fcontext + restorecon | Rule in the policy, then applied | Yes |
chcon -t type file | Changes the label directly | No: restorecon or a relabel reverts it |
restorecon -Rv path | Apply the rules to existing files | — |
touch /.autorelabel + reboot | Relabel the whole file system | — |
fixfiles | Relabel tools, wrapping restorecon | — |
Common patterns
| Need | Rule |
|---|---|
| Web content in /srv/www | semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?" |
| A writable upload directory for the web app | semanage fcontext -a -t httpd_sys_rw_content_t "/srv/www/uploads(/.*)?" |
| Database data on another volume | semanage fcontext -a -e /var/lib/pgsql /data/pgsql |
| Home directories in /export/home | semanage fcontext -a -e /home /export/home |
Rule precedence
Local rules are checked from newest to oldest, and the first match wins. Keep the regular expressions specific, so a broad rule doesn't override a narrower one.
host$ sudo semanage fcontext -a -t httpd_sys_rw_content_t "/srv/www/uploads(/.*)?"
host$ sudo semanage fcontext -l -C
SELinux fcontext type Context
/srv/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/www/uploads(/.*)? all files system_u:object_r:httpd_sys_rw_content_t:s0
host$ sudo restorecon -Rvn /srv/www | head -3
Would relabel /srv/www/uploads from system_u:object_r:httpd_sys_content_t:s0 to system_u:object_r:httpd_sys_rw_content_t:s0
host$ sudo restorecon -Rv /srv/www
host$ matchpathcon /srv/www/uploads/avatar.png
/srv/www/uploads/avatar.png system_u:object_r:httpd_sys_rw_content_t:s0
Check before you relabel
restorecon -Rvn shows what would change, which is valuable on large trees. matchpathcon shows the label a path should get under the current rules. That's useful for checking a rule before any files exist.
Documented example, not captured output.
How it works at runtime
Labels is part of SELinux → Labels. SELinux Labels covers how filesystem/object labels are assigned, restored and persisted through policy file-context mappings, relabel operations and package/service expectations.
Topic-specific state model. SELinux Labels covers how filesystem/object labels are assigned, restored and persisted through policy file-context mappings, relabel operations and package/service expectations.
Interpretation rule. - chcon success ≠ persistent label assignment across relabel or restorecon.
- Correct label on one file ≠ parent tree, alternate paths or newly created files inherit correctly.
Quick reference
File labels come from policy rules matching paths (semanage fcontext -l); restorecon applies them. Moving files keeps their old label, copying assigns the destination's — a classic cause of denials after mv.
matchpathcon /srv/www/index.html # what the label should be
ls -Z /srv/www/index.html # what it is
semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?'
restorecon -Rv /srv/www
chcon -t httpd_sys_content_t /srv/www/test.html # temporary, lost on relabel
Use semanage fcontext + restorecon for permanent changes; chcon only for quick tests.
Limits and how it fails
Primary failure point. Label remediation is complete only when the persistent mapping is correct and a restore/relabel reproduces the intended context before the original workload is retested.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Distribution/platform context. Labels owns persistent SELinux object labeling; Contexts owns interpretation of the resulting security context.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in SELinux → Labels. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://selinuxproject.org/page/Main_Page · confidence 94%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
Labels operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for Labels.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | SELinux Labels covers how filesystem/object labels are assigned, restored and persisted through policy file-context mappings, relabel operations and package/service expectations. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin host + boot/time + real identity/workload → identify authoritative policy/key/trust source → read back loaded/effective runtime state → reproduce one safe allow/deny/handshake test → correlate audit/service/kernel evidence → locate first failed boundary → make the smallest persistent change → repeat original workload + regression/recovery check. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | policy file-context mapping → filesystem label → process/object access → restore/relabel behavior → AVC/workload | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | Label remediation is complete only when the persistent mapping is correct and a restore/relabel reproduces the intended context before the original workload is retested. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux security/trust evidence can establish this proof path: policy file-context mapping → filesystem label → process/object access → restore/relabel behavior → AVC/workload | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove unobserved identities, objects, policies, namespaces, endpoints or time periods. Label remediation is complete only when the persistent mapping is correct and a restore/relabel reproduces the intended context before the original workload is retested. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven security/trust boundary, capture the adjacent effective state and audit/handshake evidence, make only the demonstrated persistent change, then repeat the original workload plus recovery/regression checks. | Continue to the next unresolved boundary while preserving the original test condition. |
Labels · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | Labels |
|---|---|
| Topic path | SELinux → Labels |
| Runtime state | SELinux Labels covers how filesystem/object labels are assigned, restored and persisted through policy file-context mappings, relabel operations and package/service expectations. |
| Fast checks | - Record exact host, distribution/kernel, boot ID, time window, workload, identity and the authoritative configuration/policy source for Labels. - Read back the effective runtime state and precedence rather than assuming an edited file, package state or service status is active. - Reproduce one safe allowed/denied or handshake/access test and correlate kernel/service/audit evidence at the first relevant boundary. - Apply the smallest persistent cause-specific change, then repeat the original test plus an adjacent regression/recovery check. |
| Verification chain | policy file-context mapping → filesystem label → process/object access → restore/relabel behavior → AVC/workload Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. |
| Failure boundary | Label remediation is complete only when the persistent mapping is correct and a restore/relabel reproduces the intended context before the original workload is retested. Boundary chain: process credentials/namespace → path/mount lookup → DAC/ACL/LSM → filesystem metadata → block/device/backend → completion/read-back. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |