متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 76Security · SELinux

How to find and fix SELinux denials

On RHEL, Rocky and Alma the rights look right, the service runs — and still Permission denied. Very often it is SELinux. The wrong fix is setenforce 0 (it turns off a strong protection). The right fix is one of three: the file label, a port label, or a boolean. This how-to finds the denial in the audit log, reads it, and applies the right fix.

  • 5 الدروس
  • 13 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

76.1

The quick answer

getenforce
Enforcing
ausearch -m AVC,USER_AVC -ts recent
sealert -a /var/log/audit/audit.log | head -40
restorecon -Rv /srv/www

sealert (package setroubleshoot-server) explains each denial in plain words and suggests the fix.

76.2

Step by step

  1. Is it SELinux? Look for denials around the time of the error:
ausearch -m AVC -ts recent
type=AVC msg=audit(1795604711.123:812): avc:  denied  { read } for  pid=2210 comm="nginx" name="index.html" scontext=system_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file permissive=0

Read it as: nginx (httpd_t) may not read a file labelled **user_home_t**.

  1. Choose the fix by the cause:
CauseTypical signFix
Wrong file labeltcontext=…user_home_t / default_tsemanage fcontext + restorecon
Service on a non-standard portname_bind / name_connect deniedsemanage port -a -t TYPE -p tcp PORT
A feature is off by defaultWeb server connects to a DB/networksetsebool -P BOOLEAN on
  1. File labels — tell SELinux what a folder is, then apply it:
semanage fcontext -a -t httpd_sys_content_t '/srv/www(/.*)?'
restorecon -Rv /srv/www
ls -Zd /srv/www

Files moved (mv) from home keep their old label — that is the classic case; restorecon fixes it.

  1. Ports:
semanage port -l | grep http_port_t
semanage port -a -t http_port_t -p tcp 8443
  1. Booleans:
getsebool -a | grep httpd_can_network
setsebool -P httpd_can_network_connect on
setsebool -P httpd_can_network_connect_db on

-P keeps it after reboot.

  1. Test without turning SELinux off — make only that one domain permissive for a moment, never the whole system:
semanage permissive -a httpd_t
semanage permissive -d httpd_t
  1. Last resort: a custom module (only after the fixes above, and after reading what it allows):
ausearch -m AVC -ts recent | audit2allow -M local_nginx
semodule -i local_nginx.pp

76.3

Check that it worked

The app works, ausearch -m AVC -ts recent shows no new denials, and getenforce still says Enforcing.

76.4

Common problems

ProblemCauseFix
ausearch finds nothing, still denieddontaudit rules hide some denials.semodule -DB (show all), test, then semodule -B again.
Label is right after chcon, wrong after relabelchcon is temporary.Use semanage fcontext + restorecon.
Service can not bind its portPort not in the SELinux port type.semanage port -a … (or -m to change an existing one).
502 from Nginx/Apache to an apphttpd_can_network_connect off.setsebool -P httpd_can_network_connect on.
Container volume deniedMissing :Z/:z label on the volume (Podman/Docker).Mount with :Z (private) or :z (shared).
Someone set SELINUX=disabledTurned off "to make it work".Turn it on (relabel at boot: touch /.autorelabel), then fix the real causes.

76.5

Go further

1Why is setenforce 0 not a fix?

It turns off SELinux protection for the whole system; fix the label, port or boolean instead.

2Files moved from a home folder into /srv/www are denied. What fixes it?

restorecon -Rv /srv/www (after semanage fcontext sets the right type for the path).

كل الأوامر في هذا الفصل