Chmod & Chown command in Linux

Chmod command & Chown command are two essential commands in Linux used to manage file and directory permissions and change file ownership, respectively.

1. Chmod (Change Mode)

The chmod command is used to change the permissions (read, write, execute) of files and directories. It allows you to control who can access and modify them. File permissions are represented by a three-character string for the owner, group, and others. Each character indicates whether read (r), write (w), and execute (x) permissions are granted or not.

The syntax of the chmod command is as follows:

chmod [options] mode file(s)/directory(s)
  • mode: The permission mode you want to set using symbolic or numeric notation (explained in the previous answer).
  • file(s)/directory(s): The file(s) or directory(ies) for which you want to change the permissions.

Examples:

a) Grant read and write permissions to the owner of the file example.txt:

chmod u+rw example.txt

b) Set read, write, and execute permissions for all (user, group, and others) on a directory my_directory:

chmod a+rx my_directory

c) Remove write and execute permissions from the group and others for the script myscript.sh:

chmod go-wx myscript.sh

2. Chown (Change Owner)

The chown command is used to change the ownership of files and directories. It allows you to assign a new owner and group to one or multiple files and directories.

The syntax of the chown command is as follows:

chown [options] new_owner[:new_group] file(s)/directory(s)
  • new_owner: The new owner (either a username or a numeric user ID) to be assigned to the file or directory.
  • new_group: (Optional) The new group (either a group name or a numeric group ID) to be assigned to the file or directory. If not specified, the group will remain unchanged.
  • file(s)/directory(s): The file(s) or directory(ies) for which you want to change the ownership.

Examples:

a) Change the owner of the file example.txt to a user named john:

chown john example.txt

b) Change the owner and group of a directory my_directory to john and users, respectively:

chown john:users my_directory

c) Change the group of a file data.txt to a group with a numeric ID 1000:

chown :1000 data.txt

Both chmod and chown commands in linux are powerful tools for managing file and directory permissions and ownership on a Linux system. Always use them with caution and ensure you understand the implications of the changes you are making. Incorrect use of these commands can potentially lead to security risks or unwanted behavior in the system.

3 thoughts on “Chmod & Chown command in Linux”

  1. Pingback: Linux Permissions for Files & Directory - Info Influx

  2. Pingback: RMAN Backup using ShellScript (Automate) - Info Influx

  3. Pingback: Install Oracle 19c on Linux - Info Influx

Leave a Comment

Your email address will not be published. Required fields are marked *