Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

4.7. SELinux 컨텍스트 - 파일 레이블 지정

SELinux를 실행하는 시스템에서는 보안 관련 정보를 나타내는 방식으로 모든 프로세스와 파일에 레이블이 지정됩니다. 이 정보를 SELinux 컨텍스트라고 합니다. 파일의 경우 ls -Z 명령을 사용하여 확인할 수 있습니다.
~]$ ls -Z file1
-rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
이 예에서 SELinux는 사용자(unconfined_u), 역할(object_r), 유형(user_home_t) 및 수준(s0)을 제공합니다. 이 정보는 액세스 제어 결정을 내리는 데 사용됩니다. DAC 시스템에서는 Linux 사용자 및 그룹 ID를 기반으로 액세스가 제어됩니다. SELinux 정책 규칙은 DAC 규칙 후에 확인됩니다. DAC 규칙이 먼저 액세스를 거부하면 SELinux 정책 규칙이 사용되지 않습니다.
참고
기본적으로 새로 생성된 파일과 디렉터리는 상위 디렉터리의 SELinux 유형을 상속합니다. 예를 들어 etc_t 유형으로 레이블이 지정된 /etc 디렉토리에 새 파일을 만들 때 새 파일은 동일한 유형을 상속합니다.
~]$ ls -dZ - /etc
drwxr-xr-x. root root system_u:object_r:etc_t:s0       /etc
~]# touch /etc/file1
~]# ls -lZ /etc/file1
-rw-r--r--. root root unconfined_u:object_r:etc_t:s0   /etc/file1
SELinux는 chcon,semanage fcontext,restorecon, matchpathcon 과 같은 파일 시스템 레이블을 관리하기 위해 여러 명령을 제공합니다.

4.7.1. 임시 변경 사항: chcon

chcon 명령은 파일의 SELinux 컨텍스트를 변경합니다. 그러나 chcon 명령으로 변경한 내용은 파일 시스템 레이블 재레이블 또는 restorecon 명령 실행에서 지속되지 않습니다. SELinux 정책은 사용자가 지정된 파일에 대한 SELinux 컨텍스트를 수정할 수 있는지 여부를 제어합니다. chcon 을 사용하는 경우 사용자는 변경할 SELinux 컨텍스트의 전체 또는 일부를 제공합니다. 잘못된 파일 유형은 액세스를 거부하는 SELinux의 일반적인 원인입니다.

참고 자료

  • chcon -t type file-name 명령을 실행하여 파일 유형을 변경합니다. 여기서 typehttpd_sys_content_t 와 같은 SELinux 유형이며 file-name 은 파일 또는 디렉토리 이름입니다.
    ~]$ chcon -t httpd_sys_content_t file-name
  • chcon -R -t type directory-name 명령을 실행하여 디렉터리 및 해당 내용의 유형을 변경합니다. 여기서 typehttpd_sys_content_t 와 같은 SELinux 유형이며 directory-name 은 디렉터리 이름입니다.
    ~]$ chcon -R -t httpd_sys_content_t directory-name

절차 4.6. 파일 또는 디렉토리 유형 변경

다음 절차에서는 유형 및 SELinux 컨텍스트의 다른 특성을 변경하는 방법을 보여줍니다. 이 섹션의 예제에서는 디렉터리에 대해 동일한 작업을 수행합니다(예: file1 이 디렉터리인 경우).
  1. 홈 디렉터리로 변경합니다.
  2. 새 파일을 생성하고 SELinux 컨텍스트를 봅니다.
    ~]$ touch file1
    ~]$ ls -Z file1
    -rw-rw-r--  user1 group1 unconfined_u:object_r:user_home_t:s0 file1
    
    이 예에서 file1 의 SELinux 컨텍스트에는 SELinux unconfined_u 사용자, object_r 역할, user_home_t 유형 및 s0 수준이 포함됩니다. SELinux 컨텍스트의 각 부분에 대한 설명은 2장. SELinux 컨텍스트 의 내용을 참조하십시오.
  3. 다음 명령을 입력하여 유형을 samba_share_t 로 변경합니다. t 옵션은 유형만 변경합니다. 그런 다음 변경 사항을 확인합니다.
    ~]$ chcon -t samba_share_t file1
    ~]$ ls -Z file1 
    -rw-rw-r--  user1 group1 unconfined_u:object_r:samba_share_t:s0 file1
    
  4. 다음 명령을 사용하여 file1 파일의 SELinux 컨텍스트를 복원합니다. v 옵션을 사용하여 변경 사항을 확인하십시오.
    ~]$ restorecon -v file1
    restorecon reset file1 context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:user_home_t:s0
    
    이 예제에서는 이전 유형 samba_share_t 가 올바른 user_home_t 유형으로 복원됩니다. 타겟 정책(Red Hat Enterprise Linux의 기본 SELinux 정책)을 사용하는 경우 restorecon 명령은 /etc/selinux/targeted/contexts/files/ 디렉터리에 있는 파일을 읽고 어떤 SELinux 컨텍스트 파일을 보유할지 확인합니다.

절차 4.7. 디렉토리 및 콘텐츠 유형 변경

다음 예제에서는 새 디렉터리를 만들고 디렉터리의 파일 유형을 Apache HTTP 서버에서 사용하는 유형으로 변경하는 방법을 보여줍니다. 이 예제의 구성은 Apache HTTP 서버가 /var/www/html/대신 다른 문서 루트를 사용하려는 경우에 사용됩니다.
  1. root 사용자로 이 디렉터리에 새 web/ 디렉터리를 만든 다음 3개의 빈 파일(file1,file2file3)을 만듭니다. 이 디렉터리의 web/ 디렉토리와 파일에는 default_t 유형으로 레이블이 지정됩니다.
    ~]# mkdir /web
    ~]# touch /web/file{1,2,3}
    ~]# ls -dZ /web
    drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /web
    
    ~]# ls -lZ /web
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:default_t:s0 file3
    
  2. root로 다음 명령을 입력하여 web/ 디렉토리(및 해당 콘텐츠)의 유형을 httpd_sys_content_t 로 변경합니다.
    ~]# chcon -R -t httpd_sys_content_t /web/
    ~]# ls -dZ /web/
    drwxr-xr-x  root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/
    
    ~]# ls -lZ /web/
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:httpd_sys_content_t:s0 file3
    
  3. 기본 SELinux 컨텍스트를 복원하려면 root로 restorecon 유틸리티를 사용합니다.
    ~]# restorecon -R -v /web/
    restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    restorecon reset /web/file3 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0
    
chcon 에 대한 자세한 내용은 chcon(1) 도움말 페이지를 참조하십시오.
참고
유형 적용은 SELinux 대상 정책에 사용되는 기본 권한 제어입니다. 대부분의 경우 SELinux 사용자 및 역할은 무시할 수 있습니다.