Understanding Linux File Ownership

When configuring your dedicated server’s file permissions, it is important not to overlook file ownership. In Linux and other Unix-like operating systems, every file is assigned an owner, and that owner belongs to a group. A file can have generally liberal permissions but only for a particular user or group. Similarly, a file can have rather restrictive permissions but be completely accessible to a particular user or group.

The easiest way to find out who owns a file is to list it and all of its settings, using the “ls” command.

$ ls -al filename

The output will appear as follows:

-rw-r–r– 1 root root 346 2010-08-05 20:38 filename

In this example, the permissions are listed first, followed by the user, and the group. The user Root from the group Root owns this file.

To change file ownership, use the “chown” command. For example, to pass ownership of a file to the user Azizah in the group Users, you would type as root:

# chown azizah:users filename

To assign a directory and all its contents, use the “-R” flag:

# chown -R azizah:users directory

Users can belong to more than one group, so it is possible for ownership to be shared between members of a group, as long as the file permissions allow group access for read, write, and/or execute.

When dealing with your web server, some files may need to be owned by the web server itself. The username and group may be apache:apache. If you get script errors indicating a file permission problem, especially if the script needs to modify files, you may need to change ownership of a file to Apache or the appropriate user and group.