Trigger an SELinux Denial by Moving a File with an Incorrect Context
In this step, you will see what happens when a file with an incorrect SELinux context is moved into the Apache web directory. This is a common scenario that can be confusing if you are not aware of how SELinux contexts work with file operations like mv. Unlike creating a file directly in a directory (which causes it to inherit the parent's default context), moving a file preserves its original context.
First, let's create a new web page, page2.html, in your current working directory, ~/project.
echo "This is Page 2" > page2.html
Now, check the SELinux context of this new file. Since it was created in your home project directory, it will receive a default context assigned to user files.
ls -Z page2.html
The output will show a context type of user_home_t or something similar, which is the default for files in a user's home directory.
system_u:object_r:user_home_t:s0 page2.html
Notice the type is user_home_t. This is different from the httpd_sys_content_t that Apache is allowed to access.
Next, move this file to the Apache web root using the mv command. You'll need sudo because the destination directory /var/www/html is owned by root.
sudo mv page2.html /var/www/html/
The mv command preserves the source file's SELinux context. Let's verify this by checking the context of the file in its new location.
ls -Z /var/www/html/page2.html
As you can see, the context has not changed. It is still user_home_t, even though the file is now in the /var/www/html directory.
system_u:object_r:user_home_t:s0 /var/www/html/page2.html
Now, try to access this new page using curl. SELinux will block the access due to the context mismatch.
curl http://localhost/page2.html
You will receive a "403 Forbidden" error from the server. This is not a traditional file permission issue; it is SELinux enforcing its security policy and denying the httpd process from reading a file with the user_home_t label.
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /page2.html
on this server.</p>
</body></html>
This demonstrates a classic SELinux problem. In the next step, you will learn how to fix this by changing the file's context.