Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Automatically add user to all groups?

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
Jason_25
Posts: 28
Joined: 2014-10-10 17:55

Automatically add user to all groups?

#1 Post by Jason_25 »

I know to how to add a user to multiple groups. I know where to find the list of groups. I know where to find the list of groups used by the installer.

What I want to do is automatically add a user to all groups without having to manually type them out or spend half a day writing a script.

I know manually typing them out would only take a few minutes. I know a graphical user manager would work easily also but I want to do this without X installed.

Any way to do what I'm asking from the command line without a script and without manually typing the group names?

User avatar
yeti
Posts: 68
Joined: 2009-03-30 14:22

Re: Automatically add user to all groups?

#2 Post by yeti »

The following may be a start:

Code: Select all

$ echo usermod -a -G$(awk -F: 'NR>1{printf","}{printf$1}' /etc/group) SomeUserName
usermod -a -Groot,daemon,bin,sys,adm,tty,disk,lp,mail,news,uucp,man,proxy,kmem,dialout,fax,voice,cdrom,floppy,tape,sudo,audio,dip,www-data,backup,operator,list,irc,src,gnats,shadow,utmp,video,sasl,plugdev,staff,games,users,nogroup,input,systemd-journal,systemd-timesync,systemd-network,systemd-resolve,systemd-bus-proxy,crontab,netdev,ssl-cert,lpadmin,messagebus,avahi,scanner,Debian-exim,mlocate,ssh,avahi-autoipd,bluetooth,colord,geoclue,utempter,pulse,pulse-access,rtkit,saned,Debian-gdm,yeti,debian-tor,ruth,kvm,uml-net,wireshark,i2c SomeUserName
Ther sure are zillions of other ways to do it e.g. using "join" and other helpers...
"I have a natural instinct for science" — DJ Trump.
"Vrijdag voor VT100!" — Yeti.
"There is no PLANET-B!" — ???

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Automatically add user to all groups?

#3 Post by dasein »

If I'm understanding your requirements correctly, you want to perform a repetitive task on variable and arbitrary data, and you're open to any solution, so long as it's not manual or automated?

Image

Maybe have a look at sed?

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Automatically add user to all groups?

#4 Post by phenest »

Jason_25 wrote:without having to manually type them out or spend half a day writing a script.
So you want one of us to spend/waste half a day for you?
Jason_25 wrote:I know manually typing them out would only take a few minutes.
So, not a lot longer than making this post.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

Jason_25
Posts: 28
Joined: 2014-10-10 17:55

Re: Automatically add user to all groups?

#5 Post by Jason_25 »

What would it take, in terms of dollars to get an option added to the adduser command that would do this automatically? Failing that, what would it take, in terms of dollars for some non-lazy person to write this very boring script that checks all groups and then feeds them into the useradd command after asking the name of the user? Yes, I am completely capable of banging out a nice looking script that does this. No, I am not going to do this because I consider it an essential system command.

I consider any script or command that uses fixed groups "cheating" because it is not completely system agnostic.

Doesn't it seem like a shortcoming to have to manually do this or write a script? Yes this posting and procrastinating is taking a lot longer than it should but I really don't want to do this again. I have had to do it many times but this is the straw that broke the camel's back. I know others have been annoyed by this in the past and haven't bothered to post but I am doing the complaining for them this time.

If nothing else I will install an X based user manager and then uninstall it but I still hate this whole situation.

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Automatically add user to all groups?

#6 Post by Dai_trying »

Here's one way...

Code: Select all

#!/bin/bash
grp_lst=`cat /etc/group | awk -F: '{print $1}'`
for grp in $grp_lst ; do
    sudo usermod -a -G "$grp" "$1"
done
But I can't imagine why you would want to do this...

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Automatically add user to all groups?

#7 Post by dasein »

Jason_25 wrote:Yes, I am completely capable of banging out a nice looking script that does this.
Seems unlikely, since you can't even come up with an accurate order-of-magnitude estimate of the effort required to do so.

Jason_25
Posts: 28
Joined: 2014-10-10 17:55

Re: Automatically add user to all groups?

#8 Post by Jason_25 »

Code: Select all

#!/bin/bash

#this will add a user for you without all the trouble
#run as root
#here is a tip - do not use $GROUPS as a variable

read -p "Type the requested user name" -n 20 -r

COLLECTED_GROUPS=$( cat /etc/group | cut -d ':' -f1 )

COLLECTED_GROUPS_PROCESSED=$(echo $COLLECTED_GROUPS | tr ' ' ',')

useradd $REPLY -M -G $COLLECTED_GROUPS_PROCESSED
I call it ADDUSER_NOT_CRAP_VERSION. These are the highs and lows of free software usage. On one hand you can do anything you want, on the other hand you don't want to do anything.

User avatar
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 12 times
Been thanked: 66 times

Re: Automatically add user to all groups?

#9 Post by dilberts_left_nut »

There are a lot of groups on a standard system that an interactive user has no business being a member of.
AdrianTM wrote:There's no hacker in my grandma...

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Automatically add user to all groups?

#10 Post by Dai_trying »

Jason_25 wrote:

Code: Select all

