Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

semanage

Change the SELinux policy settings that admins need: file context rules, port types, booleans, permissive domains, user mappings.

Level
RHCSA exam
Group
SELinux and security
Needs root
Yes
Package
policycoreutils-python-utils

What it does

semanage writes permanent changes to the local policy. The parts you use most:

  • semanage fcontext — which label files in a path should get (then apply with restorecon),
  • semanage port — which ports a service type may use,
  • semanage boolean — list and set booleans with descriptions,
  • semanage permissive — make one process type permissive.

Install it with dnf install policycoreutils-python-utils (dnf provides '*/semanage').

How to write it

semanage fcontext -a|-d|-m -t TYPE [-f FTYPE] "PATH-REGEX"
semanage port -a|-d|-m -t TYPE -p tcp|udp PORT[-PORT]
semanage boolean -l | -m --on|--off BOOLEAN
semanage permissive -a|-d|-l TYPE
semanage SUBCOMMAND -l [-C] | -E | -D

Main parts

fcontext
File context rules (paths → types).
port
Port types.
boolean
Booleans (with descriptions).
permissive
Permissive process types.
login
Linux users → SELinux users.
user
SELinux users (roles, levels).
module
Policy modules (list, enable, disable, remove).
interface, node, ibpkey, ibendport
Network interface, node and InfiniBand types.
dontaudit
Turn dontaudit rules off or on (to see hidden denials).
import, export
Save / load all local changes (copy to other servers).

Options 18

Most used

Actions

-a, --add
Add a rule.
-d, --delete
Delete a rule.
-m, --modify
Change a rule (for example a port already defined for another type).
-l, --list
List rules.
-C, --locallist
With -l: only your local changes.
-E, --extract
Print local changes as semanage commands.
-D, --deleteall
Delete all local changes of this kind.

Values

-t, --type TYPE
The SELinux type.
-p, --proto tcp|udp
Protocol (port).
-f, --ftype a|f|d|c|b|s|l|p
File kind for fcontext (all, file, dir, char, block, socket, link, pipe).
-e, --equal PATH
fcontext: label PATH like another path (equivalence), like -e /var/www /srv/www.
-r, --range RANGE
MLS/MCS range.
-s, --seuser SEUSER
SELinux user.
-1, --on / -0, --off
boolean: on or off (with -m).

General

-n, --noheading
No header in lists.
-N, --noreload
Do not reload the policy after the change.
-S, --store STORE
Another policy store.
-h, --help
Show help.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Label a new web folder

    semanage fcontext -a -t httpd_sys_content_t "/srv/web(/.*)?"
    restorecon -Rv /srv/web

    The regex covers the folder and everything below it.

  2. 2Same labels as a known folder

    semanage fcontext -a -e /var/www /srv/www

    /srv/www gets the labels of /var/www.

  3. 3List your own file rules

    semanage fcontext -l -C

    Only local changes.

  4. 4Find the rule for a path

    semanage fcontext -l | grep '^/var/www'

    Which types the policy gives there.

  5. 5Allow a new port

    semanage port -a -t http_port_t -p tcp 82

    Apache may now listen on 82.

  6. 6Port already used by another type

    semanage port -m -t http_port_t -p tcp 8080

    -m moves it (8080 is http_cache_port_t by default).

  7. 7Show port types

    semanage port -l | grep -w ssh_port_t
    ssh_port_t                     tcp      22, 2222

    Which ports sshd may use.

  8. 8Booleans with descriptions

    semanage boolean -l | grep httpd_can_network

    Current state, default, and what it does.

  9. 9Make one domain permissive

    semanage permissive -a httpd_t
    semanage permissive -d httpd_t

    Debug one service; the rest stays enforcing.

  10. 10Copy local changes to another server

    semanage export > /root/selinux-local.txt
    semanage import < /root/selinux-local.txt

    Same rules on all web servers.

  11. 11See hidden denials

    semanage dontaudit off

    Shows denials that are normally not logged; turn it on again after debugging.

Exit codes

0Done.
1Error (like "already defined" or a bad regex).

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man 8 semanage