Skip to main content

Command Palette

Search for a command to run...

Permissions, users and groups

Published
4 min read
Permissions, users and groups
X

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.

"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."

Imagine you have a Linux system with multiple users, and you want to manage access to a specific directory called /project. You want to give some users read and write access to the /project directory, while others should have read-only access. To do this efficiently, you can use groups.

Here's how you might use groups to manage access:

  1. Create Groups:

    First, you create two groups: project_rw for users who should have read and write access and project_ro for users who should have read-only access.

     sudo groupadd project_rw
     sudo groupadd project_ro
    
  2. Add Users to Groups:

    Next, you add users to the appropriate groups. For example:

     sudo usermod -aG project_rw user1
     sudo usermod -aG project_rw user2
     sudo usermod -aG project_ro user3
     sudo usermod -aG project_ro user4
    
    • user1 and user2 are added to the project_rw group, which means they have read and write access to the /project directory.

    • user3 and user4 are added to the project_ro group, which means they have read-only access to the /project directory.

  3. Set Directory Ownership:

    You can set the ownership of the /project directory to a specific user (e.g., project_owner) and the project_rw group:

     sudo chown project_owner:project_rw /project
    

    This means that the owner (project_owner) has full control over the directory, and the project_rw group has group ownership.

  4. Set Directory Permissions:

    Finally, you can set the permissions on the /project directory to control access:

     sudo chmod 770 /project
    
    • 7 for the owner: This grants read (4), write (2) and execute (1) permissions, so the owner can read, write and execute on the directory.

    • 7 for the group: This also grants read (4) and write (2) permissions to the group, so members of project_rw can read and write to the directory.

    • 0 for others: This means others (users not in the owner or group) have no permissions on the directory.

With this setup, user1 and user2 (members of the project_rw group) can read and write to the /project directory, while user3 and user4 (members of the project_ro group) can only read from it.

In summary, groups allow you to manage permissions more effectively by grouping users with similar access needs and applying permissions to those groups, which simplifies access control and file management on a Linux system.

How to add multiple users in a linux system

Creating a Linux system with multiple users involves setting up a Linux operating system, creating user accounts, and configuring permissions and groups. Here's a step-by-step guide on how to achieve this:

  1. Install Linux:

    Install a Linux distribution of your choice on a computer or a virtual machine. Popular Linux distributions include Ubuntu, Debian, CentOS, Fedora, and others.

  2. Create a User:

    During the installation process or after the installation is complete, you'll typically create a user. This user is often created with administrative privileges and can use sudo to perform actions with superuser permissions.

  3. Add Additional Users:

    After the initial user is created, you can add more users using the adduser or useradd command. For example, to add a user named "john":

     sudo adduser john
    

    Follow the prompts to set a password and provide additional user information.

  4. Grant Administrative Privileges:

    If you want to grant administrative privileges to additional users, you can add them to the sudo group. On Ubuntu, members of the sudo group can run commands with superuser privileges.

     sudo usermod -aG sudo john
    
  5. Manage User Groups:

    Create and manage user groups as needed using the groupadd, groupmod, and groupdel commands. For instance, to create a group called "developers":

     sudo groupadd developers
    
  6. Assign Users to Groups:

    Assign users to groups using the usermod command. For example, to add the user "john" to the "developers" group:

     sudo usermod -aG developers john
    
  7. Manage Permissions:

    Set file and directory permissions based on user groups using chmod. For example, to give the "developers" group read and write access to a directory:

     sudo chgrp developers /path/to/directory
     sudo chmod g+rw /path/to/directory
    

    This allows users in the "developers" group to read and write to the specified directory.

By following these steps, you can set up a Linux system with multiple users, each having their own user account and optionally belonging to various groups with specific permissions and access rights.

More from this blog

PERSONAL BLOG

110 posts

Use the search button to search for a specific topic or keyword *all posts are updated on the go*