전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]

티스토리 뷰

container로 실습하기

ugo 시스템

파일에 대한 권한 제어는 아래와 같은 ugo 시스템을 사용합니다.

ugo 권한 수정

rwx의 위치에 따라 아래와 같이 2진수로 치환한 후 10진수로 바꾸면 각 권한은 아래와 같이 4, 2, 1이 됩니다.
r: 100 -> 4
w: 010 -> 2
x: 001 -> 1

따라서 아래와 같이 다양하게 조합하여 권한을 줄 수 있습니다.
rwx -> 7
rw -> 6
rx -> 5

sh-4.4# touch abc
sh-4.4# ls -l
total 4
-rw-r--r-- 1 root root     0 Jun  3 07:48 abc
drwxrwxr-- 2 root user1 4096 Jun  3 07:35 noaccess
sh-4.4# chmod 440 noaccess
sh-4.4# ls -l
total 4
-rw-r--r-- 1 root root     0 Jun  3 07:48 abc
dr--r----- 2 root user1 4096 Jun  3 07:35 noaccess

또한 아래와 같이 변경할 수도 있습니다.

sh-4.4# touch abc && chmod 000 abc
sh-4.4# ls -l
total 0
---------- 1 root root 0 Jun  3 07:53 abc
sh-4.4# chmod +r abc
sh-4.4# ls -l
total 0
-r--r--r-- 1 root root 0 Jun  3 07:53 abc
sh-4.4# chmod +x abc
sh-4.4# ls -l
total 0
-r-xr-xr-x 1 root root 0 Jun  3 07:53 abc
sh-4.4# chmod +w abc
sh-4.4# ls -l
total 0
-rwxr-xr-x 1 root root 0 Jun  3 07:53 abc
sh-4.4# chmod -rx abc
sh-4.4# ls -l
total 0
--w------- 1 root root 0 Jun  3 07:53 abc
sh-4.4# chmod -w abc
sh-4.4# ls -l
total 0
---------- 1 root root 0 Jun  3 07:53 abc

sh-4.4# chmod a+w abc
sh-4.4# ls -l
total 0
--w--w--w- 1 root root 0 Jun  3 07:53 abc
sh-4.4#

수정, 삭제

파일을 수정하려면 그 파일에 대한 w권한이 있어야 합니다.
주의할점은 r과 x는 ugo전체에 적용되지만 w는 u파트에만 적용니다.
w까지 전체 다 적용할때는 'chmod a+w '처럼 'a'를 앞에 붙이십시오.

[ocean1@557bc45cf6f2 tmp]$ touch abc
[ocean1@557bc45cf6f2 tmp]$ chmod 244 abc
[ocean1@557bc45cf6f2 tmp]$ ls -al
total 12
drw-rwxrw- 2 root   admins 4096 Jun  3 07:00 .
drw-rwxrw- 1 root   admins 4096 Jun  2 08:28 ..
--w-r--r-- 1 ocean1 group1    0 Jun  3 07:00 abc

내용 편집은 되지만 내용을 읽을 수 없음  
[ocean1@557bc45cf6f2 tmp]$ echo "aaa" > abc
[ocean1@557bc45cf6f2 tmp]$ cat abc
cat: abc: Permission denied

읽기 권한을 부여하면 이제 읽을 수 있음
[ocean1@557bc45cf6f2 tmp]$ chmod 644 abc
[ocean1@557bc45cf6f2 tmp]$ cat abc
aaa

파일을 삭제할때도 w권한이 있어야 합니다.

[ocean1@557bc45cf6f2 tmp]$ touch abc && chmod 244 abc
[ocean1@557bc45cf6f2 tmp]$ ls -l
total 0
--w-r--r-- 1 ocean1 group1 0 Jun  3 07:24 abc
[ocean1@557bc45cf6f2 tmp]$ rm abc

r권한만 있을 경우에는 확인 후 삭제 합니다.

[ocean1@557bc45cf6f2 tmp]$ touch abc && chmod 444 abc
[ocean1@557bc45cf6f2 tmp]$ ls -l
total 0
-r--r--r-- 1 ocean1 group1 0 Jun  3 07:25 abc
[ocean1@557bc45cf6f2 tmp]$ rm abc
rm: remove write-protected regular empty file 'abc'? n
[ocean1@557bc45cf6f2 tmp]$

디렉토리는 rwx권한이 모두 있어야 접근이 됩니다.