#!/bin/bash
etc...
I call it ADDUSER_NOT_CRAP_VERSION.
I have to wonder why you even asked the question in the first place?? if you can use your ADDUSER_NOT_CRAP_VERSION (a two minute script) then why the need to ask someone else to do it for you? If it was simply to get this as a request for inclusion in the adduser package I sincerely hope it does not get in (I think the developers would have more sense).
dilberts_left_nut wrote:There are a lot of groups on a standard system that an interactive user has no business being a member of.
Agreed.

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1400
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 79 times
Been thanked: 175 times

Re: Automatically add user to all groups?

#11 Post by steve_v »

Jason_25 wrote:I call it ADDUSER_NOT_CRAP_VERSION.
And by implication the standard adduser binary is crap? Because you are too lazy to tackle the basic system-administration task of selecting groups for a user?
If adduser added new users to all groups by default it would be chaos, this is a task that is critical to system security and requires human intelligence behind the wheel.

If you have a lot of users to add at once, with the same groups, that's trivial to automate... the tool to do it is known as sh. As you well know.
But as a default behaviour of adduser? No. No way in hell. Horrible idea.
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

jibberjabber
Posts: 162
Joined: 2016-01-10 16:58

Re: Automatically add user to all groups?

#12 Post by jibberjabber »

by steve_v » If adduser added new users to all groups by default it would be chaos, this is a task that is critical to system security and requires human intelligence behind the wheel.
I need to add several new users to the PC
is this what this code does ? :
Jason_25 wrote:

Code: Select all

#!/bin/bash

#this will add a user for you without all the trouble
#run as root
#here is a tip - do not use $GROUPS as a variable

read -p "Type the requested user name" -n 20 -r

COLLECTED_GROUPS=$( cat /etc/group | cut -d ':' -f1 )

COLLECTED_GROUPS_PROCESSED=$(echo $COLLECTED_GROUPS | tr ' ' ',')

useradd $REPLY -M -G $COLLECTED_GROUPS_PROCESSED
I call it ADDUSER_NOT_CRAP_VERSION. These are the highs and lows of free software usage. On one hand you can do anything you want, on the other hand you don't want to do anything.
I was searching the forum and wanting to learn more about using the adduser command is this script safe to use ?
I also have been looking at this :

Code: Select all

man adduser
and

Code: Select all

man usermod
I do not want my new users to have sudo or root access to the system.
So is this script safe to try ? How would I be able to reverse it
if I do not like the results ?
From what steve_v says I am thinking it is not a good script to use but hopeing it is no problem to ask. I assume the administration and moderators would not allow any scripts that are harmful or could cause problems to my computer but there is some comments in this thread that are confusing and it is not clear what the script does.
Also how do I use it ?
Thank you in advance.

steve_v
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1400
Joined: 2012-10-06 05:31
Location: /dev/chair
Has thanked: 79 times
Been thanked: 175 times

Re: Automatically add user to all groups?

#13 Post by steve_v »

jibberjabber wrote: I need to add several new users to the PC
is this what this code does ? :
At a cursory glance, it collects all groups from /etc/groups and adds a new user with membership of those groups. Most likely not at all what you want.

Find the commands you need to add a user with the groups you want (man pages etc), then, if you have a bunch of users the same to do, wrap it in e.g. a for loop with a list of users. for user in <list>; do <something>; done
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Automatically add user to all groups?

#14 Post by dasein »

jibberjabber wrote:I need to add several new users to the PC
is this what this code does ?
No.
jibberjabber wrote:is this script safe to use ?
Bless you for asking this question.

"Safe" in the sense that it won't erase your hard drive, yes; but it lacks both elegance and error-checking. More to the point, as others have already pointed out, it's also kind of pointless. I'm sure the OP imagines that it has some value, but that value is not at all obvious to others. Then too, depending on one's configuration, the script could plausibly create a number of troubling (and entirely optional) problems.
jibberjabber wrote:I also have been looking at this :

Code: Select all

man adduser
and

Code: Select all

man usermod
Bless you :cool:
jibberjabber wrote:I assume the administration and moderators would not allow any scripts that are harmful or could cause problems to my computer
That's not a safe assumption to make.

Based on what you've said, adduser is the right tool for the job you're trying to do. And there is really no benefit to trying to write a script for it; you won't save (m)any keystrokes by automating it.

Bulkley
Posts: 6383
Joined: 2006-02-11 18:35
Has thanked: 2 times
Been thanked: 39 times

Re: Automatically add user to all groups?

#15 Post by Bulkley »

This could be one of those cheats that is harder and more time consuming than by simply doing the chore.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Automatically add user to all groups?

#16 Post by debiman »

jibberjabber wrote:
by steve_v » If adduser added new users to all groups by default it would be chaos, this is a task that is critical to system security and requires human intelligence behind the wheel.
I need to add several new users to the PC
is this what this code does ? :
I was searching the forum and wanting to learn more about using the adduser command is this script safe to use ?
I also have been looking at this :

Code: Select all

man adduser
and

Code: Select all

man usermod
no, and no.
as was pointed out already, this script does something very questionable.
it is hard to perceive how this could even be useful on one particular setup, but it definitely is NOT something you want to suggest/recommend to others.
you should use the tools that are usually present on any linux distro, and take the time to understand what you're doing and how to achieve it.

Post Reply