Tag Archives: contexts

SELinux Contexs

SELinux depends on a group of labels to make access decisions. So when you have your system installed with SELinux, the whole file system is labeled with a context and every file/object has a SELinux context described as the 4-uplet:


So every file, directory, stream, port … within your system has its own context and SELinux rules know which user should access which role to access which type.

When using targeted policy, you only need to understand the user and type in the 4-uplet context.

SELinux users are not the same as users you connect with, many linux users may have the same selinux user within their context.

Let’s have our hands on SELinux contexts:

  • Check SELinux context

The Z switch displays SELinux context of your file,port…

For example:

To check selinux context of a file,directory, use:

ls -lZ




To check selinux context of currently running process, for example apache:

Ps –efZ | grep [h]ttpd




Semanage wonderful command:

I use the semanage command to view and manage SELinux contexts. As I’m working with CentOS 6, I wasn’t it already installed:

Check which package provides the semanage command:

[root@sar ~]# yum provides *bin/semanage
updates | 3.4 kB 00:00
updates/primary_db | 3.5 MB 00:30
updates/filelists_db | 2.2 MB 00:05
policycoreutils-python-2.0.83-19.39.el6.i686 : SELinux policy core python
: utilities
Repo : base
Matched from:
Filename : /usr/sbin/semanage


Install it with:

[root@sar ~]# yum –y install policycoreutils-python


Now let’s try some of semanage options:

Discover the mapping between linux users and SELinux users:

# semanage login -l


Context of a port, for example http ports:

# semanage port -l | grep http


To list context of some files, use semanage as follows and grep on the name of your files:

#semanage fcontext -l | grep drupal



  • Change and modify SELinux contexts:


Commands like chcon and semanage allow you to alter SELinux contexts easily.

If you are just testing a new update, use chcon for temporary changes. With t for type, u for user… man chcon will do for the rest 🙂 :

#chcon –t  httpd_user_content_t /home/setuto/www

Updates using chcon are not persistent, if you relabel your file system or you run restorecon, you will lose the new configuration.


If you want to go for persistent changes, use semanage command instead. This command adds your new updates to SELinux configuration files saving them forever.

For example, use semanage fcontext wih appropriate switch like a to add and d to delete in order to change file’s context.

Here we change the type context of the directory /home/setuto/www from user_home_t to httpd_user_content :

#semanage fcontext -a -t httpd_user_content_t /home/setuto/www


Observe that semanage fcontext doesn’t change the context unless you run restorecon.

The restorecon command is very useful and you can use it to restore contexts modified by chcon command, restorecon returns back to last modification saved into selinux configuration files.

To compare current contexts with the default one, use matchpathcon command with filename.



For further details, man command will do 😉



Hands on SELinux

Selinux was such a burden for me years ago ( and still sometimes 🙂 ). First time I worked as a sys admin, I had to deploy an apache server with its document Root mounted as an NFS partition. I spent 2 days wondering and searching why in hell my apache couldn’t access its html directory. Too stupid when you found out that there is something called SELinux that is turned on by default to restrict access to your NFS partition by Apache processes!!!!

SELinux is such a wonderful tool for hardening your system and that can get your system to the chaos if you mess with it xD xD xD.

What is SELinux?

SELinux is a built-in kernel module with a bunch of rules and decisions determining who/what has the right to access what. This is done through a labeling of the whole file system, security decisions are then results of a combination of these labels and policy rules used.

It handles access to files/objects much more meticulously than do other classic permissions, ACLs …. But read/write, owner permissions and ACL applies before SELinux rules, meaning that SELinux rules applies only when DAC permissions allow access to an object.

What do you need to know about SELinux?

Suppose you have your kernel compiled with SELinux, and your system is installed with SELinux support, here are some basics to help you get familiar with SELinux (I’m using Centos 6).