SELinux Essentials: Control Filesystem Access
In this article
- Background: Why SELinux Exists
- How SELinux Works: Labels, Types, and Policies
- Controlling Filesystem Access: Practical Commands
- How to Choose: Enforcing vs Permissive vs Disabled
- Common Problems and How to Fix Them
- Comparisons: SELinux vs AppArmor vs Traditional Permissions
- Safety and Best Practices
- Frequently Asked Questions
SELinux (Security-Enhanced Linux) is a mandatory access control (MAC) system built into the Linux kernel that restricts how processes and users access files, directories, and other resources—even if the traditional Unix permissions (owner/group/other) would allow it. To control filesystem access with SELinux, you assign a security context (label) to every file and process, then define policies that state which contexts may interact. This article explains the core concepts, how to manage labels and booleans, and how to troubleshoot common access denials.
Background: Why SELinux Exists
Traditional Linux uses discretionary access control (DAC): the owner of a file decides who can read, write, or execute it. If a process runs as root, it can access almost anything. That is dangerous—a single compromised daemon (like a web server) can read your SSH keys or overwrite system binaries. SELinux was developed by the NSA and released to the open source community in 2000. It was merged into the Linux kernel in 2003 and became the default enforcement mechanism in Red Hat Enterprise Linux (RHEL), Fedora, CentOS, and their derivatives. Other distributions like Debian and Ubuntu ship with SELinux available but often use AppArmor instead by default.
SELinux enforces a policy that defines every type of process and every type of file. For example, the httpd_t type is assigned to the Apache web server process, and httpd_sys_content_t is assigned to files in /var/www/html. The policy says that httpd_t can read httpd_sys_content_t but cannot read shadow_t (which protects /etc/shadow). Even if Apache runs as root, it cannot access files outside its allowed types.
How SELinux Works: Labels, Types, and Policies
Every file, directory, process, port, and even device has a security context. A typical context looks like this:
system_u:object_r:httpd_sys_content_t:s0
The fields are: user, role, type, and sensitivity (MLS). The type is the most important for filesystem access. SELinux policies are written in a language that allows or denies operations (read, write, execute, etc.) between types. The default policy on most systems is targeted, which confines only specific daemons (like httpd, sshd, named) and leaves everything else unconfined.
To see the context of a file, use:
ls -Z /var/www/html/index.htmlTo see the context of a process:
ps -eZ | grep httpdIf a file has the wrong label, a confined process will be denied access even if the Unix permissions are correct. For example, if you move a file into /var/www/html with mv from your home directory, it may retain the user_home_t type, and Apache will get a “Permission denied” error. The fix is to restore the correct label with restorecon.
Controlling Filesystem Access: Practical Commands
The most common task is setting the correct label on a file or directory. Use chcon to change the context temporarily, and restorecon to reset it to the default policy value.
chcon -t httpd_sys_content_t /path/to/file– sets the type tohttpd_sys_content_t.restorecon -v /path/to/file– restores the context to what the policy expects.semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"– tells SELinux that files in/srv/wwwshould always have that type, sorestoreconwill apply it.semanage fcontext -d /path– removes a custom mapping.
If you are running a web server on a non-standard port, you may also need to allow SELinux to listen on that port:
semanage port -a -t http_port_t -p tcp 8080Booleans are switches that allow you to enable or disable certain policy rules without editing policy files. For example, to allow Apache to make network connections:
setsebool -P httpd_can_network_connect onThe -P flag makes the change persistent across reboots.
How to Choose: Enforcing vs Permissive vs Disabled
SELinux has three modes:
- Enforcing: Denies actions and logs them.
- Permissive: Logs actions but does not deny them. Useful for testing.
- Disabled: SELinux is turned off entirely.
To check the current mode:
getenforceTo switch modes temporarily (until reboot):
setenforce 0 # permissiveTo switch permanently, edit /etc/selinux/config and set SELINUX=permissive or enforcing. Disabling SELinux entirely is generally not recommended because you lose the security benefits. If you are migrating a service that is being denied, start with permissive mode, examine the logs, and fix the labels or booleans before switching back to enforcing.
Common Problems and How to Fix Them
The most frequent SELinux issue is a “Permission denied” error that persists after you check Unix permissions. The first step is to check the audit log:
grep "denied" /var/log/audit/audit.log | tail -20On systems without auditd, look at /var/log/messages or journalctl -t setroubleshoot. The log will show the source type, target type, and the operation that was denied. For example:
type=AVC msg=audit(1234567890.123:456): avc: denied { read } for pid=1234 comm="httpd" name="index.html" dev=sda1 ino=12345 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:user_home_t:s0 tclass=fileThat tells you that httpd_t was denied read access to a file labeled user_home_t. The fix is to change the label to httpd_sys_content_t (or whatever the policy expects).
Another common issue is that a service cannot write to a directory it owns. For example, if you run a custom application in /opt/myapp that needs to write to /var/log/myapp, you may need to set the correct type on the log directory. Use semanage fcontext to define the mapping, then restorecon to apply it.
If you are unsure what type a file should have, compare it with a similar file that works. For example, if your website works in /var/www/html but not in /srv/web, check the context of both directories. You can also use matchpathcon to see what context the policy expects for a path.
Comparisons: SELinux vs AppArmor vs Traditional Permissions
SELinux is not the only MAC system. AppArmor is used by default on Ubuntu and Debian. The key differences are:
- Labeling: SELinux labels every file and process; AppArmor uses path-based profiles (e.g., “
/etc/passwdis readable”). - Granularity: SELinux can control operations like “append” or “ioctl” on a file; AppArmor typically limits to read/write/execute.
- Configuration: SELinux policies are complex and can be difficult to write from scratch; AppArmor profiles are simpler and often easier to troubleshoot.
- Default usage: RHEL/Fedora/CentOS use SELinux; Ubuntu/Debian use AppArmor. Both provide strong isolation, but SELinux is considered more fine-grained.
Traditional Unix permissions (chmod, chown) are still the first line of defense. SELinux sits on top and can only restrict further—it never grants more access than the DAC allows. That is why you still need to set proper file ownership and permissions.
Safety and Best Practices
When working with SELinux, always make changes in permissive mode first if you are unsure. This lets you see what would be denied without breaking services. Always use restorecon instead of chcon when you want to return to the policy default. If you manually set a label with chcon, the label will revert after a filesystem relabel (e.g., after a reboot with autorelabel). Use semanage fcontext to make permanent rules.
Never disable SELinux just to make an application work. Instead, find the correct label or boolean. The setroubleshoot package provides a daemon that analyzes denials and suggests fixes. For example, it might suggest: “setsebool -P httpd_can_network_connect 1”. These suggestions are usually accurate, but verify them against your security requirements.
If you are running a database or a custom daemon, check the SELinux man pages for that service. Most distributions ship with policy modules for common services like MariaDB, PostgreSQL, and Nginx. If you need a custom policy, you can use audit2allow to generate a module from the denial logs:
grep "denied" /var/log/audit/audit.log | audit2allow -M mypolThis creates a policy module that allows the exact denials you saw. Use it sparingly—it can grant more than necessary if you are not careful.
Frequently Asked Questions
How do I check if SELinux is enabled?
Run getenforce. It prints Enforcing, Permissive, or Disabled. You can also run sestatus for more details, including the policy version and the loaded policy name.
Why does my web server get “Permission denied” even though the file permissions are correct?
Most likely the file has the wrong SELinux label. Check with ls -Z /path/to/file. If the type is not httpd_sys_content_t (or similar), run restorecon -v /path/to/file. If the file is in a non-standard directory, you may need to add a custom fcontext mapping with semanage fcontext.
What is the difference between chcon and restorecon?
chcon sets the context immediately, but the change is not permanent—a filesystem relabel will revert it. restorecon sets the context to the default value defined in the policy (or by semanage fcontext rules). Use chcon for temporary testing, and restorecon for permanent fixes.
How can I see what SELinux is blocking?
Look at the audit log: grep "denied" /var/log/audit/audit.log. If auditd is not running, check /var/log/messages or use journalctl -t setroubleshoot. The ausearch -m avc -ts recent command is another way to see recent denials.
Can I turn SELinux off permanently?
Yes, by editing /etc/selinux/config and setting SELINUX=disabled, then rebooting. However, this removes a valuable security layer. Instead, try permissive mode to see what would be denied, then fix the labels or booleans. If you must disable it, consider using AppArmor or another MAC system.
What does setsebool -P do?
It changes a boolean value (a policy switch) and makes the change persistent across reboots. Without -P, the change is temporary and reverts on reboot. For example, setsebool -P httpd_can_network_connect on allows Apache to make outbound network connections permanently.