{"id":2132,"date":"2021-05-30T01:08:45","date_gmt":"2021-05-30T01:08:45","guid":{"rendered":"https:\/\/dft.wiki\/?p=2132"},"modified":"2026-04-21T13:34:08","modified_gmt":"2026-04-21T17:34:08","slug":"setting-up-selinux-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2132","title":{"rendered":"Setting Up SELinux on Ubuntu"},"content":{"rendered":"<p>SELinux is an upgrade to the file system that creates labels to enhance the policies for groups and users.<\/p>\n<p>Created by Red Hat and the NSA it also comes natively in CentOS and Fedora but can be installed in any other Linux and Unix distributions because it uses Kernel security modules.<\/p>\n<p>Install the packets:<\/p>\n<pre>sudo apt update\r\nsudo apt install policycoreutils selinux-utils selinux-basics -y<\/pre>\n<p>Check the status and activate:<\/p>\n<pre>sestatus\r\nsudo selinux-activate\r\nsudo reboot<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2152\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se03.png\" alt=\"\" width=\"747\" height=\"404\" srcset=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se03.png 747w, https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se03-300x162.png 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><\/p>\n<p>Then it will reboot automatically one more time.<\/p>\n<p>By default, it will be permissive when enabled.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2149\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se01.png\" alt=\"\" width=\"419\" height=\"175\" srcset=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se01.png 419w, https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se01-300x125.png 300w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/p>\n<p>The permissive mode will allow the applications to access the file even if it is not labeled accordingly but it will log everything.<\/p>\n<p>Enforcing will restrict access based on the labeling policy of SELinux.<\/p>\n<pre>sudo selinux-config-enforcing\r\nsestatus<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2150\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se02.png\" alt=\"\" width=\"425\" height=\"175\" srcset=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se02.png 425w, https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2021\/05\/se02-300x124.png 300w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/p>\n<p>The following commands can get the current mode and set the current mode:<\/p>\n<pre>getenforce\r\nsetenforce\r\nsetenforce 0<\/pre>\n<p>The mode can also be defined in the configuration file:<\/p>\n<pre>sudo nano \/etc\/selinux\/config<\/pre>\n<p>The configuration file can be also modified by following the commands:<\/p>\n<pre>sudo sed -i 's\/SELINUX=.*\/SELINUX=<strong>enforcing<\/strong>\/' \/etc\/selinux\/config<\/pre>\n<pre>sudo sed -i 's\/SELINUX=.*\/SELINUX=<strong>permissive<\/strong>\/' \/etc\/selinux\/config<\/pre>\n<pre>sudo sed -i 's\/SELINUX=.*\/SELINUX=<strong>disabled<\/strong>\/' \/etc\/selinux\/config<\/pre>\n<p>By listing the content of the directory the labels can be seen for each file:<\/p>\n<pre>ls -Zd<\/pre>\n<p>Change the label:<\/p>\n<pre>semanage fcontext -a -t <span style=\"color: #ff0000;\"><strong>FILE_TYPE<\/strong><\/span> \"\/web\"<\/pre>\n<p>For a webserver, the <strong>FILE_TYPE<\/strong> would be <span style=\"color: #ff0000;\"><strong>httpd_sys_content_t<\/strong><\/span>.<\/p>\n<p>Apply the changes:<\/p>\n<pre>restorecon -Rv \/web<\/pre>\n<p>Checking the log messages:<\/p>\n<pre>grep AVC \/var\/log\/messages<\/pre>\n<p>AVC (Access Vector Cache) can be understood as Access Violation.<\/p>\n<p>Another way is to look for Alerts on the same file:<\/p>\n<pre>grep sealert \/var\/log\/messages<\/pre>\n<p>Then copy and execute the command relative to the alert you want more details, for example:<\/p>\n<pre>sealert -l askjc1c63deb-2af3-9d23-a3247a234ab34<\/pre>\n<p>Note that files created will inheritance the labels from the parent directory. In the case of moving files, they will maintain the labels and will require to be re-labeled.<\/p>\n<p>Keep the system in permissive mode, check the logs, and apply all the necessary labels according to the applications running.<\/p>\n<p>In conclusion, SELinux requires a lot of work labeling all the file-system before enabling enforcing mode. Otherwise, it will crash many applications and possibly the system itself.<\/p>\n<hr \/>\n<p>SE Linux is embedded in RHEL-based distributions and the installation of it on a Debian-based distribution looks simple but might create side effects and needs to be done very carefully.<\/p>\n<p>A native alternative for Debian-based dist that is not embedded but runs as a service is called AppArmor and may have a similar security feature. Read more about it at [<a href=\"https:\/\/dft.wiki\/?p=2532\">Link<\/a>].<\/p>\n<p>Check out Seccomp too. It can sandbox the applications to prevent a vulnerability in it to protect the pode\/node\/cluster of a Kubernetes environment, for example. It uses a profile to allow\/deny permissions in order to restrict the process to the scope to the namespaces the app can see (as granular as per system call). Not too different but simple than apparmor and much simple than selinux but same concept.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELinux is an upgrade to the file system that creates labels to enhance the policies [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2132","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2132"}],"version-history":[{"count":10,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2132\/revisions"}],"predecessor-version":[{"id":5484,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2132\/revisions\/5484"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}