OpenBSD manual page server

Manual Page Search Parameters

CHMOD(1) General Commands Manual CHMOD(1)

chmodchange file modes

chmod [-h] [-R [-H | -L | -P]] mode file ...

The chmod utility modifies the file mode bits of the listed files as specified by the mode operand. The mode of a file dictates its permissions, among other attributes.

The options are as follows:

If the -R option is also specified, symbolic links on the command line are followed. Symbolic links encountered in the tree traversal are not followed.
Treat symbolic links like other files: modify links instead of following them. The -h and -R options are mutually exclusive.
If the -R option is also specified, all symbolic links are followed.
If the -R option is also specified, no symbolic links are followed.
Recurse. Where file is a directory, change the mode of the directory and all the files and directories in the file hierarchy below it.

Symbolic links have modes, but those modes have no effect on the kernel's access checks. The -H, -L, and -P options are ignored unless the -R option is specified; if none of them are given, the default is to not follow symbolic links. In addition, these options override each other and the command's actions are determined by the last one specified.

Only the file's owner or the superuser is permitted to change the mode of a file.

Absolute modes are specified according to the following format:

chmod nnnn file ...

An absolute mode is an octal number (specified as nnnn, where n is a number from 0 to 7) constructed by ORing any of the following values:

Allow read by owner.
Allow write by owner.
Allow execution (or search in directories) by owner.
Allow read, write, and execute/search by owner.
Allow read by group.
Allow write by group.
Allow execution (or search in directories) by group.
Allow read, write, and execute/search by group.
Allow read by others.
Allow write by others.
Allow execution (or search in directories) by others.
Allow read, write, and execute/search by others.

In addition to the file permission modes, the following mode bits are available:

Set-user-ID on execution.
Set-group-ID on execution.
Enable sticky bit; see sticky(8) and chmod(2).

The execute bit for a directory is often referred to as the “search” bit. In order to access a file, a user must have execute permission in each directory leading up to it in the filesystem hierarchy. For example, to access the file /bin/ls, execute permission is needed on /, /bin, and, of course, the ls binary itself.

Symbolic modes are specified according to the following format:

chmod [who]op[perm],... file ...

The who symbols indicate whose permissions are to be changed or assigned:

u
User (owner) permissions.
g
Group permissions.
o
Others permissions.
a
All of the above.

Do not confuse the ‘o’ symbol with “owner”. It is the user bit, ‘u’, that refers to the owner of the file.

The op symbols represent the operation performed, as follows:

+
If no value is supplied for perm, the ‘+’ operation has no effect. If no value is supplied for who, each permission bit specified in perm, for which the corresponding bit in the file mode creation mask is clear, is set. Otherwise, the mode bits represented by the specified who and perm values are set.
-
If no value is supplied for perm, the ‘-’ operation has no effect. If no value is supplied for who, each permission bit specified in perm, for which the corresponding bit in the file mode creation mask is clear, is cleared. Otherwise, the mode bits represented by the specified who and perm values are cleared.
=
The mode bits specified by the who value are cleared or, if no who value is specified, the user, group and other mode bits are cleared. Then, if no value is supplied for who, each permission bit specified in perm, for which the corresponding bit in the file mode creation mask is clear, is set. Otherwise, the mode bits represented by the specified who and perm values are set.

The perm (permission symbols) represent the portions of the mode bits as follows:

r
Read bits.
s
Set-user-ID and set-group-ID on execution bits.
t
Sticky bit.
w
Write bits.
x
Execute/search bits.
X
The execute/search bits if the file is a directory or any of the execute/search bits are set in the original (unmodified) mode.
u
User permission bits in the mode of the original file.
g
Group permission bits in the mode of the original file.
o
Other permission bits in the mode of the original file.

Each clause (given in a comma-delimited list on the command line) specifies one or more operations to be performed on the mode bits, and each operation is applied in the order specified.

Operations upon the “other” permissions (specified by the symbol ‘o’ by itself), in combination with the perm symbols ‘s’ or ‘t’, are ignored.

The chmod utility exits 0 on success, and >0 if an error occurs.

Set file readable by anyone and writable by the owner only:

$ chmod 644 file

Deny write permission to group and others:

$ chmod go-w file

Set the read and write permissions to the usual defaults, but retain any execute permissions that are currently set:

$ chmod =rwX file

Make a directory or file searchable/executable by everyone if it is already searchable/executable by anyone:

$ chmod +X file

Any of these commands will make a file readable/executable by everyone and writable by the owner only:

$ chmod 755 file
$ chmod u=rwx,go=rx file
$ chmod u=rwx,go=u-w file

Clear all mode bits for group and others:

$ chmod go= file

Set the group bits equal to the user bits, but clear the group write bit:

$ chmod g=u-w file

chflags(1), chgrp(1), find(1), install(1), chmod(2), stat(2), umask(2), fts_open(3), setmode(3), symlink(7), chown(8), sticky(8)

The chmod utility is compliant with the specification.

The flags [-HhLP] are extensions to that specification.

The ‘t’ perm symbol (sticky bit) is marked by as being an X/Open System Interfaces option.

A chmod command appeared in Version 1 AT&T UNIX.

There's no perm option for the naughty bits.

August 1, 2024 OpenBSD-current