DEV Community

Cover image for User and Group Management
Abhishek Pathak
Abhishek Pathak

Posted on • Edited on

User and Group Management

Create new user

Linux daddy will guide you.

sudo adduser username
Enter fullscreen mode Exit fullscreen mode

You are now a daddy

sudo useradd username
Enter fullscreen mode Exit fullscreen mode

useradd

Check all the users

sudo cat /etc/shadow
Enter fullscreen mode Exit fullscreen mode

etc shadow

Consider,
abhishek2:!:19579:0:99999:7:::

The row is split into 9 fields seperate by :

Each field resembles as:

  1. Username. User account and login name that exist in the system.
    1. Encrypted password. Password using the format $type$salt$hashed and eight to 12 characters long.
    2. Last password change. Date since Jan. 1, 1970, when the password was last changed.
    3. Minimum password age. The minimum number of days that must elapse before the password can be changed by the user.
    4. Maximum password age. The number of days after which the password must be changed.
    5. Warning period. The number of days before the password expires, during which time the user gets a warning to change the password.
    6. Inactivity period. The number of days post-expiration -- since Jan. 1, 1970 -- before the user's account is disabled.
    7. Expiration date. The date on which the account was disabled.
    8. Unused. This field is left empty and reserved for future use

User with custom shell

sudo useradd -s /bin/bash abhishek
Enter fullscreen mode Exit fullscreen mode
  • /bin/bash -> env shell

User with own home directory

sudo useradd -m -d /home/abhishek abhishek
Enter fullscreen mode Exit fullscreen mode
  • /home/abhishek4 -> this can be any path

User with comment

sudo useradd -c "user name is abhishek" abhishek
Enter fullscreen mode Exit fullscreen mode

User with the account expiery date

useradd -e 2023-08-32 apple
Enter fullscreen mode Exit fullscreen mode
  • -e -> The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.

User with custom userId

sudo useradd -u 1822 abhishek
Enter fullscreen mode Exit fullscreen mode

Change default home directory of user geet

usermod -d /var/tmp/user/geet -m geet
Enter fullscreen mode Exit fullscreen mode

Change login name of user geet

sudo usermod -i geet GEET 
Enter fullscreen mode Exit fullscreen mode

Change default shell of user geet

usermod -s /bin/fish geet
Enter fullscreen mode Exit fullscreen mode

Set user password

sudo passwd username
Enter fullscreen mode Exit fullscreen mode

add user passswd

Switch user

su username
Enter fullscreen mode Exit fullscreen mode

add new group with own home directory with custom user id and group and custom comment to user geet, with login shell bash

useradd -m -d /home/geet -u 1029 -g 1822 -c "geet user" -s /bin/bash geet
Enter fullscreen mode Exit fullscreen mode

Create new group

Create a new group

Linux daddy will guide you

sudo addgroup groupName
Enter fullscreen mode Exit fullscreen mode

addgroup

You are now daddy

sudo groupadd grp1
Enter fullscreen mode Exit fullscreen mode

create new group

Create group with custom ID

sudo groupadd -g 2218 groupName
Enter fullscreen mode Exit fullscreen mode

Add user to group

# sudo usermod --append --groups demo user1
sudo usermod -aG groupname userName
Enter fullscreen mode Exit fullscreen mode

usermod

Display details about group

sudo cat /etc/gshadow
Enter fullscreen mode Exit fullscreen mode

Change primary group of geet to ak

sudo usermod -g ak geet
Enter fullscreen mode Exit fullscreen mode

Add geet as a menber of group jeet

sudo gpasswd -M geet jeet
Enter fullscreen mode Exit fullscreen mode

Remove user geet from group jeet

sudo gpasswd -d geet jeet
Enter fullscreen mode Exit fullscreen mode

Make geet admin of group jeet

sudo gpasswod -A geet jeet
Enter fullscreen mode Exit fullscreen mode

Set password for group jeet

sudo gpasswd jeet
Enter fullscreen mode Exit fullscreen mode

Delete user and group

Delete user

sudo userdel geet
Enter fullscreen mode Exit fullscreen mode

Delelet user with all files

sudo userdel -r geet
Enter fullscreen mode Exit fullscreen mode

Delete Group

sudo groupdel jeet
Enter fullscreen mode Exit fullscreen mode

If the article helps you, leave a like, follow, or anything 🙂.

You can follow me on LinkedIn and GitHub.

Top comments (2)

Collapse
 
akshar2001 profile image
Akshar Parmar

Great!

Collapse
 
scorcism profile image
Abhishek Pathak

Thank you :)