 <p>The most basic security toggle on your Linux computer is the <pre><code>setenforce</code></pre> command. Using just a single <pre><code>setenforce</code></pre> instruction, you can configure SELinux to allow a violation it would normally prevent.  There are two states: Enabled and Permissive.  By default, SELinux is <pre><code>Enabled</code></pre> (also represented as <pre><code>1</code></pre> when using Boolean values).  To set SELinux to permissive mode:</p>

<p><pre><code>$ sudo setenforce Permissive</code></pre></p>
<p>When something works in Permissive mode, you've successfully identified the symptom, but you haven't fixed the problem yet.  Activate Enforcing mode again:</p>
<p><pre><code>$ sudo setenforce Enforcing</code></pre></p>
<h2>Check the status of SELinux</h2>
<p>You can check the state of SELinux at any time using the <pre><code>sestatus</code></pre> command:</p>
<p><pre><code>$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
[...]</code></pre></p>
<h2>Look at labels and contexts</h2>
<p>If you have a running Linux system, then you have an example of what SELinux requires for normal operation.  You don't have to learn about security contexts or memorize labels.  For most anything you try to do on your computer, there are likely already files doing something similar.  Use those files as templates.</p>
<p>You can look at the security labels of any file you have access to by using the <pre><code>-Z</code></pre> (that's a capital Z) option of <pre><code>ls</code></pre>:</p>
<p><pre><code>$ touch hellotouch hello.txt
$ ls -Z hello.txt
unconfined_u:object_r:user_home_t:s0 hello</code></pre></p>
<p>An empty file created by a user in the user's own home directory has, as you might expect, a very specific security profile.  Even with the executable bit set, that file would not be permitted to run as a systemwide service.  It just doesn't have the correct security context.</p>
<p>If you use an <pre><code>ll</code></pre> alias, try adding the <pre><code>-Z</code></pre> option to its option list so you get used to seeing SELinux labels.  The more you see what labels exist on your system, and how they relate to various system roles, you're more likely to recognize when they're wrong.</p>
<h2>Copy contexts</h2>
<p>Suppose you were developing a custom SELinux service for your laptop.  You've written a shell script, a service file, and you've placed them in the appropriate system locations.  You're also careful to set ownership and permissions correctly.  But no matter what you do, you get errors when attempting to start the service.</p>
<p>You suspect that SELinux might be preventing an unrecognized service from running.  That would normally be appreciated, but in this case you want to make an exception.</p>
<p>First, confirm that the service runs successfully with SELinux in Permissive mode:</p>
<p><pre><code>$ sudo setenforce Permissive
$ sestatus | grep Current
Current mode:                 permissive
$ sudo systemctl start hello.service || echo "fail"
$
$ sudo setenforce Enforcing</code></pre></p>
<p>Then look at the files you've created using the <pre><code>-Z</code></pre> and compare them with other files that you know to be working properly.  Note the differences:</p>
<p><pre><code>$ ls -Z /usr/lib/systemd/system/hello.service
unconfined_u:object_r:systemd_unit_file_t:s0
$ ls -Z /usr/lib/systemd/system/rdisc.service
system_u:object_r:rdisc_unit_file_t:s0</code></pre></p>
<p>The working service (<pre><code>rdisc.service</code></pre> in this example, chosen at random) features the <pre><code>system_u</code></pre> label as well as a special <pre><code>rdisc_unit_file_t</code></pre> label.  Suppose you know from previous experience with <pre><code>ls -Z</code></pre> that a common SELinux label for systemd service files is <pre><code>systemd_unit_file_t</code></pre> so you ignore that difference.  However, <pre><code>unconfined_u</code></pre> and <pre><code>system_u</code></pre> seem to be important.</p>
<p>Use the <pre><code>chcon</code></pre> ("change context") command to change the security context of your service file:</p>
<pre>
$ sudo chcon system_u:object_r:systemd_unit_file_t:s0 \
/usr/lib/systemd/system/hello.service
$ ls -Z /usr/lib/systemd/system/hello.service
system_u:object_r:systemd_unit_file_t:s0
</pre>
<p>Your systemd service is probably triggering some executable file on your system.  If you created that yourself, it probably also has the incorrect security context.  Comparing it to a known working script:</p>
<p><pre><code>$ ls -Z /usr/bin/example.sh
unconfined_u:object_r:gconf_home_t:s0
$ ls -Z /usr/bin/brltty-prologue.sh
system_u:object_r:bin_t:s0</code></pre></p>
<p>Again, there's one obvious difference, which you can correct with <pre><code>chcon</code></pre>:</p>
<p><pre><code>$ chcon system_u:object_r:bin_t:s0 \

/usr/bin/example.sh</code></pre></p>
