chmod コマンドを使用して、ファイルやディレクトリのパーミッションを変更できます。chmod コマンドは、符号(記号モード)または数字(数値モード)で権限を設定できます。
chmod [オプション] モード ファイル名
主なオプション
オプション | 説明 | 使用例 |
---|---|---|
-R | ディレクトリとその中のファイルすべてに再帰的に適用 | chmod -R 755 /var/www |
-v | 処理されたファイルごとに診断結果を出力する | chmod -v 644 file.txt |
-c | 変更があった場合のみ表示 | chmod -c 600 file.txt |
-f | エラーを表示しない(静かに実行) | chmod -f 777 file.txt |
--reference=FILE | 指定したファイルと同じ権限に変更 | chmod –reference=example.txt file.txt |
記号モード
記号 | 説明 |
---|---|
u | ユーザー/所有者 |
g | グループ |
o | その他 |
a | 全ユーザー |
パーミッションの変更例
現在のtestファイルのパーミッションの確認。「rw-r--r--
」となっている。
ls -l ~/test-dir/test

所有者に実行権限を追加
chmod u+x ~/test-dir/test

実行権限 -rw「x」r–r–. が付与されていることを確認
ls -l ~/test-dir/test

グループとその他のユーザーから読み取り権限を削除
読み取り権限を削除
chmod go-r ~/test-dir/test

グループとその他のユーザーから読み取り権限を削除されたことが確認できます。
ls -l ~/test-dir/test

全ユーザーに読み取り権限を付与し、書き込みと実行権限を削除
パーミッションの変更
chmod a=r ~/test-dir/test

全ユーザーの権限が読み取りのみになっていることが確認できます。
ls -l ~/test-dir/test

数値モード
数値モードでは権限は数字で指定します。読み取り(4)、書き込み(2)、実行(1)の合計で設定します。
ディレクトリとその配下のパーミッションを再帰的に変更
test-dirとその配下のパーミッションを744に変更します
sudo chmod -R 744 ~/test-dir/

test-dirとその配下のtestファイルのパーミッションが744になっていることが確認できます。
ls -l ~ | grep test-dir ls -l ~/test-dir/test

コメント