Unix Users and Groups

Basic info

Each user has a primary group and a list of supplementary groups. The primary group is used mainly when creating new files. The list of groups (primary and all supplementary) are used for granting access.

A file has a user-owner and group-owner. Any particular user is either the file owner, in the file's group, or other.

The user permissions (rwx------ 0700) grant access to the owner.
The group permissions (---rwx--- 0070) grant access to users who are in the file's group.
All other users get other permissions (------rwx 0007).

The setuid bit (--s------ 4000):

The setgid bit (-----g--- 2000):

The sticky bit (--------t 1000):

The umask is a per-process value that indicates which permission bits to turn off when creating a file. See and set its value with the umask shell command.

Creating files

When a file is created:

  • Its owner is the user creating the file.
  • Its group is the primary group of the user creating the file.
  • Its permissions are 0666 & ~umask. The umask is a per-process value that indicates which permission bits to turn off when creating a file.

    Create new user

    This creates a new user and adds them to /etc/password. It also creates a group of the same name and assigns it as the primary group. The -m option causes a home directory to be created with default files.

        sudo useradd -m -s /bin/bash username
    
    Useful options:
                    -m           - create the home dir and cp files from /etc/skel
                    -s /bin/bash - set shell 
    

    Create new group

        sudo groupadd name_of_new_group
    

    Add existing user to existing group

    This adds the user to the group so the group will be in the users list of supplementary groups.

        sudo usermod -a -G name_of_group username