50.2. Administrator Control of SELinux
50.2.1. Viewing the Status of SELinux
sestatus
command provides a configurable view into the status of SELinux. The simplest form of this command shows the following information:
~]# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 21
Policy from config file: targeted
-v
option includes information about the security contexts of a series of files that are specified in /etc/sestatus.conf
:
~]# sestatus -v
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 21
Policy from config file: targeted
Process contexts:
Current context: user_u:system_r:unconfined_t
Init context: system_u:system_r:init_t
/sbin/mingetty system_u:system_r:getty_t
/usr/sbin/sshd system_u:system_r:unconfined_t:s0-s0:c0.c1023
File contexts:
Controlling term: user_u:object_r:devpts_t
/etc/passwd system_u:object_r:etc_t
/etc/shadow system_u:object_r:shadow_t
/bin/bash system_u:object_r:shell_exec_t
/bin/login system_u:object_r:login_exec_t
/bin/sh system_u:object_r:bin_t -> system_u:object_r:shell_exec_t
/sbin/agetty system_u:object_r:getty_exec_t
/sbin/init system_u:object_r:init_exec_t
/sbin/mingetty system_u:object_r:getty_exec_t
/usr/sbin/sshd system_u:object_r:sshd_exec_t
/lib/libc.so.6 system_u:object_r:lib_t -> system_u:object_r:lib_t
/lib/ld-linux.so.2 system_u:object_r:lib_t -> system_u:object_r:ld_so_t
-b
displays the current state of booleans. You can use this in combination with grep or other tools to determine the status of particular booleans:
~]# sestatus -b | grep httpd | grep on$
httpd_builtin_scripting on
httpd_disable_trans on
httpd_enable_cgi on
httpd_enable_homedirs on
httpd_unified on
50.2.2. Relabeling a File System
The recommended method for relabeling a file system is to reboot the machine. This allows the init
process to perform the relabeling, ensuring that applications have the correct labels when they are started and that they are started in the right order. If you relabel a file system without rebooting, some processes may continue running with an incorrect context. Manually ensuring that all the daemons are restarted and running in the correct context can be difficult.
touch /.autorelabel
reboot
init.rc
checks for the existence of /.autorelabel
. If this file exists, SELinux performs a complete file system relabel (using the /sbin/fixfiles -f -F relabel
command), and then deletes /.autorelabel
.
It is possible to relabel a file system using the fixfiles
command, or to relabel based on the RPM database:
fixfiles
command:
fixfiles relabel
fixfiles -R <packagename> restore
fixfiles
to restore contexts from packages is safer and quicker.
Warning
fixfiles
on the entire file system without rebooting may make the system unstable.
fixfiles relabel
prompts for approval to empty /tmp/
because it is not possible to reliably relabel /tmp/
. Since fixfiles
is run as root, temporary files that applications are relying upon are erased. This could make the system unstable or behave unexpectedly.
50.2.3. Managing NFS Home Directories
nfs_t
type, which is not a type that httpd_t
is allowed to execute.
nfs_t
, try mounting the home directories with a different context:
mount -t nfs -o context=user_u:object_r:user_home_dir_t \
fileserver.example.com:/shared/homes/ /home
Warning
httpd
can execute scripts. If you do this for user home directories, it gives the Apache HTTP Server increased access to those directories. Remember that a mountpoint label applies to the entire mounted file system.
50.2.4. Granting Access to a Directory or a Tree
root_t
, tmp_t
, and usr_t
that grant read access for a directory. These types are suitable for directories that do not contain any confidential information, and that you want to be widely readable. They could also be used for a parent directory of more secured directories with different contexts.
avc: denied
message, there are some common problems that arise with directory traversal. For example, many programs run a command equivalent to ls -l /
that is not necessary to their operation but generates a denial message in the logs. For this you need to create a dontaudit
rule in your local.te
file.
path=/
component. This path is not related to the label for the root file system, /
. It is actually relative to the root of the file system on the device node. For example, if your /var/
directory is located on an LVM (Logical Volume Management [22]) device, /dev/dm-0
, the device node is identified in the message as dev=dm-0
. When you see path=/
in this example, that is the top level of the LVM device dm-0
, not necessarily the same as the root file system designation /
.
50.2.5. Backing Up and Restoring the System
50.2.6. Enabling or Disabling Enforcement
setenforce
command to change between permissive and enforcing modes at runtime. Use setenforce 0
to enter permissive mode; use setenforce 1
to enter enforcing mode.
sestatus
command displays the current mode and the mode from the configuration file referenced during boot:
~]# sestatus | grep -i mode
Current mode: permissive
Mode from config file: permissive
~]#setenforce 1
~]#sestatus | grep -i mode
Current mode: enforcing Mode from config file: permissive
named
daemon and SELinux, you can turn off enforcing for just that daemon.
getsebool
command to get the current status of the boolean:
~]# getsebool named_disable_trans
named_disable_trans --> off
~]#setsebool named_disable_trans 1
~]#getsebool named_disable_trans
named_disable_trans --> on
Note
-P
option to make the change persistent across reboots.
~]# getsebool -a | grep disable.*on
httpd_disable_trans=1
mysqld_disable_trans=1
ntpd_disable_trans=1
setsebool
command:
setsebool -P httpd_disable_trans=1 mysqld_disable_trans=1 ntpd_disable_trans=1
togglesebool <boolean_name>
to change the value of a specific boolean:
~]#getsebool httpd_disable_trans
httpd_disable_trans --> off ~]#togglesebool httpd_disable_trans
httpd_disable_trans: active
Use the following procedure to change a runtime boolean using the GUI.
Note
- On themenu, point to and then click to display the Security Level Configuration dialog box.
- Click the SELinux tab, and then click Modify SELinux Policy.
- In the selection list, click the arrow next to the Name Service entry, and select the Disable SELinux protection for named daemon check box.
- Clickto apply the change. Note that it may take a short time for the policy to be reloaded.

