Wednesday, July 6, 2011

Adding a user to a group

How can I add a user to a group under Linux operating system?

You to use the useradd or usermod commands to add a user to a group. The useradd command creates a new user or update default new user information.

The usermod command modifies a user account i.e. it is useful to add user to existing group.

There are two types of group.

  1. Primary user group
  2. Secondary group.

All user account related information is stored in /etc/passwd, /etc/shadow and /etc/group files to store user information.
useradd Example - Add A New User To Secondary Group

You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it. The syntax is as follows:

useradd -G {group-name} username

In this example, create a new user called sgupta and add it to group called hack. First login as a root user (make sure group  hack exists if group didn’t exists you can create the one using these coomand),

First login as a root user:
[user@rhel52x86 blog-dir]$su -
Password: *******

If you have already added a working group then verifies that first

[root@rhel52x86 blog-dir]# grep developers /etc/group

If group is not there add it using the following commands
[root@rhel52x86 blog-dir]# groupadd hack
bash: groupadd: command not found

OOPs seems you have not setted the binary path, no worries try it out:
[root@rhel52x86 blog-dir]# find / -name groupadd

Now add you group as follows:
[root@rhel52x86 blog-dir]# /usr/sbin/groupadd hack

Next, add a user called sgupta to group developers:
[root@rhel52x86 blog-dir]# useradd -G hack sgupta

Setup password for user sgupta:
[root@rhel52x86 blog-dir]# passwd sgupta
Changing password for user sgupta.
New UNIX password:
BAD PASSWORD: it is based on a dictionary word
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Ensure that user added properly to group developers:
[root@rhel52x86 blog-dir]# id sgupta
uid=5008(sgupta) gid=5008(sgupta) groups=5008(sgupta)

Please note that capital G (-G) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no intervening whitespace. For example, add user sgupta to groups admins, ftp, www, and developers, enter:

[root@rhel52x86 blog-dir]# useradd -G admins,ftp,www,hack sgupta

You have sucessfully added the user now, go and enjoy developing and hacking there.

No comments:

Post a Comment