Automatically add user to all groups?

Kernels & Hardware, configuring network, installing services

Automatically add user to all groups?

Postby Jason_25 » 2017-05-02 14:33

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?
Jason_25
 
Posts: 28
Joined: 2014-10-10 17:55

Re: Automatically add user to all groups?

Postby yeti » 2017-05-02 14:48

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...
"They may have computers, and other weapons of mass destruction." — Janet Reno
"Logic, my dear Zoe, merely enables one to be wrong with authority." — The 2nd Doctor
"Don't we all wait for SOMETHING-ELSE-1.0?" — yeti
User avatar
yeti
 
Posts: 37
Joined: 2009-03-30 14:22
Location: Wrong Planet.

Re: Automatically add user to all groups?

Postby dasein » 2017-05-02 15:16

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
dasein
 
Posts: 7635
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Automatically add user to all groups?

Postby phenest » 2017-05-02 15:38

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.
NEC Spirit 550 P4 3.8GHz HT - 2GB RAM - nVidia 7600GT - Pioneer BDR-209DBK
ASUS Sabertooth P67 i7 3770K - 32GB RAM - 2x nVidia 660GTX SLI'd
User avatar
phenest
 
Posts: 1571
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Automatically add user to all groups?

Postby Jason_25 » 2017-05-02 19:57

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.
Jason_25
 
Posts: 28
Joined: 2014-10-10 17:55

Re: Automatically add user to all groups?

Postby Dai_trying » 2017-05-02 20:26

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...
Dai_trying
 
Posts: 292
Joined: 2016-01-07 12:25

Re: Automatically add user to all groups?

Postby dasein » 2017-05-03 00:10

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.
User avatar
dasein
 
Posts: 7635
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Automatically add user to all groups?

Postby Jason_25 » 2017-05-03 02:31

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.
Jason_25
 
Posts: 28
Joined: 2014-10-10 17:55

Re: Automatically add user to all groups?

Postby dilberts_left_nut » 2017-05-03 06:22

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...
User avatar
dilberts_left_nut
 
Posts: 4614
Joined: 2009-10-05 07:54
Location: enzed

Re: Automatically add user to all groups?

Postby Dai_trying » 2017-05-03 07:36

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.
Dai_trying
 
Posts: 292
Joined: 2016-01-07 12:25

Re: Automatically add user to all groups?

Postby steve_v » 2017-05-03 07:55

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.
steve_v
 
Posts: 452
Joined: 2012-10-06 05:31
Location: New Zealand

Re: Automatically add user to all groups?

Postby jibberjabber » 2017-05-03 21:44

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.
jibberjabber
 
Posts: 13
Joined: 2016-01-10 16:58

Re: Automatically add user to all groups?

Postby steve_v » 2017-05-03 23:56

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
steve_v
 
Posts: 452
Joined: 2012-10-06 05:31
Location: New Zealand

Re: Automatically add user to all groups?

Postby dasein » 2017-05-04 00:05

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.
User avatar
dasein
 
Posts: 7635
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Automatically add user to all groups?

Postby Bulkley » 2017-05-04 01:11

This could be one of those cheats that is harder and more time consuming than by simply doing the chore.
Bulkley
 
Posts: 5194
Joined: 2006-02-11 18:35

Next

Return to System configuration

Who is online

Users browsing this forum: ramblin and 9 guests

fashionable