Figure 50.1. Using the Security Level Configuration dialog box to change a runtime boolean.
setenforce(1)
, getenforce(1)
, and selinuxenabled(1)
commands.
50.2.7. Enable or Disable SELinux
Important
/etc/sysconfig/selinux
file. This file is a symlink to /etc/selinux/config
. The configuration file is self-explanatory. Changing the value of SELINUX
or SELINUXTYPE
changes the state of SELinux and the name of the policy to be used the next time the system boots.
~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=permissive
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
# SETLOCALDEFS= Check local definition changes
SETLOCALDEFS=0
Use the following procedure to change the mode of SELinux using the GUI.
Note
- On themenu, point to and then click to display the Security Level Configuration dialog box.
- Click the SELinux tab.
- In the SELinux Setting select either
Disabled
,Enforcing
orPermissive
, and then click . - If you changed from
Enabled
toDisabled
or vice versa, you need to restart the machine for the change to take effect.
/etc/sysconfig/selinux
.
50.2.8. Changing the Policy
/etc/sysconfig/selinux
:
SELINUXTYPE=<policyname>
/etc/selinux/
. This assumes that you have the custom policy installed. After changing the SELINUXTYPE
parameter, run the following commands:
touch /.autorelabel
reboot
Note
- Ensure that the complete directory structure for the required policy exists under
/etc/selinux
. - On themenu, point to and then click to display the Security Level Configuration dialog box.
- Click the SELinux tab.
- In the Policy Type list, select the policy that you want to load, and then click . This list is only visible if more than one policy is installed.
- Restart the machine for the change to take effect.

Figure 50.2. Using the Security Level Configuration dialog box to load a custom policy.
50.2.9. Specifying the Security Context of Entire File Systems
mount -o context=
command to set a single context for an entire file system. This might be a file system that is already mounted and that supports xattrs, or a network file system that obtains a genfs label such as cifs_t
or nfs_t
.
httpd_sys_content_t
:
mount -t nfs -o context=system_u:object_r:httpd_sys_content_t \
server1.example.com:/shared/scripts /var/www/cgi
Note
httpd
and SELinux problems, reduce the complexity of your situation. For example, if you have the file system mounted at /mnt
and then symbolically linked to /var/www/html/foo
, you have two security contexts to be concerned with. Because one security context is of the object class file and the other of type lnk_file, they are treated differently by the policy and unexpected behavior may occur.
50.2.10. Changing the Security Category of a File or User
50.2.11. Running a Command in a Specific Security Context
runcon
command to run a command in a specific context. This is useful for scripting or for testing policy, but care should be taken to ensure that it is implemented correctly.
~/bin/contexttest
is a user-defined script.)
runcon -t httpd_t ~/bin/contexttest -ARG1 -ARG2
runcon user_u:system_r:httpd_t ~/bin/contexttest
50.2.12. Useful Commands for Scripts
getenforce
- This command returns the enforcing status of SELinux.
setenforce [ Enforcing | Permissive | 1 | 0 ]
- This command controls the enforcing mode of SELinux. The option
1
orEnforcing
tells SELinux to enter enforcing mode. The option0
orPermissive
tells SELinux to enter passive mode. Access violations are still logged, but not prevented. selinuxenabled
- This command exits with a status of
0
if SELinux is enabled, and1
if SELinux is disabled.~]#
selinuxenabled
~]#echo $?
0 getsebool [-a] [boolean_name]
- This command shows the status of all booleans (
-a
) or a specific boolean (<boolean_name>
). setsebool [-P] <boolean_name> value | bool1=val1 bool2=val2 ...
- This command sets one or more boolean values. The
-P
option makes the changes persistent across reboots. togglesebool boolean ...
- This command toggles the setting of one or more booleans. This effects boolean settings in memory only; changes are not persistent across reboots.
50.2.13. Changing to a Different Role
newrole
command to run a new shell with the specified type and/or role. Changing roles is typically only meaningful in the strict policy; the targeted policy is generally restricted to a single role. Changing types may be useful for testing, validation, and development purposes.
newrole -r <role_r> -t <type_t> [-- [ARGS]...]
ARGS
are passed directly to the shell specified in the user's entry in the /etc/passwd
file.
Note
newrole
command is part of the policycoreutils-newrole
package, which is required if you install the strict or MLS policy. It is not installed by default in Red Hat Enterprise Linux.
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.