chown

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

chown, fchown, lchown, fchownat - ファイルの所有者を変更する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <unistd.h>

int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);

#include <fcntl.h>           /* AT_* 定数の定義 */
#include <unistd.h>

int fchownat(int dirfd, const char *path,
             uid_t owner, gid_t group, int flags);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

fchown(), lchown():


    /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
        || _XOPEN_SOURCE >= 500
        || /* glibc <= 2.19: */ _BSD_SOURCE

fchownat():

    glibc 2.10 以降:
        _POSIX_C_SOURCE >= 200809L
    glibc 2.10 以前:
        _ATFILE_SOURCE
 

説明

これらのシステムコールは、いずれもファイルの所有者 (owner) とグループを変更する。システムコール chown(), fchown(), lchown() 間の違いは、ファイルの指定の仕方だけである。
•
chown() changes the ownership of the file specified by path, which is dereferenced if it is a symbolic link.
•
fchown() はオープンされたファイルディスクリプター fd により参照されるファイルの所有権を変更する。
•
lchown() は chown() と同じだが、シンボリックリンクを展開しない点が異なる。

特権を持つプロセス (Linux では CAP_CHOWN ケーパビリティ (capability) を持つプロセス) だけが ファイルの所有者を変更できる。 ファイルの所有者は、その所有者が属しているグループのいずれかに ファイルのグループを変更することができる。 特権 (Linux では CAP_CHOWN) を持つプロセスは、任意のグループに変更できる。

owner または group に -1 が指定された場合、それらの ID は変更されない。

When the owner or group of an executable file is changed by an unprivileged user, the S_ISUID and S_ISGID mode bits are cleared. POSIX does not specify whether this also should happen when root does the chown(); the Linux behavior depends on the kernel version, and since Linux 2.2.13, root is treated like other users. In case of a non-group-executable file (i.e., one for which the S_IXGRP bit is not set) the S_ISGID bit indicates mandatory locking, and is not cleared by a chown().

When the owner or group of an executable file is changed (by any user), all capability sets for the file are cleared.  

fchownat()

fchownat() システムコールは chown() と全く同様に動作するが、以下で説明する点が異なる。

If path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by chown() for a relative pathname).

If path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like chown()).

path で指定されたパス名が絶対パスの場合、 dirfd は無視される。

flags 引数は、以下に示す値の 0 個以上の OR (論理和) をとって作成される ビットマスクである。

AT_EMPTY_PATH (Linux 2.6.39 以降)
If path is an empty string, operate on the file referred to by dirfd (which may have been obtained using the open(2) O_PATH flag). In this case, dirfd can refer to any type of file, not just a directory. If dirfd is AT_FDCWD, the call operates on the current working directory. This flag is Linux-specific; define _GNU_SOURCE to obtain its definition.
AT_SYMLINK_NOFOLLOW
If path is a symbolic link, do not dereference it: instead operate on the link itself, like lchown(). (By default, fchownat() dereferences symbolic links, like chown().)

fchownat() の必要性についての説明については openat(2) を参照。  

返り値

成功すると、0 を返す。 エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

ファイルシステムによっては、下記の一覧にない他のエラーが返されることもある。

chown() でごく一般的なエラーを以下に挙げる:

EACCES
パス名の構成要素に検索許可がない (path_resolution(7) も見よ)。
EBADF
(fchown()) fd is not a valid open file descriptor.
EBADF
(fchownat()) path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor.
EFAULT
path がアクセスできるアドレス空間外を指している。
EINVAL
(fchownat()) Invalid flag specified in flags.
EIO
(fchown()) A low-level I/O error occurred while modifying the inode.
ELOOP
path を解決する際に遭遇したシンボリックリンクが多過ぎる。
ENAMETOOLONG
path が長過ぎる。
ENOENT
ファイルが存在しない。
ENOMEM
カーネルに十分なメモリーがない。
ENOTDIR
パス名の構成要素がディレクトリではない。
ENOTDIR
(fchownat()) path is relative and dirfd is a file descriptor referring to a file other than a directory.
EPERM
呼び出したプロセスに所有者またはグループ (もしくはその両方) を変更するために 要求される許可 (上記を参照) がない。
EPERM
The file is marked immutable or append-only. (See FS_IOC_SETFLAGS(2const).)
EROFS
ファイルが読み込み専用 (read only) のファイルシステム上にある。
 

バージョン

4.4BSD 版ではスーパーユーザーのみが使用できる (つまり、普通のユーザーはファイルを手放すことはできない)。  

標準規格

POSIX.1-2024.  

歴史

