IT 공부내용 정리/Linux

터미널 명령어 - umask 기본 접근 권한의 설정을 바꿔야 할 때

따뜻한 차가움 2025. 3. 6. 14:25

사용 버전 : Linux 우분투(레드햇 계열) 24.04.01 LTS 버전
사용한 시스템 지역/언어 : 대한민국/한글

man umask 원문

더보기

umask(2)                      System Calls Manual                     umask(2)

NAME
       umask - set file mode creation mask

LIBRARY
       Standard C library (libc, -lc)

SYNOPSIS
       #include <sys/stat.h>

       mode_t umask(mode_t mask);

DESCRIPTION
       umask()  sets  the calling process's file mode creation mask (umask) to
       mask & 0777 (i.e., only the file permission bits of mask are used), and
       returns the previous value of the mask.

       The umask is used by open(2), mkdir(2), and  other  system  calls  that
       create files to modify the permissions placed on newly created files or
       directories.   Specifically,  permissions  in  the umask are turned off
       from the mode argument to open(2) and mkdir(2).

       Alternatively, if the parent directory has a default ACL (see  acl(5)),
       the umask is ignored, the default ACL is inherited, the permission bits
       are  set  based on the inherited ACL, and permission bits absent in the
       mode argument are turned off.  For example, the following  default  ACL
       is equivalent to a umask of 022:

           u::rwx,g::r-x,o::r-x

       Combining  the  effect of this default ACL with a mode argument of 0666
       (rw-rw-rw-), the resulting file permissions would be 0644 (rw-r--r--).

       The constants that should be used to specify mask are described in  in‐
       ode(7).

       The  typical  default  value for the process umask is S_IWGRP | S_IWOTH
       (octal 022).  In the usual case where the mode argument to  open(2)  is
       specified as:

           S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH

       (octal 0666) when creating a new file, the permissions on the resulting
       file will be:

           S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH

       (because 0666 & ~022 = 0644; i.e. rw-r--r--).

RETURN VALUE
       This  system call always succeeds and the previous value of the mask is
       returned.

STANDARDS
       POSIX.1-2008.

HISTORY
       POSIX.1-2001, SVr4, 4.3BSD.

NOTES
       A child process created via fork(2) inherits its parent's  umask.   The
       umask is left unchanged by execve(2).

       It  is  impossible to use umask() to fetch a process's umask without at
       the same time changing it.  A second call  to  umask()  would  then  be
       needed  to restore the umask.  The nonatomicity of these two steps pro‐
       vides the potential for races in multithreaded programs.

       Since Linux 4.7, the umask of any process can be viewed via  the  Umask
       field  of /proc/pid/status.  Inspecting this field in /proc/self/status
       allows a process to retrieve its umask without at the same time  chang‐
       ing it.

       The  umask  setting  also affects the permissions assigned to POSIX IPC
       objects (mq_open(3), sem_open(3), shm_open(3)), FIFOs (mkfifo(3)),  and
       UNIX  domain  sockets (unix(7)) created by the process.  The umask does
       not affect the permissions assigned to System V IPC objects created  by
       the process (using msgget(2), semget(2), shmget(2)).

SEE ALSO
       chmod(2), mkdir(2), open(2), stat(2), acl(5)

Linux man-pages 6.7               2023-10-31                          umask(2)

man umask 번역기 번역본

더보기

umask(2)      시스템 호출 설명서       umask(2)

이름
umask - 파일 모드 생성 마스크 설정

라이브러리
표준 C 라이브러리(libc, -lc)

요약
#include <sys/stat.h>

mode_t umask(mode_t mask);

설명
umask()는 호출 프로세스의 파일 모드 생성 마스크(umask)를
mask & 0777로 설정합니다(즉, mask의 파일 권한 비트만 사용됨). 그리고
마스크의 이전 값을 반환합니다.

umask는 open(2), mkdir(2) 및 새로 생성된 파일이나 디렉토리에 부여된 권한을 수정하기 위해 파일을 생성하는 다른 시스템 호출에서 사용됩니다. 특히, umask의 권한은 open(2) 및 mkdir(2)의 모드 인수에서 해제됩니다.

