Skip to content

Users & Groups

Each user in UNIX has the following parameters:

  • User name
  • Encrypted password (or ‘x’ if hash is stored)
  • User Identifier (UID)
  • Group Identifier (GID)
  • Full name or description
  • User’s home directory
  • User’s shell
  • Expiration date

Each group in UNIX has the following parameters:

  • Group name
  • Encrypted password (or ‘x’ if hash is stored)
  • Group Identifier (GID)

root is super user. It has unlimited rights in the system.

Do not use without necessity!

User:

  • su
  • sudo
  • useradd
  • userdel
  • usermod
  • passwd
  • finger

Groups:

  • groupadd
  • groupdel
  • groupmod
  • groups

image.png

  • to create a new user - useradd <name>

    • NOTE: this just creates a user, no home directory and other stuff like shell etc..
    • to create a complete user run adduser <username>
      • Then whats the use of useradd?
        • it is used heavily in scripting where we dont want to give inputs to adduser but do everything from script
  • to set password to created user - passwd <user_name>

  • to switch to that user su - <user> or su <user>

    • su - - switch to root by default
  • check all users in /etc/passwd

  • to check all passwords /etc/shadow

  • to delete user userdel <user_name>

    • this won’t delete home dir… to delete user along with home dir use -r like.. userdel -r username

Key files involved in user management:

  • /etc/passwd – Stores user account details.
  • /etc/shadow – Stores encrypted user passwords.
  • /etc/group – Stores group information.
  • /etc/gshadow – Stores secure group details

image.png

image.png

image.png

image.png

useradd -u <CUSTOM_USER_ID> -G <GROUP> -d <HOME_DIR> -m -c "<COMMENT>" -s /usr/bin/bash <USER_NAME>

Modifying Users

Modify an existing user with usermod:

  • add to a group

    • usermod -aG <GROUP> <USER>
  • Change the username:

    usermod -l new_username old_username
  • Change the default shell:

    usermod -s /bin/zsh username

image.png

image.png

image.png

Creating Groups

groupadd groupname

Adding Users to Groups

usermod -aG groupname username

NOTE: usermod -G group username here user is removed from his previous group and added to current group, bcoz no a flag in command

Adding a User to Sudo Group

On Debian-based systems:

usermod -aG sudo username

On RHEL-based systems:

usermod -aG wheel username

Viewing Group Memberships

groups username

Changing Primary Group

usermod -g new_primary_group username

what is su - username ?

  • if we run su with - then , .bashrc_profile file will be executed after login, without - i.e su username it is not loaded…