This page covers commands for managing file permissions and ownership, crucial for system security.
16. chmod - Change File Permissions
Purpose: The chmod command (change mode) is used to change the permissions (read, write, execute) of files and directories.
Permissions are defined for three categories: user (owner), group, and others.
Syntax: chmod [OPTIONS] MODE FILE...
Permission Modes (Octal):
- Read (r): 4
- Write (w): 2
- Execute (x): 1
- No permission: 0
Combine numbers for desired permissions (e.g., 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--).
Permission Modes (Symbolic):
u: user (owner)g: groupo: othersa: all (user, group, others)+: add permission-: remove permission=: set exact permissionr,w,x: read, write, execute
Examples:
chmod 755 script.sh # rwx for user, r-x for group and others chmod u+x my_program # Add execute permission for the owner chmod go-w shared_file # Remove write permission for group and others chmod 644 document.txt # rw- for user, r-- for group and others
17. chown - Change File Ownership
Purpose: The chown command (change owner) is used to change the owner of files and directories. This typically requires root privileges.
Syntax: chown [OPTIONS] USER[:GROUP] FILE...
Key Options:
-R: (recursive) Changes ownership recursively for directories and their contents.
Examples:
sudo chown newuser file.txt sudo chown newuser:newgroup folder/ sudo chown -R webadmin:webgroup /var/www/html/
18. chgrp - Change File Group Ownership
Purpose: The chgrp command (change group) is used to change the group owner of files and directories. You must be the owner of the file or have root privileges.
Syntax: chgrp [OPTIONS] GROUP FILE...
Key Options:
-R: (recursive) Changes group ownership recursively.
Examples:
chgrp developers project_file.py sudo chgrp -R www-data /var/www/
19. whoami - Display Current Username
Purpose: The whoami command prints the effective username of the current user. It's a quick way to verify which user account you are currently operating as.
Syntax: whoami
Example:
whoami
Output might be your_username or root if you're logged in as root.
20. id - Print User and Group Information
Purpose: The id command displays detailed information about the current user, including their user ID (UID), primary group ID (GID), and all supplementary group memberships.
Syntax: id [USERNAME]
Examples:
id id johndoe
Output includes UID, GID, and groups the user belongs to.