엔지니어 은성의 성장록

[RHCSA] RHEL7 - SELinux 컨텍스트 변경 본문

RHCSA

[RHCSA] RHEL7 - SELinux 컨텍스트 변경

엔지니어 은성 2021. 11. 9. 17:56

▶ SELinux

관리자가 시스템 액세스 권한을 효과적으로 제어할 수 있게 하는 Linux 시스템용 보안 아키텍처

SELinux는 레이블 시스템으로 작동 - 시스템의 모든 파일, 프로세스 및 포트에 이와 연결된 SELinux 레이블이 있음.

▶ SELinux 콘텍스트

  • user : system_u 사용자로 할당 받음
  • role : system_u 사용자는 object_r 역할을 부여 받음
  • type : httpd_config_t 유형을 부여 받음

▶ 파일의 SELinux 콘텍스트 변경 (chcon, restorecon)

- 보안 콘텍스트 확인 Z 옵션으로 확인 가능

- chcon : 파일의 콘텍스트(보안 문맥) 변경 

user 사용자 바꿀 때는 -urole을 바꿀 때는 -rtype을 바꿀 때는 -t 

- restorecon : 파일 또는 디렉터리의 SELinux 콘텍스트를 변경하는데 선호되는 방법 

restorecon 명령어는 직접 보안 콘텍스트를 지정해야 하는 chcon과 다르게 잘못 설정된 파일과 디렉터리를 보안 정책에 맞도록 콘텍스트를 설정해줍니다.

# mkdir /virtual
# ls -Zd /virtual
# chcon -t httpd_sys_content_t /virtual
# ls -Zd /virtual
# restorecon -v /virtual
# ls -Zd /virtua

▶SELinux의 콘텍스트와 콘텐츠 영구 변경

파일 : /etc/httpd/conf/httpd.conf

1. yum을 이용하여 Apache 웹서버 설치

# yum install -y httpd

2. 비표준 위치에 문서 루트 사용을 위해 Apache 구성 

 

2.1 새 문서 루트 /custom 생성

# mkdir /custom

2.2 index.html 생성

# echo 'This is serverX.' > /custom/index.html

2.3 새 위치를 문서 루트로 사용하도록 Apache 구성

-/var/www/html 두 개를 /custom과 /etc/httpd/conf/httpd.conf로 변경 

# vi /etc/httpd/conf/httpd.conf
/var/www/html -> /custom으로 변경

3. Apache 웹 서비스 시작

# systemctl start httpd

4. 웹브라우저에서 httpd://localhost/index.html 확인

파일에 액세스 할 수 있는 권한이 없다는 오류 메시지 확인

 

5. /custom 아래의 모든 파일에 대한 콘텍스트 유형을 httpd_sys_content_t로 설정하는 SELinux 파일 콘텍스트 규칙 정의

# semanage fcontext -a -t httpd_sys_content_t '/custom(/.*)?'

6. restorecon을 사용하여 콘텍스트 변경

# restorecon -Rv /custom

 

 

 

 

Comments