Raspberry Pi OS (4) - Permissions and ownership

Permissions and ownership

File Permissions and Ownership in Linux

Linux uses a permission-based system to control who can read, write, or execute files and directories. Every file or directory in Linux has an owner and is assigned a set of permissions for three categories of users:

  • Owner: The user who owns the file.
  • Group: A set of users who share similar permissions on the file.
  • Others: All other users who are not the owner or part of the group.

Permissions can be set for each of these categories for three types of access:

  • Read (r): Permission to read or view the contents of the file or directory.
  • Write (w): Permission to modify or write to the file or directory.
  • Execute (x): Permission to execute the file (if it’s a program) or access files inside the directory.

 Commands for Managing Users, Permissions, and Ownership

1. ls -l: Listing Permissions

The ls -l command is used to display detailed information about files, including their permissions, ownership, and modification time. The output shows file permissions in the format: 

drwxr-xr--

 This string represents:

  • First character: Type of file (d for directory, - for regular file).
  • Next three characters: Permissions for the owner.
  • Next three characters: Permissions for the group.
  • Final three characters: Permissions for others.

2. chmod: Change File Permissions

The chmod command is used to change the permissions of files and directories. You can set permissions using symbolic notation (r, w, x) or numeric notation.

Symbolic Mode:

  • chmod u+r file.txt: Adds read permission for the owner.
  • chmod g-w file.txt: Removes write permission for the group.
  • chmod o+x file.txt: Adds execute permission for others.
  • chmod a=rwx file.txt: Gives all users (owner, group, others) read, write, and execute permissions.

Numeric Mode: Permissions can be represented by numbers: r = 4, w = 2, x = 1. To set permissions, add these values together:

  • chmod 755 file.txt: Assigns rwx to the owner (7), r-x to the group (5), and r-x to others (5).
  • chmod 644 file.txt: Assigns rw- to the owner (6), r-- to the group (4), and r-- to others (4).

3. chown: Change File Ownership

The chown command changes the ownership of a file or directory. The syntax is:

  • chown user file.txt: Changes the owner of file.txt to the specified user.
  • chown user:group file.txt: Changes both the owner and group of file.txt.
  • chown :group file.txt: Changes only the group.

Example:

  • chown alice file.txt: Sets "alice" as the owner of the file.
  • chown alice:developers file.txt: Sets "alice" as the owner and "developers" as the group.

4. useradd: Add a New User

The useradd command is used to create a new user in the system.

  • useradd username: Creates a new user.
  • useradd -m username: Creates a new user and a home directory for them.
  • useradd -G groupname username: Adds the user to a specific group.

After creating a user, set their password with:

  • passwd username

5. id: Display User and Group IDs

The id command shows the user and group IDs (UIDs and GIDs) for a specific user.

  • id username: Displays the user ID, group ID, and supplementary groups for the user.

6. sudo: Execute Commands as Superuser

The sudo command allows a permitted user to execute a command as the superuser (root) or another user. It’s required for most administrative tasks, including changing file ownership and permissions.

  • sudo chown user file.txt: Changes ownership with elevated permissions.
  • sudo chmod 755 file.txt: Changes file permissions as an admin.

Summary of Commands

  • ls -l: List files with permissions and ownership.
  • chmod: Modify file and directory permissions.
  • chown: Change file and directory ownership.
  • useradd: Add a new user.
  • id: Display user and group IDs.
  • sudo: Execute commands as the superuser.

 Components

If you want to buy a RaspberyPi  board try on amazon: https://amzn.to/4dqDhXz
or aliexpress: https://s.click.aliexpress.com/e/_DEVUTJh    

Documentation

Support this blog

If you make purchases using the links that lead to emag.roamazon and aliexpress.com, you will support my blog, and 50% of the donations will be directed to the "Dăruiește Viață" foundation. Thank you!

Thank you for your attention!

For questions and/or technical consulting, I am available in the comments section below or via email at simedruflorin@automatic-house.ro. Have a great day, everyone!


Back to top of page

Etichete

Afișați mai multe

Arhiva

Afișați mai multe