What command is used to change the group ownership of a file or directory

Format

chown [-fhR] owner[:group] pathname ...

Description

chown sets the user ID (UID) to owner for the files and directories that are named by pathname arguments. owner can be a user name from the user database, or it can be a numeric user ID. (If a numeric owner exists as a user name in the user database, the user ID number associated with that user name is used.) If there is no change to the UID, then specify - -  -1.

If you include a group name (that is, if you specify owner followed immediately by a colon (:) and then group with no intervening spaces, such as owner:group) chown also sets the group ID (GID) to group for the files and directories named. group can be a group name from the security facility group database, or it can be a numeric group ID. If a numeric group exists as a group name in the group database, the group ID number that is associated with that group is used. If there is no change to the GID, then specify -1 (or do not specify the :group).

Restriction: Only a superuser can change the UID. To change the GID, you must either be a superuser, or the effective user ID of the process must be equal to the user ID of the file owner, and the owner argument is also equal to the user ID of the file owner or -1, and the group argument is the calling process's effective group ID or one of its supplementary group IDs.

chown also turns off the set-user-ID bit and set-group-ID bit of the named files and directories.

For additional information related to chown usage, Using UNIXPRIV class profiles in z/OS UNIX System Services Planning.

Options

-fDoes not issue an error message if chown cannot change the owner. In this case, chown always returns a status of zero. Other errors may cause a nonzero return status.-hDoes not attempt to follow the symbolic link (or external link), but instead makes the changes on the symbolic link (or external link) itself.-RIf pathname on the command line is the name of a directory, chown changes all the files and subdirectories in that directory to belong to the specified owner (and group, if :group is specified).

If a symbolic link is specified or encountered during the traversal of a file hierarchy, chown changes the directory referenced by the symbolic link and all files in the file hierarchy under it.

If chown cannot change some file or subdirectory in the directory, it continues to try to change the other files and subdirectories in the directory, but exits with a nonzero status.

Localization

chown uses the following localization environment variables:

  • LANG
  • LC_ALL
  • LC_CTYPE
  • LC_MESSAGES
  • NLSPATH

Exit values

0You specified -f, or chown successfully changed the ownership of all the specified files and directories.1Failure due to any of the following reasons:
  • Inability to access a specified file.
  • Inability to change the owner of a specified file.
  • Inability to read the directory that contains the directory entry of the file.
  • An unrecoverable error was encountered when using the -R option.
2Failure due to any of the following reasons:
  • The command line contained an incorrect option.
  • The command line had too few arguments.
  • An owner was specified with a user ID that the system did not recognize.

Messages

function not implementedThis error might occur if the directory is under automount control.

Portability

POSIX.2, UNIX systems.

The -f and -h options are an extension of the POSIX standard.

chgrp, chmod

As we said, most of the time you can get by with the default security the system gives you. But there are always exceptions, particularly for system administrators. To take a simple example, suppose you are creating a directory under

# chown bin.bin sampsoft
1 for a new user. You have to create everything as root, but when you’re done you have to change the ownership to the user; otherwise, that user won’t be able to use the files! (Fortunately, if you use the adduser command discussed in in Chapter 5, it takes care of ownership for you.)

Similarly, there are certain utilities such as UUCP and News that have their own users. No one ever logs in as UUCP or News, but those users and groups must exist so that the utilities can do their job in a secure manner. In general, the last step when installing software is usually to change the owner, group, and permissions as the documentation tells you to do.

The chown command changes the owner of a file, and the chgrp command changes the group. On Linux, only root can use chown for changing ownership of a file, but any user can change the group to another group he belongs to.

So after installing some software named

# chown bin.bin sampsoft
2, you might change both the owner and the group to bin by executing:

# chown bin sampsoft 
# chgrp bin sampsoft

You could also do this in one step by using the dot notation:

# chown bin.bin sampsoft

The syntax for changing permissions is more complicated. The permissions can also be called the file’s “mode,” and the command that changes permissions is chmod. Let’s start our exploration of this command through a simple example; say you’ve written a neat program in Perl or Tcl named

# chown bin.bin sampsoft
3, and you want to be able to execute it. You would type the following command:

$ chmod +x header

The plus sign means “add a permission,” and the

