LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to add user in a group? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-user-in-a-group-795996/)

gardenair 03-17-2010 06:15 AM

How to add user in a group?
 
Hi,
I have four users in my red hat linux 9. I want that all these four users should add in a group i.e "Marketing".please guide me that using terminal which command may i write so that the users should added in the group.

Note:- I does't want to use GUI interface to do it.

thanks in advance,
garden

Sayan Acharjee 03-17-2010 06:18 AM

Try this:
Quote:

#usermod -G Marketing username

blacky_5251 03-17-2010 06:21 AM

Code:

for user in john bill mary jane
do
  usermod -G marketing $user
done

Substitute john bill mary and jane for your real user names.

gardenair 03-17-2010 06:34 AM

thanks for the prompt reply. Well the command works fine. Now a question rises that how can I see all four users in "Marketing " Group. Is there anyway command to check it ?

Sayan Acharjee 03-17-2010 06:36 AM

Quote:

Originally Posted by gardenair (Post 3901646)
thanks for the prompt reply. Well the command works fine. Now a question rises that how can I see all four users in "Marketing " Group. Is there anyway command to check it ?

Use this command:
Quote:

#cat /etc/group| grep Martketing

Zuulie 03-17-2010 06:44 AM

You may want to use
Code:

egrep '^Marketing:' /etc/group
to make sure you only get exact matches of "Marketing", especially if you want to use it in a script.

-- Dan

gardenair 03-17-2010 06:47 AM

thanks a lot all of you for guiding me. Well about users and groups in linux and the files related to it, kindly refer me any site in which I may study it in more detail.

Again thanks
garden

gardenair 03-17-2010 06:51 AM

well the command

#cat /etc/group| grep Martketing

and

egrep '^Marketing:' /etc/group

provide same output.So what is the difference in both these and in which situation I may use egrep or cat command.

Iam sure there should be a minor difference.

jamescondron 03-17-2010 06:52 AM

Man pages will tell you.
Code:

man grep
man egrep


Sayan Acharjee 03-17-2010 06:54 AM

Basically grep and egrep is same command, look into the man page for more information.
#man grep

Zuulie 03-17-2010 06:57 AM

Quote:

Originally Posted by gardenair (Post 3901676)
well the command

#cat /etc/group| grep Martketing

and

egrep '^Marketing:' /etc/group

provide same output.So what is the difference in both these and in which situation I may use egrep or cat command.

Iam sure there should be a minor difference.

The significance isn't the type of grep used; it's the usage of regexp that is more explicit.

Your example would match more than "Marketing" (e.g. "Marketing2"), that could cause problems when used in scripts.

jamescondron 03-17-2010 07:04 AM

Nothing like allowing the OP to answer his/her own question, eh? Your answer isn't quite right either.

from the man page:
Quote:

In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as
grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow
historical applications that rely on them to run unmodified.

<snip>

Matcher Selection
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

Zuulie 03-17-2010 07:14 AM

Quote:

Originally Posted by jamescondron (Post 3901694)
Nothing like allowing the OP to answer his/her own question, eh? Your answer isn't quite right either.

from the man page:

LOL. I forgot how these discussions can take off. Quite refreshing.

Yes, you're right. Let's get it entirely right:
Code:

/bin/grep '^Marketing:' /usr/group

chrism01 03-17-2010 07:17 AM

If you're really using red hat linux 9 (codename Shrike) http://en.wikipedia.org/wiki/Red_Hat_Linux, it hasn't been updated, inc security in years. Do yourself (& us ) a favour an get something current eg http://en.wikipedia.org/wiki/CentOS

jwl17330536 03-17-2010 07:36 AM

Quote:

Originally Posted by Zuulie (Post 3901662)
You may want to use
Code:

egrep '^Marketing:' /etc/group
to make sure you only get exact matches of "Marketing", especially if you want to use it in a script.

-- Dan

I'm sure if he were writing a script he wouldn't be asking for help on adding users to a explicit group... My mind tells me he would know that (or google) already.

Zuulie 03-17-2010 08:07 AM

Wow. This was an interesting and unexpected first experience of this forum.

So, the command has been corrected as 'egrep' was deprecated according to the man page.

Let's correct the rationale as well:
Quote:

To prevent more results than the intended to be returned, the following, more explicit, expression should be used.

