Managing users and permissions in Linux is essential for maintaining system security and controlling access to resources. This guide walks you through the fundamental commands and concepts for creating users, managing groups, and setting file permissions on Linux systems.

How to do it

  1. Step 1: Create and manage users

    To add a new user to your system, use the useradd command:

    sudo useradd john

    After creating the user, set their password with passwd:

    sudo passwd john

    To remove a user and their home directory, use userdel with the -r flag:

    sudo userdel -r john

    You can modify user properties like username, home directory, or primary group using the usermod command.

  2. Step 2: Create and manage groups

    Create a new group using groupadd:

    sudo groupadd team

    Add a user to a group with usermod and the -aG flags:

    sudo usermod -aG team john

    Check which groups a user belongs to:

    groups john

    Remove a group using groupdel:

    sudo groupdel team
  3. Step 3: Understand and view permissions

    Each file and directory in Linux has three sets of permissions for the owner, group, and others: read (r), write (w), and execute (x).

    View permissions for a file or directory using ls -l:

    ls -l /path/to/file
  4. Step 4: Change file permissions and ownership

    Use chmod to change permissions. For example, to add execute permission to a script:

    chmod +x script.sh

    Change the owner and group of a file using chown:

    sudo chown john:team file.txt

    This changes the owner to user "john" and the group to "team".

  5. Step 5: Set default permissions with umask

    The umask command sets the default permissions for new files and directories created by users:

    umask 022
  6. Step 6: Work with special permissions

    Linux supports special permissions like Setuid, Setgid, and Sticky Bit that control file and directory behavior. These are represented by s, S, and t respectively in permission listings.

Next steps

This guide covers the fundamentals of user and permission management in Linux. As you become more familiar with these concepts, you can explore advanced topics such as ACLs (Access Control Lists) for finer-grained control over file permissions.