Sub topic 6
Labeling Files Persistently with semanage fcontext and restorecon
This chapter assumes you can read a security context (user:role:type:level), know that a confined service such as httpd runs in a domain like httpd_t, and are comfortable with sudo, ls, and systemctl on a RHEL-family system with SELinux in enforcing mode.
Why labels matter for files
SELinux decides whether a process may open a file by comparing the process domain with the file's type. A web server running as httpd_t can read files labeled httpd_sys_content_t. It cannot read files labeled user_home_t or default_t, even if the Unix permissions are 777.
The most common SELinux problem on a server is a file with the wrong label. It usually happens in one of three ways:
- You create a new directory outside the standard paths, such as
/srv/web, and the files inherit a generic label. - You move a file with
mvfrom your home directory into the web root. A move keeps the old label, while a copy creates a new file that takes the label of its destination directory. - You restore a backup or extract an archive that carries labels from another system.
The fix is to tell SELinux what the label should be, then apply it. Two tools do this, and they do different jobs.
| Tool | What it does | Survives a relabel? |
|---|---|---|
| `chcon` | Changes the label on existing files right now | No |
| `semanage fcontext` | Records a rule that maps a path pattern to a type | Yes, but only as a rule |
| `restorecon` | Applies the recorded rules to files on disk | Yes, when a rule exists |
The rule from semanage fcontext is stored in the policy. The label on the file is stored in the file's extended attributes. Adding a rule does not change any file. You must run restorecon afterwards.
Inspecting current labels
Start by looking at what a standard path looks like, and at the process that needs it.
getenforce
sestatus
ls -Zd /var/www/html
ps -eZ | grep httpdOn a default Apache install, /var/www/html shows httpd_sys_content_t and the processes run in httpd_t. Those two types are linked by policy rules that allow reads. Your task for a custom path is to make it look like the standard one.
The persistent workflow
Suppose you host a site from /srv/web instead of /var/www/html. Files created there get the label of /srv, which is var_t, and Apache is denied access.
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
sudo restorecon -Rv /srv/web
ls -Zd /srv/webThe three commands do the following:
semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"adds (-a) a rule that assigns the typehttpd_sys_content_tto the pattern.restorecon -Rv /srv/webwalks the tree recursively (-R) and prints each label change (-v).ls -Zd /srv/webconfirms the result.
The semanage command is in the policycoreutils-python-utils package on RHEL 8 and later. If the command is missing, install that package.
Reading the path pattern
The pattern is a regular expression, not a shell glob. The expression /srv/web(/.*)? matches /srv/web itself, and also anything below it. The part in parentheses is optional, so the directory and all of its contents are covered by one rule.
Common mistakes:
- Writing
/srv/web/*. This is not a valid regular expression for "everything below", and it misses the directory itself. - Forgetting the quotes. The shell may expand the parentheses or the asterisk before
semanagesees them. - Leaving out the leading slash. Patterns must be absolute paths.
Choosing the right type
Pick the type from what the service needs to do with the files. For Apache, the most common choices are these.
| Type | Use it for |
|---|---|
| `httpd_sys_content_t` | Static content that httpd only reads |
| `httpd_sys_rw_content_t` | Content that httpd must also write, such as an upload directory |
| `httpd_sys_script_exec_t` | CGI scripts that httpd may execute |
| `httpd_log_t` | Log files that httpd writes |
| `httpd_config_t` | Configuration files |
Use the least permissive type that works. If only one subdirectory needs writes, give only that subdirectory the read-write type.
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/srv/web/uploads(/.*)?"
sudo restorecon -Rv /srv/web/uploadsWhen two rules match one file, SELinux uses the more specific one. The /srv/web/uploads rule wins over the /srv/web rule for files in that directory.
To list the types available for a service, run man httpd_selinux. If the page is missing, install selinux-policy-doc and run sudo mandb.
The chcon trap
chcon looks easier, and it works at first:
sudo chcon -R -t httpd_sys_content_t /srv/webThe problem is that the change is not in the policy. The next time a full relabel runs, or someone runs restorecon on the tree, the files return to whatever the policy says. A full relabel is triggered by touch /.autorelabel followed by a reboot, and by some upgrades. A site that worked for weeks suddenly returns 403 Forbidden, and nobody remembers the old chcon.
Previewing and verifying
Before you change many files, preview the effect with -n, which makes no changes, together with -v.
sudo restorecon -Rnv /srv/webEach line of output shows a label that would change. No output means all labels already match the policy.
To see which rule applies to a path without touching the file, use matchpathcon.
matchpathcon /srv/web/index.htmlTo list the local rules you have added, and only those, use the -C option.
sudo semanage fcontext -l -CTo see which label a path should have by default, search the full list.
sudo semanage fcontext -l | grep '/var/www'Changing and removing rules
To change the type of an existing rule, use -m (modify). To delete a rule, use -d.
sudo semanage fcontext -m -t httpd_sys_rw_content_t "/srv/web/uploads(/.*)?"
sudo semanage fcontext -d "/srv/web/uploads(/.*)?"
sudo restorecon -Rv /srv/web/uploadsDeleting a rule does not change files already on disk. You must run restorecon afterwards, and the files then take the label from the next matching rule, or from the parent default.
Equivalence rules
If you move a whole tree, you may want it to be labeled exactly like another one. An equivalence rule says that one path should be treated as another for labeling purposes.
sudo semanage fcontext -a -e /var/www /srv/web
sudo restorecon -Rv /srv/webWith this rule, /srv/web/html gets the label that /var/www/html would get, and so on for every subpath. This is a good fit when you relocate a service's entire data tree. It saves you from writing one rule per subdirectory. Equivalence rules appear in semanage fcontext -l -C with the = sign.
How labels are assigned to new files
A new file normally inherits the type of its parent directory. This is why correct labeling of the directory matters so much. If /srv/web is labeled httpd_sys_content_t and you then create /srv/web/new.html as root, the file gets the same type.
Moving and copying behave differently:
| Action | Resulting label |
|---|---|
| `cp file /srv/web/` | Takes the label of the destination directory |
| `mv file /srv/web/` on the same file system | Keeps the old label |
| `cp -a file /srv/web/` | Preserves the source label, including SELinux context |
| `tar` extract | Depends on options; labels are kept only if `--selinux` is used |
After any mv or cp -a into a confined path, run restorecon -Rv on the target. This habit prevents most labeling problems.
Edge cases
- NFS and other network file systems. These use mount-time labeling. A path rule does not apply. Use the mount option
context=or an SELinux boolean such ashttpd_use_nfs. - Mount points. The
restoreconcommand works on the file system it is pointed at. It does not cross into other mounted file systems unless you run it on them. - Home directories. Apache reading
/home/user/public_htmlneeds the booleanhttpd_enable_homedirsas well as the correct type. The label alone is not enough. - Containers and volumes. Bind mounts into containers need their own labels, such as
container_file_t. That topic comes later in the tutorial. - Full relabel time. On large file systems, an
.autorelabelboot takes a long time. Correct rules make it harmless, but expect the delay.
A short debugging pass
If a service gets "Permission denied" and Unix permissions look correct, check labels before anything else.
ls -Zd /srv/web /srv/web/index.html
matchpathcon /srv/web/index.html
sudo restorecon -Rnv /srv/webIf matchpathcon shows the type you want and ls -Z shows something else, run restorecon. If matchpathcon shows the wrong type, you are missing a rule, so add one with semanage fcontext.
Exercise
You created /srv/app/static and ran sudo chcon -R -t httpd_sys_content_t /srv/app/static. The site worked, but after a full relabel it returns 403. What two commands make the fix permanent, and why did the first approach fail?
Show answer
Run sudo semanage fcontext -a -t httpd_sys_content_t "/srv/app/static(/.*)?" and then sudo restorecon -Rv /srv/app/static. The chcon command changed only the labels on the files and wrote no rule to the policy. A full relabel resets files to the label the policy gives their path, and with no rule for /srv/app/static the files returned to a generic type that httpd_t cannot read.
Exercise
A directory /srv/web/uploads must be writable by Apache, while the rest of /srv/web is read-only content. Which type do you give the uploads directory, and which rule wins for a file at /srv/web/uploads/a.png?
Show answer
Give it httpd_sys_rw_content_t with a rule for "/srv/web/uploads(/.*)?". For /srv/web/uploads/a.png the uploads rule wins because it is the more specific match, so the file is labeled httpdsysrwcontentt.