Permissions and owners

The semanage command

Manages the SELinux policy of the machine: which ports each service may use, which security label folders carry and which booleans are on. Changes survive reboots.

Syntax

semanage objeto -a|-d|-m|-l [opciones]Try it in the terminal →

Most useful options

-aAdds a new rule (add)
-mModifies an existing rule (modify)
-dDeletes a rule (delete)
-lLists the existing rules (list)
-t <tipo>SELinux type to assign, such as http_port_t or httpd_sys_content_t
-p <tcp|udp>Protocol of the ports the rule manages

Copy-ready examples

$ sudo semanage port -l | grep httpports SELinux associates with web services.

Example output

http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
$ sudo semanage port -a -t http_port_t -p tcp 8080lets Apache or Nginx listen on port 8080.
$ sudo semanage fcontext -a -t httpd_sys_content_t '/srv/web(/.*)?'tells SELinux which label the files in /srv/web must have. It only saves the rule.
$ sudo restorecon -Rv /srv/webapplies the labels to the existing files. Without this step, new rules do not affect what is already there.

Example output

Relabeled /srv/web/index.html from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: semanage: command not foundThe package is not installed. On Fedora and RHEL the tool comes in policycoreutils-python-utils; on Debian and Ubuntu, SELinux is not enabled by default.Install it with dnf install policycoreutils-python-utils if your distribution uses SELinux.

Typical mistakes

  • fcontext rules are stored and survive reboots, but existing files need restorecon to change.
  • A manual chcon is lost at the next restorecon, so it is not a lasting fix.
  • If a port is already assigned, -a fails: change its rule with -m instead of adding it.

Differences from similar commands

  • chcon: changes a file's label, but only temporarily: restorecon resets it according to semanage's rules.
  • restorecon: applies the rules to the files that already exist. semanage only stores the rules.
  • setsebool: turns policy booleans on or off, such as httpd_can_network_connect. It does not create new rules.

Security

Giving a port or a folder a type that is too broad can expose a service that SELinux was containing. Do not disable SELinux to fix a permission: check the warnings with ausearch -m avc.

FAQ

Why does my site return 403 even though the Unix permissions are right?

Because SELinux blocks access: the content does not have the type the server expects. Add the rule with fcontext, run restorecon and check ausearch -m avc -ts recent.

What is the difference between semanage and chcon?

chcon changes one file's label and it is lost at the next restorecon. semanage stores the rule in the policy and survives reboots.

Can I use it on Ubuntu?

Ubuntu uses AppArmor instead of SELinux, so these commands make no sense there.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course