Course contentsShow
← Security
Lesson 1455 of 3,05229. Operating System Security - HardeningFree lesson

SELinux Contexts and Labels

Master reading and modifying security contexts using ls -Z, chcon, semanage, and restorecon commands.

SELinux Contexts and Labels

What you'll learn: How to read, interpret, and modify SELinux security contexts that control access decisions.

Understanding Security Contexts

Every file, process, port, and user in SELinux has a security context (or label) that determines what can interact with it. Think of these labels as color-coded badges in a secure facility—only processes with the right "badge color" can access resources with matching colors.

A security context looks like this:

user:role:type:level

For example: system_u:object_r:httpd_sys_content_t:s0

  • user: SELinux user (not the same as Linux user)
  • role: organizational category
  • type: the most important part for Type Enforcement policy
  • level: used for Multi-Level Security (optional)

Reading Security Contexts

Use ls -Z to view file contexts:

ls -Z /var/www/html/

Check process contexts with ps -Z:

ps -Z | grep httpd

Modifying Contexts

Temporary changes with chcon:

chcon -t httpd_sys_content_t /var/www/html/index.html

Permanent policy changes with semanage:

semanage fcontext -a -t httpd_sys_content_t "/custom/web(/.*)?"

Restore default contexts with restorecon:

restorecon -Rv /var/www/html/

This is crucial after moving files or when contexts get corrupted—restorecon reapplies the policy-defined defaults.

Best Practices

Always prefer semanage + restorecon over chcon for permanent changes. Use chcon only for troubleshooting. The restorecon command is your safety net when contexts become misconfigured.

Key Takeaway: SELinux contexts are security labels on every system object; use ls -Z to inspect them, semanage for permanent policy changes, and restorecon to restore correct labels from policy defaults.