Ubuntu 14.04
Sponsored Link

ACL によるアクセス制御2016/01/17

 
ACL (Access Control Lists) の設定です。
ファイル/ディレクトリに対する [所有者/グループ/第三者] と [読み取り/書き込み/実行] のアクセスコントロールをより細かく設定可能です。
[1] ACL ツールをインストールします。
root@dlp:~#
apt-get -y install acl
[2] ACL を利用するには ext2/ext3/ext4 や xfs など、ACL に対応したファイルシステムで ACL オプションを有効にする必要があります。 Ubuntu では OS インストール時に設定されたデバイスで 且つ デフォルトの ext4 を利用中であれば、デフォルトマウントオプションに ACL オプションが設定され ACL が有効化された状態となっています。
# デフォルトマウントオプション確認

root@dlp:~#
tune2fs -l /dev/tmp-vg/root | grep "Default mount options"

Default mount options:   user_xattr acl    
# acl オプション設定済みとなっている
[3] HDD の増設等で、後から追加したデバイスで ACL を利用する場合は、ACL オプションを指定してマウントするか、 または デフォルトマウントオプションに追加しておく必要があります。
# acl オプションを有効にしてマウントする

root@dlp:~#
mount -o acl /dev/sdb1 /mnt

root@dlp:~#
mount | grep sdb1

/dev/sdb1 on /mnt type ext4 (rw,acl)
# デフォルトマウントオプションに追加する

root@dlp:~#
tune2fs -o acl /dev/sdb1

root@dlp:~#
tune2fs -l /dev/vdb1 | grep "Default mount options"

Default mount options: acl
[4] ACL の設定です。
例として /home に「test.txt」を所有者「root」のアクセス権「700」で作成し、そのファイルに ACL を設定します。
# /home/test.txt に対して、ユーザー "ubuntu" に r(read) を許可

root@dlp:~#
setfacl -m u:ubuntu:r /home/test.txt
# ACLを設定するとアクセス権の右に以下のように「+」が付加される

root@dlp:~#
ll /home/test.txt

-rwxr-----+ 1 root root 10 Jan 20 11:27 /home/test.txt*

# ACLを表示して設定を確認

root@dlp:~#
getfacl /home/test.txt

getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rwx
user:ubuntu:r--
group::---
mask::r--
other::---

# "ubuntu" ユーザーで確認

ubuntu@dlp:~$
cat /home/test.txt

ACL test file
# 正常に表示できた
# 他ユーザーで確認

debian@dlp:~$
cat /home/test.txt

cat: /home/test.txt: Permission denied    
# 正常に表示不可
[5] あるディレクトリ配下全てに再帰的に ACL を設定する。
# /home/testdir ディレクトリ配下に対して、ユーザー "ubuntu" に r(read) を許可

root@dlp:~#
setfacl -R -m u:ubuntu:r /home/testdir
root@dlp:~#
ll -laR /home/testdir

/home/testdir:
total 12
drwxr-----+ 2 root root 4096 Jan 20 11:33 ./
drwxr-xr-x  5 root root 4096 Jan 20 11:32 ../
-rwxr-----+ 1 root root   10 Jan 20 11:33 test.txt*

root@dlp:~#
getfacl -R /home/testdir

getfacl: Removing leading '/' from absolute path names
# file: home/testdir
# owner: root
# group: root
user::rwx
user:ubuntu:r--
group::---
mask::r--
other::---

# file: home/testdir/test.txt
# owner: root
# group: root
user::rwx
user:ubuntu:r--
group::r--
mask::r--
other::---
[6] グループ単位で ACL を設定する。
# /home/test.txt に対して、グループ "security" に rw(read/write) を許可

root@dlp:~#
setfacl -m g:security:rw /home/test.txt

root@dlp:~#
getfacl /home/test.txt

getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
group::r--
group:security:rw-
mask::rw-
other::r--

# "security" グループ所属の "ubuntu" ユーザーで確認

ubuntu@dlp:~$
echo "test write" >> /home/test.txt

ubuntu@dlp:~$
cat /home/test.txt

ACL test file
test write
# 正常に書き込めた
# "security" グループに属さない他ユーザーで確認

debian@dlp:~$
echo "test write" >> /home/test.txt

-bash: /home/test.txt: Permission denied    
# 正常に書き込めない
[7] ACL 設定を削除する。
# /home/test.txt のACLを全て削除

root@dlp:~#
setfacl -b /home/test.txt
# /home/test.txt に付与されている "ubuntu" ユーザーのACLのみ削除

root@dlp:~#
setfacl -x u:ubuntu /home/test.txt
[8] あるディレクトリに対してデフォルトの ACL を設定する。
デフォルト ACL が設定されたディレクトリ配下にファイル/ディレクトリを作成すると、デフォルト ACL が継承される。 ( デフォルト設定のみでなく、対象ディレクトリに対して個別の許可設定もしておく必要がある )
ただし、新規作成ファイルにはデフォルトで ACL が付与されるが、それらのファイルに後から chmod でアクセス権を変更すると ACL 設定はクリアされるため、注意が必要。
root@dlp:~#
setfacl -m u:ubuntu:r-x /home/testdir

# /home/testdir ディレクトリに対して "ubuntu" に r-x(read/execute) を許可するデフォルト ACL

root@dlp:~#
setfacl -d -m u:ubuntu:r-x /home/testdir

root@dlp:~#
getfacl /home/testdir

getfacl: Removing leading '/' from absolute path names
# file: home/testdir
# owner: root
# group: root
user::rwx
user:ubuntu:r-x
group::---
mask::r-x
other::---
default:user::rwx
default:user:ubuntu:r-x
default:group::---
default:mask::r-x
default:other::---
root@dlp:~#
echo "ACL default setting" > /home/testdir/test.txt

root@dlp:~#
ll /home/testdir/test.txt

-rw-r-----+ 1 root root 20 Jan 31 22:32 /home/testdir/test.txt

# "ubuntu" ユーザーで確認

ubuntu@dlp:~$
cat /home/testdir/test.txt

ACL default setting    
# 正常に読めた
[9] デフォルト ACL を削除する。
root@dlp:~#
setfacl -k /home/testdir

root@dlp:~#
getfacl /home/testdir

getfacl: Removing leading '/' from absolute path names
# file: home/testdir
# owner: root
# group: root
user::rwx
user:ubuntu:r-x
group::---
mask::r-x
other::---
[10] ファイルから ACL を読み込み設定する。
# ACL設定ファイルを作成 ( 書式は getfacl の出力結果と同じ )

# 設定したいACLが他システムにあるなら、getfaclの結果をリダイレクトでファイルに出力しそのまま利用することも可能

root@dlp:~#
vi acl.txt
# file: /home/testdir
# owner: root
# group: root
user::rwx
user:ubuntu:r-x
group::---
mask::r-x
other::---
# file: /home/test.txt
# owner: root
# group: root
user::rwx
user:ubuntu:r--
group::---
mask::r--
other::---
root@dlp:~#
setfacl --restore=acl.txt

root@dlp:~#
ll /home

total 16
drwx------. 2 ubuntu   ubuntu   4096 Jan 31 12:14 ubuntu
drwx------  2 fedora fedora     4096 Jan 31 12:14 fedora
drwxr-x---+ 2 root   root       4096 Jan 31 22:32 testdir
-rwxr-----+ 1 root   root         25 Jan 31 21:56 test.txt
関連コンテンツ