Using sed to configure SELinux

For a while we have been looking for a practical example as to an area you would want to use SED.

sed is the stream editor. It reads a file line by line, applies editing commands such as search and replace, and with -i writes the result back to the file. That makes it perfect for small, repeatable config changes in scripts and kickstart files, where opening an editor is not an option.

Prerequisites

  • Root access (the file is owned by root).
  • A RHEL/CentOS-family system with SELinux, which keeps its boot-time mode in /etc/selinux/config.

The manual way

So, normally, we would go through the painful process of vi /etc/selinux/config, edit it to

vi /etc/selinux/config on the BASE server

and then change SELINUX=enforcing to disabled

/etc/selinux/config with an arrow pointing at the SELINUX=enforcing line

save, and then reboot. How fun right?

The sed one-liner

How about a one-liner that does that whole process?

sed -i 's/enforcing/disabled/g' /etc/selinux/config /etc/selinux/config
sed -i 's/enforcing/disabled/g' /etc/selinux/config /etc/selinux/config

check that out!

cat /etc/selinux/config after sed: SELINUX=disabled, with the comment lines also changed to disabled

all that’s needed is a reboot

The command broken down

here’s the command broken down:

Same screenshot: sed -i 's/enforcing/disabled/g' one-liner to be broken down
  • sed -i: edit the file in place. (-i means in-place, not ignore case; sed’s case-insensitive match is the I flag at the end of the expression.) -i.bak keeps a backup copy as config.bak.
  • s/ means substitute.
  • enforcing/disabled/ means you are substituting enforcing for disabled.
  • g replaces every match on a line, not just the first.
  • The file names: sed edits every file listed. Naming /etc/selinux/config twice just edits the same file twice (the second pass has nothing left to change), so one file name is enough.

The general form is:

sed -i 's/OriginalWord/ReplacementWord/g' /path/to/file

A safer version

Look closely at the cat output above: the plain pattern also rewrote the comment lines, which now read “disabled – SELinux security policy is enforced”. It works, but it is untidy and confusing for the next admin. Anchor the match to the setting itself, keep a backup, and accept any current value:

sed -i.bak 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
grep ^SELINUX= /etc/selinux/config

^ matches only at the start of a line, so comments are left alone. Swap in disabled or enforcing as needed.

Verify

getenforce
sestatus

The file only takes effect at boot. To switch between enforcing and permissive immediately, without a reboot, use setenforce 0 (permissive) or setenforce 1 (enforcing). You cannot go to or from disabled without rebooting.

Troubleshooting and notes

  • Prefer permissive over disabled: permissive still logs denials (to /var/log/audit/audit.log) so you can see what would have been blocked, and it keeps file labels up to date.
  • Re-enabling after disabled: files created while SELinux was off are unlabeled. Run touch /.autorelabel and reboot so the system relabels before enforcing.
  • RHEL, AlmaLinux and Rocky 9/10: SELINUX=disabled in the config file no longer fully disables SELinux; the kernel still starts with SELinux enabled but loads no policy. To really disable it, use the kernel argument: grubby --update-kernel ALL --args selinux=0. Setting permissive with sed as above still works normally.

that’s it!

To get a hosted Linux server visit www.zwiegnet.com/go today!

Related guides: Disable SELinux CentOS 7 | Show Line Numbers with Vi

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *