chmod


The chmod command is a fundamental utility in RHEL and other Unix-like operating systems. It stands for "change mode" and is used to modify the access permissions of files and directories. These permissions determine who can read, write, or execute a file.


Example 1: Assigning specific permissions using octal notation

# This command sets the permissions for 'myfile.txt'
# Owner gets read, write, and execute permissions (7)
# Group gets read and execute permissions (5)
# Others get only read permission (4)
chmod 754 myfile.txt

Explanation: This example uses numeric (octal) notation to set file permissions. Each digit corresponds to a user category (owner, group, other), and the number itself is the sum of the values for read (4), write (2), and execute (1).


Example 2: Adding execute permission for the owner

# Adds execute permission for the user (owner) of the script
chmod u+x myscript.sh

Explanation: This command utilizes symbolic notation to modify permissions. The u+x argument specifies that the execute (x) permission should be added (+) for the user (u) who owns the file, without altering other existing permissions.


Example 3: Removing write permission for group and others

# This command removes write permission for the group and others
chmod go-w sensitive_data.doc

Explanation: Here, go-w tells the system to remove (-) the write (w) permission for the group (g) and others (o). This is a common way to secure a file from being modified by unintended users.


Example 4: Recursively changing permissions for a directory

# The -R option applies the permissions recursively
# This sets read and write for the owner, and read-only for group and others
# on the 'project_files' directory and all its contents.
chmod -R 644 project_files

Explanation: The -R (or --recursive) option is powerful for applying permission changes to a directory and all the files and subdirectories within it. It ensures a consistent permission scheme throughout a directory tree.


Example 5: Setting permissions exactly for all categories

# This command sets the permissions for 'config.cfg' precisely.
# Owner gets read and write (rw-).
# Group gets read-only (r--).
# Others have no permissions (---).
chmod u=rw,g=r,o= config.cfg

Explanation: Using the = operator with symbolic notation sets the permissions exactly as specified, overwriting any previous permissions. In this case, o= removes all permissions for "others".


Example 6: Copying permissions from another file

# This command uses the --reference option to apply the permissions
# of 'template.sh' to 'new_script.sh'.
chmod --reference=template.sh new_script.sh

Explanation: The --reference flag is a convenient way to clone permissions from one file to another. This ensures that a new file has the exact same access rights as a reference file, promoting consistency.


Example 7: Using Special Permissions - SetGID on a directory

# This sets the SetGID bit on the 'shared_docs' directory.
# '2' represents the SetGID bit.
# '775' sets standard read/write/execute permissions for owner/group.
chmod 2775 shared_docs

Explanation: When the SetGID bit is applied to a directory, any new files created within it will inherit the group ownership of the directory itself, not the primary group of the user who created the file. This is useful for collaborative directories.


Example 8: Using Special Permissions - The Sticky Bit

# The '1' at the beginning represents the sticky bit.
# This is often used on /tmp directories.
# It allows users to write but only allows the file owner to delete their own files.
chmod 1777 /tmp/shared_space

Explanation: The sticky bit, represented by the first digit 1 in octal mode or t symbolically, is a security feature for directories. When set, it restricts file deletion within that directory, allowing only the file's owner (and root) to remove it.

In older UNIX systems like Solaris, the commands are largely the same. However, a non-RHEL system like the classic Mac OS (pre-OS X) had a completely different graphical method for changing permissions and lacked a command-line chmod in the same way. In FreeBSD, a popular UNIX-like OS, the chmod command and its options are virtually identical to RHEL 8.