Rocky 9 웹 폴더 403, SELinux 라벨 맞추기
권한은 맞는데 새 폴더만 403 이면 파일 라벨을 봅니다. ls -Z 로 확인하고, semanage fcontext 와 restorecon 으로 라벨을 영구히 맞춥니다.
기본 /var/www 가 아닌 /var/test_www 같은 폴더에서 웹 서버를 띄웠더니 403 Forbidden 이 나옵니다. chmod·소유자·서버 설정이 맞는데도 그렇다면 파일 SELinux 라벨을 봅니다. 프록시 쪽 거부(502)는 boolean 문제이니 Nginx 프록시 SELinux 글로 넘깁니다.
[1] SELinux 가 Enforcing 인지 봅니다.
# getenforce[2] 새 폴더와 기본 폴더의 라벨을 ls -Z 로 나란히 봅니다. 라벨의 세 번째 칸이 타입입니다.
# ls -Zd /var/www/html /var/test_www/html[3] 거부 기록(AVC)을 찾습니다. 아무것도 안 나오면 journalctl -t setroubleshoot 와 dmesg | grep -i -e type=1300 -e type=1400 도 순서대로 봅니다.
# ausearch -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -ts recent[4] 규칙이 가리키는 정상 라벨과 비교합니다. matchpathcon 은 정책상 그 경로가 가져야 할 라벨을 보여 줍니다. 공식 문서의 출력입니다.
# matchpathcon /var/www/html /var/test_www/html/var/www/html system_u:object_r:httpd_sys_content_t:s0/var/test_www/html system_u:object_r:var_t:s0[5] 새 폴더가 기본 폴더와 같은 라벨 규칙을 따르게 등록합니다. 이 명령은 규칙만 저장합니다.
# semanage fcontext -a -e /var/www /var/test_www[6] 실제 파일에 규칙을 적용합니다. 변경된 파일마다 한 줄씩 나옵니다.
# restorecon -Rv /var/test_wwwRelabeled /var/test_www/html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0Relabeled /var/test_www/html/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0[7] 등록된 규칙을 확인하고 같은 요청을 다시 보냅니다.
# semanage fcontext -l | grep test_wwwchcon 은 왜 쓰지 않나
chcon -t httpd_sys_content_t /var/test_www/html 도 그 순간에는 403 을 풉니다. 그러나 RHEL 문서는 chcon 으로 한 변경이 파일 시스템 재라벨에 유지되지 않는다고 적습니다. restorecon 은 정책에 등록된 기본 라벨로 되돌리는 도구라서, 등록(semanage fcontext)이 없으면 다음 재라벨 때 var_t 로 돌아가 같은 403 이 다시 납니다.
| 명령 | 하는 일 | 재라벨 뒤 |
|---|---|---|
chcon | 파일의 라벨만 바꿈 | 유지되지 않음 |
semanage fcontext + restorecon | 규칙을 등록하고 파일에 적용 | 유지됨 |