sh-4.4# mkdir noaccess && chmod 444 noaccess
sh-4.4# ls -l
total 4
dr--r--r-- 2 root root 4096 Jun  3 07:35 noaccess
sh-4.4# cd noaccess
sh-4.4# su - user1
Last login: Wed Jun  3 07:34:20 UTC 2020 on pts/1
[user1@557bc45cf6f2 ~]$ cd /root/tmp/noaccess
-bash: cd: /root/tmp/noaccess: Permission denied
[user1@557bc45cf6f2 ~]$

이번에는 Others에 rw권한을 부여해 보겠습니다.  
[user1@557bc45cf6f2 ~]$ exit
logout
sh-4.4# chmod 446 /root/tmp/noaccess
sh-4.4# su - user1
Last login: Wed Jun  3 07:35:27 UTC 2020 on pts/1
[user1@557bc45cf6f2 ~]$ cd /root/tmp/noaccess
-bash: cd: /root/tmp/noaccess: Permission denied

이번에는 Others에 rwx권한을 부여해 보겠습니다. 
[user1@557bc45cf6f2 ~]$ exit
logout
sh-4.4# chmod 447 /root/tmp/noaccess
sh-4.4# su - user1
Last login: Wed Jun  3 07:37:30 UTC 2020 on pts/1
[user1@557bc45cf6f2 ~]$ cd /root/tmp/noaccess

chown

owner를 변경합니다.

sh-4.4# chmod 774 noaccess
sh-4.4# ls -l
total 4
drwxrwxr-- 2 root root 4096 Jun  3 07:35 noaccess

ocean1 유저는 Others그룹에 해당하므로 noaccess디렉토리를 접근 못합니다.  
sh-4.4# su - ocean1
Last login: Wed Jun  3 07:27:10 UTC 2020 on pts/1
[ocean1@557bc45cf6f2 ~]$ cd /root/tmp/noaccess
-bash: cd: /root/tmp/noaccess: Permission denied
[ocean1@557bc45cf6f2 ~]$ exit
logout

이제 noaccess의 소유자 그룹을 root에서 admins로 바꾸겠습니다.  
sh-4.4# chown root:admins noaccess

ocean1은 admins그룹에 속해 있으므로 이제 noaccess디렉토리를 접근할 수 있습니다.  
sh-4.4# groups ocean1
ocean1 : group1 group2 group3 admins
sh-4.4# ls -l
total 4
drwxrwxr-- 2 root admins 4096 Jun  3 07:35 noaccess
sh-4.4# su - ocean1
Last login: Wed Jun  3 07:41:32 UTC 2020 on pts/1
[ocean1@557bc45cf6f2 ~]$ cd /root/tmp/noaccess

아래는 chown을 사용하는 예제입니다.

sh-4.4# chown :root noaccess
sh-4.4# ls -l
total 4
drwxrwxr-- 2 root root 4096 Jun  3 07:35 noaccess
sh-4.4# chown user1: noaccess
sh-4.4# ls -l
total 4
drwxrwxr-- 2 user1 user1 4096 Jun  3 07:35 noaccess
sh-4.4# chown root noaccess
sh-4.4# ls -l
total 4
drwxrwxr-- 2 root user1 4096 Jun  3 07:35 noaccess
sh-4.4#

sticky directory bit

sticky(어려운이라는 뜻)디렉토리는 공용으로 사용하는 디렉토리내의 파일이 함부로 삭제되는것을 방지합니다.
지정 방법은 아래 예제와 같이 'o+t' 또는 '1'+rwx권한을 이용하면 됩니다.

'o+t'라는 의미는 ugo스키마에서 others에 't'를 추가하라는 의미입니다.

Others는 ugo스키마에서 '1'이므로 '1'+rwx형식으로 sticky directory bit를 지정하는 겁니다.

file 속성의 가장 마지막이 't'로 변경됩니다.

[ocean@bastion ~]$ ls -l
total 12
drwxrwxr-x 2 ocean ocean  4096 Jun 24 11:44 stickydir1
drwxrwxr-x 2 ocean ocean  4096 Jun 24 11:45 stickydir2
drwxrwxr-x 2 ocean admins 4096 Jun 24 11:39 tmp
[ocean@bastion ~]$ chmod o+t stickydir1
[ocean@bastion ~]$ chmod 1775 stickydir2
[ocean@bastion ~]$ ls -l
total 12
drwxrwxr-t 2 ocean ocean  4096 Jun 24 11:44 stickydir1
drwxrwxr-t 2 ocean ocean  4096 Jun 24 11:45 stickydir2
drwxrwxr-x 2 ocean admins 4096 Jun 24 11:39 tmp

