Using chcon

There are two ways to use chcon to fix an incorrect SELinux type on a file or directory.  The first is to just manually specify the proper type:

[donnie@localhost html]$ sudo chcon -t httpd_sys_content_t index.html
[sudo] password for donnie:

[donnie@localhost html]$ ls -Z
-rw-rw-r--. donnie donnie unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[donnie@localhost html]$

We can use chcon to change any part of the context, but as I keep saying, we're only interested in the type, which gets changed with the -t option. You can see in the ls -Z output that the command was successful.

The other way to use chcon is to reference a file that has the proper context.  For demo purposes, I changed the index.html file back to the home directory type and have created a new file within the /var/www/html directory:

[donnie@localhost html]$ ls -Z
-rw-rw-r--. donnie donnie unconfined_u:object_r:user_home_t:s0 index.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 some_file.html
[donnie@localhost html]$

As you can see, any files that I create within this directory will automatically have the proper SELinux context settings. Now, let's use that new file as a reference in order to set the proper context on the index.html file:

[donnie@localhost html]$ sudo chcon --reference some_file.html index.html
[sudo] password for donnie:

[donnie@localhost html]$ ls -Z
-rw-rw-r--. donnie donnie unconfined_u:object_r:httpd_sys_content_t:s0 index.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 some_file.html
[donnie@localhost html]$

So, I used the --reference option and specified the file that I wanted to use as a reference. The file that I wanted to change is listed at the end of the command.

Now, that's all good, but I want to find an easier way that doesn't require quite as much typing. After all, I am an old man, and I don't want to overexert myself. So, let's take a look at the restorecon utility.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.182.179