LoginSignup
1
0

More than 1 year has passed since last update.

シンボリックリンクの所有者・パーミッションを設定する

Last updated at Posted at 2022-10-28

シンボリックリンク自体の権限設定について気にする必要があったのでメモ。

TL; DR

所有者は chown -h で変える。
パーミッションは仕様上変えられない。変えることが必要な場面は無いはず。

シンボリックリンクの所有者の変更

普通に chown で変えてみる。

$ ls -l ..
total 0
drwxr-xr-x. 2 user1 group1 21 Oct 28 11:28 test1
drwxr-xr-x. 2 user1 group1  6 Oct 28 11:28 test2
$ ls -l
total 0
lrwxrwxrwx. 1 user1 group1 8 Oct 28 11:28 symlink -> ../test2
$ sudo chown user2:group2 symlink
$ ls -l
total 0
lrwxrwxrwx. 1 user1 group1 8 Oct 28 11:28 symlink -> ../test2

変わってないやん。

$ ls -l ..
total 0
drwxr-xr-x. 2 user1 group1 21 Oct 28 11:28 test1
drwxr-xr-x. 2 user2 group2  6 Oct 28 11:28 test2

リンク先の ../test2 の所有者が変わったようです。
なんか、シンボリックリンク自体の所有者を変えたい場合は -h オプションが必要とのこと。

$ sudo chown -h user2:group2 symlink/
$ ls -l
total 0
lrwxrwxrwx. 1 user1 group1 8 Oct 28 11:28 symlink -> ../test2

変わってないやん。
ディレクトリの最後の / を書いちゃいけないらしい。きびしい。

$ sudo chown -h user2:group2 symlink
$ ls -l
total 0
lrwxrwxrwx. 1 user2 group2 8 Oct 28 11:28 symlink -> ../test2

シンボリックリンクのパーミッションの変更

ということはこうかな?

$ sudo chmod -h 755 symlink
chmod: invalid option -- 'h'
Try 'chmod --help' for more information.

chmod には -h ないんかい。
man chmod より

       chmod  never changes the permissions of symbolic links; the chmod system
       call cannot change their permissions.  This is not a problem  since  the
       permissions  of  symbolic  links are never used.  However, for each sym‐
       bolic link listed on the command line, chmod changes the permissions  of
       the  pointed-to file.  In contrast, chmod ignores symbolic links encoun‐
       tered during recursive directory traversals.

シンボリックリンクのパーミッションというのは使用されることはなく変更の必要はない。 chmod コマンドはリンク先のパーミッションを変更する。とのこと

なんで気にする必要があったのか

apache の設定ファイルで以下のようにするとシンボリックリンクを辿ってくれるようになる。

<Directory "/">
  Options FollowSymLinks 
</Directory>

下のようにすると、シンボリックリンク自体の所有者とリンク先の所有者が一致するときのみシンボリックリンクを辿る。こちらのほうがより制限された設定。

<Directory "/">
  Options SymLinksIfOwnerMatch
</Directory>

これが設定されていてシンボリックリンクを使いたかったのです。
これはパーミッションは関係ないけどパーミッションも合わせないといけないのかと思って chmod -h なんてコマンドを創り出してしまった。

私の事象ではページは開けるのにCSSが適用されていないって感じでした。
つまりこの設定は直接参照した場合であって、PHPがシンボリック越しにファイル読み込もうとした場合は特に問題ない模様。これも気をつけないといけないですな。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0