chown()
SVr4, POSIX.1-1988, 4.4BSD.
fchown()
SVr4, 4.4BSD, SUSv1, POSIX.1-2001.
lchown()
SVr4, 4.4BSD, SUSv1, POSIX.1-2001 XSI, POSIX.1-2008.
fchownat()
POSIX.1-2008. Linux 2.6.16, glibc 2.4.
 

注意

 

新しいファイルの所有権

(open(2) や mkdir(2) などにより) 新しいファイルが作成されるとき、 その所有者は呼び出したプロセスのファイルシステムユーザー ID と 同じに設定される。 そのファイルのグループはいくつかの要因により決定される。 その要因としては、 ファイルシステムの種類、そのファイルシステムのマウント時に 使用されたオプション、親ディレクトリで set-group-ID モードビットが 有効になっているどうか、がある。 ファイルシステムが mount(8) オプションの -o grpid (-o bsdgroups も同義語) と -o nogrpid (-o sysvgroups も同義語) に対応している場合、ルールは以下の通りとなる。
•
ファイルシステムが -o grpid 付きでマウントされている場合、新しいファイルのグループは 親ディレクトリのグループと同じになる。
•
ファイルシステムが -o nogrpid 付きでマウントされており、親ディレクトリでは set-group-ID ビットが 無効になっている場合、新しいファイルのグループは プロセスのファイルシステム GID と同じになる。
•
ファイルシステムが -o nogrpid 付きでマウントされており、親ディレクトリでは set-group-ID ビットが 有効になっている場合、新しいファイルのグループは 親ディレクトリのグループと同じになる。

Linux 4.12 の時点では、マウントオプション -o grpid と -o nogrpid に対応しているファイルシステムは ext2, ext3, ext4, XFS である。 これらのマウントオプションに対応していないファイルシステムでは、 -o nogrpid に関するルールが適用される。  

glibc での注意

On older kernels where fchownat() is unavailable, the glibc wrapper function falls back to the use of chown() and lchown(). When path is relative, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument.  

NFS

chown() 方式は UID マッピングを使用した NFS ファイルシステムを侵害する。 さらにファイルの内容にアクセスする全てのシステムコールを侵害する。 これは chown() が既にオープンされたファイルに対する アクセスをただちに取り消すことによる。 クライアント側のキャッシュにより所有権が変更されて ユーザーのアクセスが許した時点と、実際に他のクライアントでユーザーによって ファイルにアクセスできる時点との間に時間差があるかもしれない。  

歴史的な詳細

元々の Linux の chown(), fchown(), lchown() システムコールは、 16 ビットのユーザー ID とグループ ID だけに対応していた。 その後、 32 ビットの ID に対応した chown32(), fchown32(), lchown32() が Linux 2.4 で追加された。 chown(), fchown(), and lchown() の glibc のラッパー関数は、 カーネルのバージョンによる違いを吸収している。

Before Linux 2.1.81 (except 2.1.46), chown() did not follow symbolic links. Since Linux 2.1.81, chown() does follow symbolic links, and there is a new system call lchown() that does not follow symbolic links. Since Linux 2.1.86, this new call (that has the same semantics as the old chown()) has got the same syscall number, and chown() got the newly introduced number.  

例

以下のプログラムは、 二つ目のコマンドライン引数で指定された名前のファイルの所有者を、 一つ目のコマンドライン引数で指定された値に変更する。 新しい所有者は、数字のユーザー ID かユーザー名のいずれかで指定できる (ユーザー名で指定した場合には、 getpwnam(3) を使ってシステムのパスワードファイルの検索が行われ、 ユーザー ID への変換が行われる)。  

プログラムのソース

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    char           *endptr;
    uid_t          uid;
    struct passwd  *pwd;

    if (argc != 3 || argv[1][0] == '\0') {
        fprintf(stderr, "%s <owner> <file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    uid = strtol(argv[1], &endptr, 10);  /* Allow a numeric string */

    if (*endptr != '\0') {         /* Was not pure numeric string */
        pwd = getpwnam(argv[1]);   /* Try getting UID for username */
        if (pwd == NULL) {
            perror("getpwnam");
            exit(EXIT_FAILURE);
        }

        uid = pwd->pw_uid;
    }

    if (chown(argv[2], uid, -1) == -1) {
        perror("chown");
        exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS);
}
 

関連項目

chgrp(1), chown(1), chmod(2), flock(2), path_resolution(7), symlink(7)


 

Index

名前
ライブラリ
書式
説明
fchownat()
返り値
エラー
バージョン
標準規格
歴史
注意
新しいファイルの所有権
glibc での注意
NFS
歴史的な詳細
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.