사용 예제

sh-4.4# mkdir shared

shared디렉토리를 777로 바꾸면서 맨 앞에 '1'을 붙여 sticky 디렉토리로 만듭니다. 
sh-4.4# chmod 1777 shared

ugo의 맨 마지막에 't'가 들어가 있습니다.  
sh-4.4# ls -l
total 4
drwxrwxrwt 2 root root 4096 Jun  3 08:43 shared
sh-4.4# cd shared
sh-4.4# ls -l
total 0
sh-4.4# touch sticky1
sh-4.4# ls -l
total 0
-rw-r--r-- 1 root root 0 Jun  3 08:44 sticky1
sh-4.4# chmod 777 sticky1
sh-4.4# ls -l
total 0
-rwxrwxrwx 1 root root 0 Jun  3 08:44 sticky1

소유자가 아닌 user는 내용을 편집할 수는 있으나 sticky디렉토리내의 파일을 삭제할 수는 없습니다. 
sh-4.4# su - ocean1
Last login: Wed Jun  3 08:41:26 UTC 2020 on pts/1
[ocean1@557bc45cf6f2 ~]$ echo "written by ocean1" >> /root/tmp/shared/sticky1
[ocean1@557bc45cf6f2 ~]$ cat /root/tmp/shared/sticky1
written by ocean1
[ocean1@557bc45cf6f2 ~]$ rm /root/tmp/shared/sticky1
rm: cannot remove '/root/tmp/shared/sticky1': Operation not permitted

동일 group에 속해 있는 경우에도 sticky directory bit로 지정된 디렉토리 내 파일은 삭제할 수 없습니다.

[ocean@bastion shared]$ ls -al
total 12
drwxrwxrwt   2 root root   4096 Jun 24 11:52 .
drwxrwxrwt. 15 root root   4096 Jun 24 11:52 ..
-rwxrwxrwx   1 root admins   34 Jun 24 11:52 nodelete
[ocean@bastion shared]$ groups ocean
ocean : ocean admins
[ocean@bastion shared]$ rm -f nodelete
rm: cannot remove ‘nodelete’: Operation not permitted

파일의 owner가 되면 삭제됩니다.
[ocean@bastion shared]$ sudo chown ocean nodelete
[sudo] password for ocean:
[ocean@bastion shared]$ ls -l
total 4
-rwxrwxrwx 1 ocean admins 34 Jun 24 11:52 nodelete
[ocean@bastion shared]$ rm -f nodelete
[ocean@bastion shared]$ ls -l
total 0

sticky 디렉토리 해제는 'o-t' 또는 '0'+rwx권한을 사용합니다.

sh-4.4# chmod o-t shared
sh-4.4# chmod 0777 dummy
sh-4.4# ls -l
total 8
drwxrwxrwx 2 root root 4096 Jun  3 08:50 dummy
drwxrwxrwx 2 root root 4096 Jun  3 08:54 shared

set userid bit

setuid bit로 지정된 프로그램을 실행하면 현재 사용자가 아닌 그 프로그램 소유자의 권한으로 실행됩니다.
대표적으로 사용되는 프로그램이 /usr/bin/passwd입니다.
passwd 변경 시 /etc/passwd와 /etc/shadow파일을 update해야 하는데 일반 user는 권한이 없습니다.
그래서 일반user에게 암호변경 권한을 주고 싶을때는 /usr/bin/passwd파일에 setuid bit를 지정합니다.

  • 지정 방법: 'chmod u+s <파일명>' 또는 'chmod 4+rwx권한 <파일명>'
  • 해제 방법: 'chmod u-s <파일명>' 또는 'chmod 0+rwx권한 <파일명>'

'u+s'라는 의미는 ugo스키마에서 user에 's'를 추가하라는 의미입니다.

user는 ugo스키마에서 '4'이므로 '4'+rwx형식으로 지정하는 겁니다.

sh-4.2# cd /
sh-4.2# cd tmp
sh-4.2# echo "can't read file except root" > noread
sh-4.2# useradd user1
sh-4.2# chmod 000 noread

user1은 noread파일을 접근할 권한이 없습니다.  
sh-4.2# su - user1
[user1@d40f95e875c2 ~]$ cat /tmp/noread
cat: /tmp/noread: Permission denied
[user1@d40f95e875c2 ~]$ exit
logout