# chown bin.bin sampsoft
4 indicates which permission to add.

If you want to remove execute permission, use a minus sign in place of a plus:

$ chmod -x header

This command assigns permissions to all levels—user, group, and other. Let’s say that you are secretly into software hoarding and don’t want anybody to use the command but yourself. (No, that’s too cruel; let’s say instead that you think the script is buggy and want to protect other people from hurting themselves until you’ve exercised it.) You can assign execute permission just to yourself through the command:

$ chmod u+x header

Whatever goes before the plus sign is the level of permission, and whatever goes after is the type of permission. User permission (for yourself) is u, group permission is

# chown bin.bin sampsoft
5, and other is
# chown bin.bin sampsoft
6. So, to assign permission to both yourself and the file’s group, enter:

$ chmod ug+x header

You can also assign multiple types of permissions:

$ chmod ug+rwx header

There are a few more shortcuts you can learn from the chmod manual page in order to impress someone looking over your shoulder, but they don’t offer any functionality besides what we’ve shown you.

As arcane as the syntax of the mode argument may seem, there’s another syntax that is even more complicated. We have to describe it though, for several reasons. First of all, there are several situations that cannot be covered by the syntax, called symbolic mode, that we’ve just shown. Second, people often use the other syntax, called absolute mode, in their documentation. Third, there are times you may actually find the absolute mode more convenient.

To understand absolute mode, you have to think in terms of bits and octal notation. Don’t worry, it’s not too hard. A typical mode contains three characters, corresponding to the three levels of permission (user, group, and other). These levels are illustrated in . Within each level, there are three bits corresponding to read, write, and execute permission.

What command is used to change the group ownership of a file or directory

Figure 4-3. Bits in absolute mode

Let’s say you want to give yourself read permission and no permission to anybody else. You want to specify just the bit represented by the number 400. So the chmod command would be:

$ chmod 400 header

To give read permission to everybody, choose the correct bit from each level: 400 for yourself, 40 for your group, and 4 for other. The full command is:

$ chmod 444 header

This is like using a mode

# chown bin.bin sampsoft
7, except that it simultaneously removes any write or execute permission. (To be precise, it’s just like a mode of
# chown bin.bin sampsoft
8, which we didn’t mention earlier. The equal sign means “assign these rights and no others.”)

To give read and execute permission to everybody, you have to add up the read and execute bits. 400 plus 100 is 500, for instance.

So the corresponding command is:

$ chmod 555 header

which is the same as

# chown bin.bin sampsoft
9. To give someone full access, you would specify that digit as a 7—the sum of 4, 2, and 1.

One final trick: how to set the default mode that is assigned to each file you create (with a text editor, the > redirection operator, and so on). You do so by executing a umask command, or putting one in your shell’s start-up file. This file could be called

$ chmod +x header
0,
$ chmod +x header
1, or something else depending on the shell you use (we’ll discuss startup files in the next section).

The umask command takes an argument like the absolute mode in chmod, but the meaning of the bits is inverted. You have to determine the access you want to grant for user, group, and other, and subtract each digit from 7. That gives you a three-digit mask.

For instance, say you want yourself to have all permissions (7), your group to have read and execute permissions (5), and others to have no permissions (0). Subtract each bit from 7 and you get 0 for yourself, 2 for your group, and 7 for other. So the command to put in your start-up file is:

# chown bin.bin sampsoft
0

A strange technique, but it works. The chmod command looks at the mask when it interprets your mode; for instance, if you assign execute mode to a file at creation time, it will assign execute permission for you and your group, but will exclude others because the mask doesn’t permit them to have any access.

How to change ownership of all files in a directory in Linux?

The chown command is used to change the file owner or group. Syntax: chown [ OPTION ] [OWNER][:[GROUP]] FILE

What command is used to change file ownership on Linux systems?

The chown command changes user ownership of a file, directory, or link in Linux. Every file is associated with an owning user or group.

Which of the following commands can be used to change group ownership of a field directory?

The chgrp command can be abbreviated as change group. You can change the group owner of the file using chgrp command. Syntax: chgrp

What is the command for change ownership and permissions?

You can change the ownership of a file or folder using the chown command. In some cases, changing ownership requires sudo permissions.