0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Linux】chownコマンドについて(所有者/所有グループの変更)

Last updated at Posted at 2022-08-31

ファイルの所有者/所有グループの変更について

chownコマンドを実施する事で所有者/所有グループの変更を行う事が出来ます。
構文は、「chown 所有者:所有グループ ファイル名」となります。
所有者のみ変更を行いたい場合は、「chown 所有者 ファイル名」で変更を行う事が可能です。

グループのみ変更を行いたい場合は、chgrpコマンドを使用します。構文は、「chgrp グループ名 ファイル名」です。あまり、chgrpコマンドは使う機会がないのかなと思います。(自分は業務で使った事がないです。)

所有グループの変更については、chownコマンドでも変更出来ます。
「chown :所有グループ ファイル名」で行えます。所有グループの前には:(コロン)をつけます。

再帰的に所有者等の変更を行いたい場合は、-Rオプションを使用する事で行えます。

実際にやってみた

以下ファイルの所有者/所有グループの変更を行いたいと思います。
現状はパーミッションが700となっており、所有者以外は 読み込み/実行 等が行えないような設定です。所有者は test_01ユーザー となっております。

今回は 所有者/所有グループ を test_02 に変更して、読み込み/実行を行うまでを目標とします。

ファイルの所有者確認など
[test_02@localhost tmp]$ ll
合計 8
-rwx------. 1 test_01 test_01 362  8月 30 07:37 memo.txt
-rwx------. 1 test_01 test_01   4  8月 30 07:04 test.sh
[test_02@localhost tmp]$ cat memo.txt
cat: memo.txt: 許可がありません
[test_02@localhost tmp]$ /tmp/test.sh
-bash: /tmp/test.sh: 許可がありません

変更を行う為に、特権ユーザーにスイッチします。

特権ユーザーへスイッチ
[test_02@localhost tmp]$ su -
[root@localhost ~]#

chownコマンドを実施し、所有者/所有グループを変更します。

所有者/所有グループを変更
[root@localhost ~]# chown test_02:test_02 /tmp/memo.txt
[root@localhost ~]# chown test_02:test_02 /tmp/test.sh
[root@localhost ~]# ll /tmp
合計 8
-rwx------. 1 test_02 test_02 362  8月 30 07:37 memo.txt
-rwx------. 1 test_02 test_02   4  8月 30 07:04 test.sh

test_02ユーザーにスイッチして、読み込み/実行を行ってみます。

実行
[test_02@localhost ~]$ cat /tmp/memo.txt
合計 8
-rwx------. 1 test_02 test_02 362  8月 30 07:37 memo.txt
-rwx------. 1 test_02 test_02   4  8月 30 07:04 test.sh
[test_02@localhost ~]$ /tmp/test.sh
Hello World!

無事実行出来ました。

以上、何かの参考になれば幸いです。

0
0
3

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?