chmod command

Kobie is a RHEL System Administrator at Google. He is studying for his LFCS exam. He wants to master the chmod command. Please give and explain 5 detailed examples that will help him to master the command.

Of course, I'd be happy to help Kobie master the chmod command! chmod is used to change the permissions of files and directories in Linux. It's an important command for system administrators to understand. I'll provide five detailed examples to help Kobie learn how to use it effectively:

  1. Basic Permission Changes: Suppose Kobie has a file named "important.txt" and he wants to give read, write, and execute permissions to the owner, and only read permissions to the group and others.

    chmod u=rwx,g=r,o=r important.txt

    Explanation:

    • u=rwx: Sets read, write, and execute permissions for the owner.
    • g=r: Sets read permission for the group.
    • o=r: Sets read permission for others.
  2. Numeric Representation of Permissions: To use numeric representation, Kobie can assign values to permission types: read (4), write (2), and execute (1). He can then sum up the values to set the permissions.

    chmod 754 important.txt

    Explanation:

    • 7 (owner): 4 (read) + 2 (write) + 1 (execute)
    • 5 (group): 4 (read) + 1 (execute)
    • 4 (others): 4 (read)
  3. Recursively Change Directory Permissions: Kobie might need to change permissions for directories and their contents recursively. Let's say he wants to give read, write, and execute permissions to the owner, and read and execute permissions to the group and others for a directory "docs."

    chmod -R u=rwx,g=rx,o=rx docs/

    Explanation:

    • -R: Recursively changes permissions for the directory and its contents.
  4. Using Symbolic Notation to Add/Remove Permissions: If Kobie wants to add or remove specific permissions without affecting the others, he can use the + and - symbols.

    chmod g+w,o-x important.txt

    Explanation:

    • g+w: Adds write permission for the group.
    • o-x: Removes execute permission for others.
  5. Changing Permissions for Multiple Files: If Kobie wants to change permissions for multiple files at once, he can use wildcard characters, like * for all files or *.txt for all text files.

    chmod u=rw,g=r,o=r *.txt

    Explanation:

    • This command sets read and write permissions for the owner and read permission for the group and others for all .txt files in the current directory.

Remember, practicing these examples on a test system or in a controlled environment is a great way for Kobie to master the chmod command. It's important to be cautious when changing permissions on important system files to avoid unintended consequences.

You should also read: