Linux

Linux CentOS - Selinux 관련 명령 (권한 Permission 오류가 발생하는 경우)

Ryan's Tech Note 2023. 2. 16. 01:20

Linux 는 여러가지 방법으로 권한을 통제할 수 있다.

(1) 디렉토리나 파일에 사용자 별 실행 권한을 주는 방법 (chmod)

(2) firewalld 방화벽을 이용한 네트워크 레벨 접근 통제

(3) selinux를 이용한 데몬별 포트 / 디렉토리 / 파일 접근 통제

 

Selinux 상태는 3가지가 있다.

(1) Enforcing - 켜짐

(2) Permissive - 꺼짐 & (1) 모드에서의 차단 등의 이벤트가 발생하면 로그로 남김

(3) Disabled - 꺼짐

 

평시에는 (1) 으로 운용하다, 점검이 필요한 경우 (2) 을 사용하는 전략을 사용하고 (3) 은 가능한 사용하지 않는다.

 

  • Linux CentOs 에서 권한 오류가 발생하는 경우 테스트 해볼 수 있는 Selinux 관련 명령어

현재 Selinux 상태

# getenforce

 

Permissive 상태로 설정
# setenforce 0

 

Enforcing 상태로 설정
# setenforce 1

 

만약, 어떤 권한 오류가 발생해서 setenforce 1 로 했는데 오류가 발생하지 않는다면, Selinux 권한 문제가 발생한 것이고 아래와 같은 방법으로 조치하고 setenforce 0 으로 바꾸고 조치가 됐는지 테스트한다.

 

예) 폴더에 권한을 줌 (mysqld_db_t =mysql 데몬, httpd_modules_t =apache 데몬)
# chcon -R -t mysqld_db_t /log/mysql

 

예) 파일에 권한을 줌 (httpd_modules_t =apache 데몬)
# chcon -u system_u -r object_r -t httpd_modules_t /etc/httpd/modules/mod_jk.so

 

지나치게 이것저것 권한을 주면 안된다. 정확하게 오류가 발생되는 포인트에만 최소한으로 권한을 주어야 함을 명심하라.

 

  • Selinux 환경 설정 (reboot 시 적용)

CentOS 8
# vi /etc/selinux/config

CentOS 7 이전
# vi /etc/sysconfig/selinux

 

  • File Context 보기

# semanage fcontext -l|grep mysql

 

  • 허용된 포트 확인

# semanage port -l | grep -w http_port_t

또는

# sepolicy network -t http_port_t

또는

# sepolicy network -p 8009

 

  • 허용 포트 추가

# semanage port -a -t http_port_t -p tcp 8009

 

정책 삭제 옵션은 -d 이다.
Port tcp/900 already defined 과같은 에러가 발생하면-a 대신 -m 옵션을 사용한다.

 

  • 룰 확인

디렉토리에 적용된 룰 확인

# ll -Z

 

데몬에 적용된 룰 확인

# ps -ZC httpd 

 

  • Selinux 정책 초기화
setenforce 0
yum remove selinux-policy\*
rm -rf /etc/selinux/targeted /etc/selinux/config
yum install selinux-policy-targeted
yum install selinux-policy-devel policycoreutils-gui  ***use this if SELinux removed by yum remove.
touch /.autorelabe

참고 : https://serverfault.com/questions/958218/how-to-reset-selinux-to-its-initial-state-in-centos7

 


Selinux 문제 관련 로그 조회 및 해결 방법
# tail -f /var/log/audit/audit.log
type=AVC msg=audit(1676583753.352:3792): avc:  denied  { name_connect } for  pid=75979 comm="httpd" dest=8009 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket permissive=0

audit2why 를 하면 해결 방법을 알려준다.

# audit2why < /var/log/audit/audit.log
type=AVC msg=audit(1676583753.352:3792): avc:  denied  { name_connect } for  pid=75979 comm="httpd" dest=8009 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket permissive=0

        Was caused by:
        One of the following booleans was set incorrectly.
        Description:
        Allow httpd to can network connect

        Allow access by executing:
        # setsebool -P httpd_can_network_connect 1
        Description:
        Allow httpd to graceful shutdown

        Allow access by executing:
        # setsebool -P httpd_graceful_shutdown 1
        Description:
        Allow httpd to can network relay

        Allow access by executing:
        # setsebool -P httpd_can_network_relay 1
        Description:
        Allow nis to enabled

        Allow access by executing:
        # setsebool -P nis_enabled 1

나에게 해당하는 것으로 권한을 부여하면 된다.


 

  • 사용하지 말 것

# chcon -R -t httpd_modules_t /var/cache/httpd/mod_jk

이걸 사용하면 httpd 재기동시 오류가 나게된다. 기억!!