또는 부모 디렉토리에 기본 ACL이 있는 경우(acl(5) 참조),
umask가 무시되고, 기본 ACL이 상속되고, 권한 비트가
상속된 ACL에 따라 설정되고,
mode 인수에 없는 권한 비트가 꺼집니다. 예를 들어, 다음 기본 ACL은
umask 022와 동일합니다.

u::rwx,g::r-x,o::r-x

이 기본 ACL의 효과를 mode 인수 0666(rw-rw-rw-)과 결합하면 결과 파일 권한은 0644(rw-r--r--)가 됩니다.

mask를 지정하는 데 사용해야 하는 상수는 in‐
ode(7)에 설명되어 있습니다.

프로세스 umask의 일반적인 기본값은 S_IWGRP | S_IWOTH
(8진수 022)입니다. open(2)에 대한 모드 인수가 다음과 같이 지정된 일반적인 경우:

S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH

(8진수 0666) 새 파일을 만들 때 결과 파일의 권한은 다음과 같습니다.

S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH

(0666 & ~022 = 0644; 즉 rw-r--r--이기 때문).

반환 값
이 시스템 호출은 항상 성공하고 마스크의 이전 값이
반환됩니다.

표준
POSIX.1-2008.

역사
POSIX.1-2001, SVr4, 4.3BSD.

참고사항
fork(2)를 통해 생성된 자식 프로세스는 부모의 umask를 상속받습니다.
umask는 execve(2)에 의해 변경되지 않습니다.

umask()를 사용하여 프로세스의 umask를 가져오는 동시에 변경하지 않고는 불가능합니다. 그런 다음 umask()를 두 번째로 호출하여 umask를 복원해야 합니다. 이 두 단계의 비원자성은 멀티스레드 프로그램에서 경쟁의 가능성을 제공합니다.

Linux 4.7부터 모든 프로세스의 umask는 /proc/pid/status의 Umask
필드를 통해 볼 수 있습니다. /proc/self/status에서 이 필드를 검사하면 프로세스가 umask를 변경하지 않고도 검색할 수 있습니다.

umask 설정은 프로세스에서 생성한 POSIX IPC
객체(mq_open(3), sem_open(3), shm_open(3)), FIFO(mkfifo(3)) 및
UNIX 도메인 소켓(unix(7))에 할당된 권한에도 영향을 미칩니다. umask는 프로세스에서 생성한 System V IPC 객체에 할당된 권한에는 영향을 미치지 않습니다(msgget(2), semget(2), shmget(2) 사용).

또한 참조
chmod(2), mkdir(2), open(2), stat(2), acl(5)

Linux man-pages 6.7     2023-10-31      umask(2)

 

형태 : umask [옵션] [마스크값]

기능 : 파일 생성시 제외시킬 접근권한을 설정한다.

 

 

[마스크값]

3자리 인수와 4자리 인수를 사용할 경우 각각에 해당하는 인식방법이 다르다

 

3자리 인수를 사용할 경우 첫자리를 0으로 판단한 4자리 인수와 같게 취급한다.

4자리 인수를 사용할 경우 해당 값을 모두 마스크 값에 반영한다.

 

3자리 인수  002  만 사용한 경우

0 + 002 라는 형태로 인식하게 되며 0은 일반적인 상태에서 접근권한을 의미한다.

002는 접근권한에 대한 설정과 동일하다. 단, 제외할 대상을 지정한 것이므로

기타 사용자에 대해서 쓰기 권한을 제외하겠다는 뜻이다.

 

숫자로 표기한 권한설정

리눅스 명령어 - chmod

 

 

마스크값이 4자리인수의 경우

첫자리의 숫자에 따라 접근권한의 작동방식이 변경된다.

 

4*** : 해당 파일을 실행시키는 동안 파일 소유자(u)의 권한을 획득한다.

2*** : 해당 파일을 실행시키는 동안 파일 소유 그룹(g)의 권한을 획득한다.

1*** : 디렉토리에만 설정할 수 있으며 해당 권한이 설정된 경우 이 디렉토리에는 누구나 파일을 생성할 수 있다.

단, 파일은 파일을 생성한 계정이 소유자가 되며 소유자 외에는 해당 파일을 삭제할 수 없다.

 

특수 권한을 지정해줄 경우에는 기본적으로 실행권한을 가지고 있어야 한다.