How to add a user to the group on linux

Information TechnologyLinux

The concept of “group” may be confusing to many Linux users. Linux operating system is designed to allow more than one user to have access to the Linux system at a time. Linux/Unix operating systems provides multitasking abilities to its multi-users. In order to do this task, we need to assign a group to each Linux user. This article gives step by step process on – how to add a user to the group in Linux system.

Create a New User

To create a new user using Linux command line, use the following command –

$ sudo useradd tutorialspoint

The above command is used to create a tutorialspoint user to the Linux system.To see the user list, use the following command –

$ awk -F':' '{ print $1}' /etc/passwd

A sample output will be as below –

.......
syslog
messagebus
usbmux
dnsmasq
avahi-autoipd
kernoops
rtkit
saned
whoopsie
speech-dispatcher
avahi
lightdm
colord
hplip
pulse
linux
gitlog
gitdaemon
tutorialspoint

For example, in the above result, you can see that tutorialspoint user is created.

Add a New Group

To add a new group in Linux, use the following command –

$ sudo groupadd editors

In the above command ,It is creates a group called editors.

Adding an Existing User to a Group

For adding tutorialspoint user to the editors group, use the following command –

$ sudo usermod -a -G editors tutorialspoint

Change a User’s Primary Group

To change the user’s primary group, use the following command –

$ sudo usermod -g linux tutorialspoint

In the above command, assign tutorialspoint user to linux group.

View a User’s Group Assignments

If you’re trying to figure out permissions assigned to a user, then you have to use the id command to see a group which is linked with the particular user. The following is the command line –

$ id tutorialspoint

The sample output will be like this –

uid=1001(tutorialspoint) gid=1000(linux) groups=1000(linux),1002(editors)

Adding a New User and Assigning a Group (through a single command)

Sometimes, you may need to create a user which should have specific accessible rights or directories, then in these cases, use the following command to create a new user and assign a group as below –

$ sudo useradd -g linux john

The above command creates a user called john and which will be added to the linux group. Use the following command to assign a password to john user –

$ sudo passwd john

The sample output should be like this –

linux@linux:~$ sudo passwd john
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Adding a User to Multiple Groups

For example, you might be required to add a user to multiple groups. To do this, use the following command –

$ sudo usermod -a -G linux,editors tutorialspoint

The above command turialspoint user is added to the linux and editors  groups.

Congratulations! Now, you know “How to add a User to the Group on Linux”. We’ll learn more about these type of commands in our next Linux post. Keep reading!

raja
Published on 21-Oct-2019 15:14:54
Advertisements