You are not logged in.

#1 2004-10-15 20:22:39

punkrockguy318
Member
From: New Jersey
Registered: 2004-02-15
Posts: 707
Website

Command Line Adding Users To Groups

How can I add a user to a group through the command line?  I want to do this in a shelll script... I know you can edit /etc/groups, but I want to know the command line command for it...


If I have the gift of prophecy and can fathom all mysteries and all knowledge, and if I have a faith that can move mountains, but have not love, I am nothing.   1 Corinthians 13:2

Offline

#2 2004-10-15 20:53:31

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: Command Line Adding Users To Groups

gpasswd -a user group

Offline

#3 2004-10-15 20:58:45

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Command Line Adding Users To Groups

try "man usermod"

to add:

# user to edit and his new group
USERNAME="phrakture"
NEWGROUP="sound"

# create a comma delimited list of users
GROUPS=`groups $USERNAME | sed -e s/ /,/g`

usermod $USERNAME -G $GROUPS,$NEWGROUP

and to remove:

# user to edit and his new group
USERNAME="phrakture"
OLDGROUP="sound"

# create a comma delimited list of users
GROUPS=`groups $USERNAME | sed -e s/$OLDGROUP//g | sed -e s/ /,/g`

usermod $USERNAME -G $GROUPS

now, one minor error here - in the remove script, if the group to be removed is last in the list, the list will end with a comma... I don't know if this will error out or not

Offline

#4 2004-10-15 21:00:37

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Command Line Adding Users To Groups

Snowman wrote:

gpasswd -a user group

hmmm, yes, however this requires group admin rights....

but mine is still more fun 8)

Offline

#5 2004-10-15 21:02:40

punkrockguy318
Member
From: New Jersey
Registered: 2004-02-15
Posts: 707
Website

Re: Command Line Adding Users To Groups

thanks all, exactly what i was looking for


If I have the gift of prophecy and can fathom all mysteries and all knowledge, and if I have a faith that can move mountains, but have not love, I am nothing.   1 Corinthians 13:2

Offline

Board footer

Powered by FluxBB