Red Hat System Administration II
Persistently change the SELinux context of a directory and its contents.
Outcomes
Configure the
ApacheHTTP server to publish web content from a non-standard document root.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start selinux-filecontexts
Instructions
Log in to
serveraas thestudentuser and switch to therootuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$ [student@servera ~]$sudo -i[sudo] password for student:student[root@servera ~]#Configure Apache to use a document directory in a non-standard location.
Create the
/customdirectory.[root@servera ~]#
mkdir /customCreate the
index.htmlfile in the/customdirectory that contains theThis is SERVERA.text.[root@servera ~]#
echo 'This is SERVERA.' > /custom/index.htmlConfigure Apache to use the new directory location. Edit the Apache
/etc/httpd/conf/httpd.confconfiguration file, and replace the two occurrences of the/var/www/htmldirectory with the/customdirectory. You can use thevim /etc/httpd/conf/httpd.confcommand to do so. The following example shows the expected content of the/etc/httpd/conf/httpd.conffile.[root@servera ~]#
cat /etc/httpd/conf/httpd.conf...output omitted...DocumentRoot"/custom" ...output omitted... <Directory"/custom"> ...output omitted...
Start and enable the Apache web service and confirm that the service is running.
Start and enable the Apache web service by using the
systemctlcommand.[root@servera ~]#
systemctl enable --now httpdCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.Verify that the service is running.
[root@servera ~]#
systemctl status httpd●httpd.service- The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active:active (running)since Wed 2022-04-06 05:21:19 EDT; 22s ago Docs: man:httpd.service(8) Main PID: 1676 (httpd) ...output omitted... Apr 06 05:21:19 servera.lab.example.com systemd[1]: Starting The Apache HTTP Server... Apr 06 05:21:19 servera.lab.example.com systemd[1]:Started The Apache HTTP Server. Apr 06 05:21:19 servera.lab.example.com httpd[1676]: Server configured, listening on: port 80
Open a web browser on
workstationand try to view thehttp://servera/index.htmlweb page. You get an error message that you do not have permission to access the file.To grant access to the
index.htmlfile onservera, you must configure the SELinux context. Define an SELinux file context rule that sets the context type tohttpd_sys_content_tfor the/customdirectory and all the files under it.[root@servera ~]#
semanage fcontext -a \-t httpd_sys_content_t '/custom(/.*)?'Correct the file contexts in the
/customdirectory.[root@servera ~]#
restorecon -Rv /customRelabeled /custom from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 Relabeled /custom/index.html from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0Try to view
http://servera/index.htmlagain in the web browser on theworkstationmachine. You should see theThis is SERVERA.message.Return to the
workstationmachine as thestudentuser.[root@servera ~]#
exitlogout [student@servera ~]$exitlogout Connection to servera closed. [student@workstation ~]$