Linuxのファイルアクセス権限

  • ls -a で表示されるファイルのアクセス権限
$ ls -laF
drwx------  2 testuser testuser 4096  1月 19 11:34 ./
drwxr-xr-x 11 root     root     4096  1月 19 11:34 ../
-rw-r--r--  1 testuser testuser   24  1月 19 11:34 .bash_logout
-rw-r--r--  1 testuser testuser  176  1月 19 11:34 .bash_profile
-rw-r--r--  1 testuser testuser  124  1月 19 11:34 .bashrc
  • 先頭の1文字がファイルの形式を表わす
- 通常のファイル
d ディレクトリ
l シンボリックリンク
  • 2文字目~10文字目がアクセス権限を表わす
    • 9文字は3文字ずつ区切って見る
      • 最初の3文字=ファイル所有者に対する権限
      • 次の3文字=ファイル所有グループに対する権限
      • 最後の3文字=その他のユーザに対する権限

ファイルアクセス権限

  • アクセス権限は、lsコマンドでは9文字で表示されるが、内部では12ビットの情報を持つ。
ビット位置 内容
11 set-user-id
10 set-group-id
9 sticky
8 所有者 読み出し権限
7 所有者 書き込み権限
6 所有者 実行権限
5 所有グループ 読み出し権限
4 所有グループ 書き込み権限
3 所有グループ 実行権限
2 その他ユーザ 読み出し権限
1 その他ユーザ 書き込み権限
0 その他ユーザ 実行権限


set-user-id

  • プログラムの実行権限を変更するための情報。
  • このビットが0の場合(通常のファイル)は、実行ユーザの権限で実行される。
  • このビットが1の場合、そのファイルの所有者権限で実行される。
  • passwdコマンドはset-user-idがON、つまり一般ユーザが実行した場合でもroot権限で実行される。
    • 一般ユーザでは書き換え権限がないファイル(/etc/shadow)の書き換えが必要であるため。
    • 所有者の実行権限が s 表示となる
$ ls -laF /usr/bin/passwd
-rwsr-xr-x 1 root root 22984  /usr/bin/passwd*

set-group-id

  • ファイルの所有グループを変更するための情報。ディレクトリに対して設定する。
  • このビットが0のディレクトリにファイルを作成した場合、ファイルの所有グループ=作成者のグループになる。
  • このビットが1のディレクトリにファイルを作成した場合、ファイルの所有グループ=ディレクトリの所有グループになる。
  • 以下、set-group-id=0のgroup_offディレクトリと、group_off=1のgroup_onディレクトリにファイルを作成してみる検証結果。
# mkdir group_off                                         rootでディレクトリを作成
# mkdir group_on
# chmod g+s group_on                                      group_onディレクトリのset-group-idをONにする
# chmod o+w group_off group_on                            その他ユーザの書き込み権限を設定
# ls -l
drwxr-xrwx 2 root root 4096  1月 19 12:00 group_off
drwxr-srwx 2 root root 4096  1月 19 12:00 group_on        所有グループの実行権限が s 表示となる

# su testuser                                             testuserでファイルを作成
$ touch group_off/fileA
$ touch group_on/fileB
$ ls -l group*
group_off:
-rw-rw-r-- 1 testuser testuser 0  1月 19 12:05 fileA      所有グループ=作成者のグループ

group_on:
-rw-rw-r-- 1 testuser root 0  1月 19 12:05 fileB          所有グループ=ディレクトリの所有グループ

sticky

  • ファイルの所有者だけがファイルの削除・移動できるように設定するための情報。ディレクトリに対して設定する。
  • このビットが0のディレクトリのファイルは、ディレクトリ書き込み権限があるユーザは削除できる。
  • このビットが1のディレクトリのファイルは、ファイル所有者のみが削除できる。
  • /tmpに設定されている。
  • 以下、sticky=0のsticky_offディレクトリと、sticky=1のsticky_onディレクトリを検証。
# mkdir sticky_off                                        rootでディレクトリを作成
# mkdir sticky_on
# chmod o+t sticky_on
# chmod o+w sticky_off sticky_on                          その他ユーザの書き込み権限を設定
# ls -l
drwxr-xrwx 2 root root 4096  1月 19 12:28 sticky_off
drwxr-xrwt 2 root root 4096  1月 19 12:28 sticky_on       その他ユーザの実行権限が t 表示となる
# touch sticky_off/fileA
# touch sticky_on/fileB
# ls -l sticky*
sticky_off:
-rw-r--r-- 1 root root 0  1月 19 12:28 fileA

sticky_on:
-rw-r--r-- 1 root root 0  1月 19 12:28 fileB
# su testuser
$ rm -f sticky_off/fileA                        ディレクトリ書き込み権限あり=root所有のファイルを削除できる
$ rm -f sticky_on/fileB
rm: cannot remove `sticky_on/fileB': 許可されていない操作です
                                                      stucky=ONなので、root所有のファイルを削除できない

facebook slideshare rubygems github qiita