Linux basics part – 2
Creating Users in Linux
Let’s create a user, open your terminal in linux
$ sudo adduser <username>
Example:-
Now login created user
$ sudo su – <username>
Example:-
$ sudo su – pramod
$ whoami
What is Group in Linux?
Group is a generally a collection of users. In Linux, we can create the user accounts for users and add them in a different groups according to their role and set a privileges on them.
Instead of setting the privileges on users, create a group and set a privileges on that group and add users in that group. So, its save your time.
In Linux, there are two types of Groups
- Primary – primary group is create when we create user and remember primary group’s username and group name is same.
- Secondary – Secondary group is like existing group. for example, we have System Admin group, if we add a new user then the System Admin groups privileges automatically applied on new user.
Creating Groups in Linux
Let’s create Groups in Linux
$ sudo groupadd <group-name>
In below example, i have created one group named redient-security
Here,
redient-security | name of group |
x | group password |
1002 | group id |
Adding Users in group
$ sudo usermod -a -G <groupname> <username>
Here -a is append means adding and G is for Secondary group
For example
Adding Users in Multiple groups
In Linux, we can add one user in multiple groups.
$ sudo usermod -a -G <group-name>,<group-name> <username>
For example:-
$ sudo usermod -a -G redient-security,sredient pramod
Adding Multiple users in a group
$ sudo usermod -a -G <groupname> <username>
In below example, i have added redient and pramod users in redient-security group
Removing User from group
gpasswd is used to remove user from a group with -d option.
$ sudo gpasswd -d <username> <groupname>
For example:-
$ sudo gpasswd -d pramod sredient
Create New User and add in Existing group in one commands
$ sudo useradd -g users -G redient-security,sredient kovid
here, -g is primary group, users is primary group name, -G option is for secondary group, redient-security and sredient is secondary group name and kovid is new user name