/usr/bin/cat파일에 setuid bit를 지정합니다.  
sh-4.2# chmod u+s /usr/bin/cat
sh-4.2# su - user1
Last login: Wed Jun  3 10:52:43 UTC 2020 on pts/0
[user1@d40f95e875c2 ~]$ cat /tmp/noread
can't read file except root

set groupid bit

setuid bit가 프로그램 owner의 권한으로 실행되게 하는 방법이라면,
setgid bit는 프로그램 owner 그룹의 권한으로 실행되게 하는 방법입니다.

  • 지정 방법: 'chmod g+s <파일명>' 또는 'chmod 2+rwx권한 <파일명>'
  • 해제 방법: 'chmod g-s <파일명>' 또는 'chmod 0+rwx권한 <파일명>'

'g+s'라는 의미는 ugo스키마에서 group에 's'를 추가하라는 의미입니다.

group은 ugo스키마에서 '2'이므로 '2'+rwx형식으로 지정하는 겁니다.


admins그룹이 읽을 수 있는 noread파일 생성  

sh-4.2# cd /tmp
sh-4.2# echo "can't read except admins group" > noread
sh-4.2# chmod 640 noread
sh-4.2# groupadd admins
sh-4.2# chown :admins noread
sh-4.2# ls -l noread
-rw-r----- 1 root admins 31 Jun  3 11:14 noread

/usr/bin/cat파일에 setgid bit 지정

sh-4.2# chmod g+s /usr/bin/cat
sh-4.2# ls -l /usr/bin/cat
-rwxr-sr-x 1 root root 54080 Nov  5  2016 /usr/bin/cat
sh-4.2# chown :admins /usr/bin/cat
sh-4.2# ls -l /usr/bin/cat
-rwxr-xr-x 1 root admins 54080 Nov  5  2016 /usr/bin/cat


user1은 /tmp/noread파일을 읽을 수 없음
sh-4.2# useradd user1
sh-4.2# su - user1
Last login: Wed Jun  3 11:00:27 UTC 2020 on pts/1
[user1@b97449acab2d ~]$ cat /tmp/noread
cat: /tmp/noread: Permission denied

user1을 admins그룹에 추가하면, 이제 cat명령이 admins그룹 권한으로 실행되어 /tmp/noread파일을 읽을 수 있게됨 
[user1@b97449acab2d ~]$ exit
logout
sh-4.2# usermod -aG admins user1
sh-4.2# su - user1
Last login: Wed Jun  3 11:16:39 UTC 2020 on pts/1
[user1@b97449acab2d ~]$ cat /tmp/noread
can't read except admins group

umask

umask는 file과 디렉토리의 기본 rwx권한을 지정합니다.
계산식은 아래와 같습니다. 음수이면 0으로 계산합니다.

umask의 값을 보기 쉬운 문자로 볼수도 있습니다.

❯ umask -S
u=rwx,g=rx,o=rx

~
❯ umask 0000

~
❯ umask -S
u=rwx,g=rwx,o=rwx

 

아래는 사용 예제입니다.

[user1@b97449acab2d tmp]$ umask 0000
[user1@b97449acab2d tmp]$ mkdir dir1
[user1@b97449acab2d tmp]$ touch file1
[user1@b97449acab2d tmp]$ ls -l
total 4
drwxrwxrwx 2 user1 user1 4096 Jun 3 11:37 dir1
-rw-rw-rw- 1 user1 user1 0 Jun 3 11:37 file1

[user1@b97449acab2d tmp]$ umask 0113
[user1@b97449acab2d tmp]$ mkdir dir2
[user1@b97449acab2d tmp]$ ls -l
total 8
drwxrwxrwx 2 user1 user1 4096 Jun 3 11:37 dir1
drw-rw-r-- 2 user1 user1 4096 Jun 3 11:39 dir2


[user1@b97449acab2d tmp]$ umask 0002
[user1@b97449acab2d tmp]$ touch file2
[user1@b97449acab2d tmp]$ ls -l
total 8
drwxrwxrwx 2 user1 user1 4096 Jun 3 11:37 dir1
drw-rw-r-- 2 user1 user1 4096 Jun 3 11:39 dir2
-rw-rw-rw- 1 user1 user1 0 Jun 3 11:37 file1
-rw-rw-r-- 1 user1 user1 0 Jun 3 11:40 file2
댓글

전자책 출간 알림 [마이크로서비스패턴 쉽게 개발하기]