lugoteehalt 03-29-2010 01:19 PM

Quote:

Originally Posted by sayan_acharjee (Post 3901626)
Try this:#usermod -G Marketing username

Tried it and the only group user now belongs to is 'Marketing'. All other group memberships have been removed. Debian Lenny. Any suggestions how I rescue situation?

Bratmon 03-29-2010 04:44 PM

Quote:

Originally Posted by lugoteehalt (Post 3917025)
Tried it and the only group user now belongs to is 'Marketing'. All other group memberships have been removed. Debian Lenny. Any suggestions how I rescue situation?

Try
Code:

man usermod
I found the answer in less time than it took to make this post.

lugoteehalt 03-29-2010 08:00 PM

Quote:

Originally Posted by Bratmon (Post 3917202)
Try
Code:

man usermod
I found the answer in less time than it took to make this post.

You misunderstand. My point was that, with hind sight foolishly, I just did the usermod -G thing. I did it because I could not get the user added to the scanner group.

This was because I was adding ':username' to the end of the scanner line in /etc/group. It should have been ',username'. This is only if there is another name(s) on the end of the line, the first name is indeed seperated by a : .

I was, moderately, complaining about being buggered up in this way.

My point was it is difficult to work out which groups the user should belong to given that all this information has been destroyed.

I was warning people who read this post not to use the idiotic usermod -G command.

So to be bloody idiotic: I am in the right and you are in the wrong.

itsbrad212 03-29-2010 11:47 PM

Not that hard. Could have been solved with a simple google search:

Code:

usermod -a -G group_name user_name

lugoteehalt 03-30-2010 10:16 AM

Quote:

Originally Posted by itsbrad212 (Post 3917500)
Not that hard. Could have been solved with a simple google search:

Code:

usermod -a -G group_name user_name

Right, just once more: Using usermod -G removes all existing groups from the user. The above advice in this thread is therefore dangerous and wrong. That is the point I am making.

chrism01 03-30-2010 07:07 PM

Disagree
Quote:

If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via -a option, which appends user to the current supplementary group list.
http://linux.die.net/man/8/usermod
See 2nd sentence....

lugoteehalt 03-30-2010 08:28 PM

Quote:

Originally Posted by chrism01 (Post 3918513)
Disagree

http://linux.die.net/man/8/usermod
See 2nd sentence....

Yes I know but the above thing says 'usermod -G' i.e. no '-a'. So it removes the user from all pre-existing groups. That's all I'm saying. Someone reading this thread might, as I did, do usermod -G thinking they would add the user to a group and not realize they would remove the user from all his other groups. My motive in posting is one of service to other Linux users.:)

itsbrad212 03-31-2010 09:50 AM

Quote:

Originally Posted by lugoteehalt (Post 3918041)
Right, just once more: Using usermod -G removes all existing groups from the user. The above advice in this thread is therefore dangerous and wrong. That is the point I am making.

Correct. I believe the OP of the advice should edit the post and add the correct method of doing it, so if somebody browsing this runs the first command they see, it won't be as dangerous

lugoteehalt 03-31-2010 12:01 PM

Quote:

Originally Posted by itsbrad212 (Post 3919240)
Correct. I believe the OP of the advice should edit the post and add the correct method of doing it, so if somebody browsing this runs the first command they see, it won't be as dangerous

I feel vindicated.:D

itsbrad212 04-01-2010 01:47 AM

Quote:

Originally Posted by lugoteehalt (Post 3919342)
I feel vindicated.:D

Oh, haha I wasn't blaming you :D

prpednek 04-01-2010 02:18 AM

Quote:

Originally Posted by gardenair (Post 3901646)
thanks for the prompt reply. Well the command works fine. Now a question rises that how can I see all four users in "Marketing " Group. Is there anyway command to check it ?


Hi,
You could have also created the users with the group reference in one commandline.

useradd -g <USER_GROUP> -d <USER_HOME> -c <Some Description> <USERNAME>

This command will only work if the USER_GROUP already exists.
Command to create user group :
groupadd <USER_GROUP>

To answer to your last question, use can use either the 'id' command coupled with the username or grep on the group file as shown.

eg: grep ^<USER_GROUP> /etc/group

eg: id tom
You would need to execute 'id' command for each user.


All times are GMT -5. The time now is 01:42 AM.