WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Adding a user to multiple groups in one command  (Read 7129 times)

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Adding a user to multiple groups in one command
« on: May 14, 2018, 12:32:43 AM »
Hi All,

I'm just wondering if there is a command built into busybox to allow me to add a user to multiple groups in one command ?

atm I'm doing this

Code: [Select]
sudo addgroup User1 Group1
sudo addgroup User1 Group2
sudo addgroup User1 Group3
sudo addgroup User1 Group4
sudo addgroup User1 Group_drive5

Thx
Live long and prosper.

Offline xyz-worx

  • Jr. Member
  • **
  • Posts: 69
Re: Adding a user to multiple groups in one command
« Reply #1 on: May 14, 2018, 04:40:54 AM »
Hi remus,

might this work?

https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/

See the section 'usermod' ....

best regards
    xyz-worx

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Adding a user to multiple groups in one command
« Reply #2 on: May 14, 2018, 01:22:29 PM »
Hi remus
The TC9 repo doesn't have  shadow-utils.tcz  which supplies  usermod. Scripting the function might make sense, especially
if you are constantly adding users to the same list of groups. Something along the lines of this may be of use:
Code: [Select]
#!/bin/sh
# Script for adding user(s) to group(s) by Richard Rost May 14.2018

Groups="All The Kings Men"
Users=$1

SyntaxMessage()
{
echo -e "Usage: $0 User [\"Group1 Group2 Group3 etc\"]\n"
echo -e "   Or: $0 \"User1 User2 User3 etc\" [\"Group1 Group2 Group3 etc\"]\n"
}

if [ -z "$Users" ]
then # No Users parameter was supplied
SyntaxMessage
exit
fi

if [[ ${Users:0:1} == "-" ]] # Test $Users:Character 0:Length 1
then # Parameter with a leading dash was passed. Assume it's -h or --help
SyntaxMessage
exit
fi

if [ ! -z "$2" ]
then # Groups list from command line
Groups=$2
fi

for User in $Users
do
for Group in $Groups
do
# ****** Modify the following line for your needs ******
echo "addgroup $User $Group"
done
done

This version echos rather than executing  addgroup  commands so you can see how it works. This is the help screen:
Code: [Select]
tc@box:~$ ./addgroups2Groups -h
Usage: ./addgroups2Groups User ["Group1 Group2 Group3 etc"]

   Or: ./addgroups2Groups "User1 User2 User3 etc" ["Group1 Group2 Group3 etc"]

tc@box:~$

Adding 2 users to the  Groups  list in the script:
Code: [Select]
tc@box:~$ ./addgroups2Groups "remus Rich"
addgroup -G All remus
addgroup -G The remus
addgroup -G Kings remus
addgroup -G Men remus
addgroup -G All Rich
addgroup -G The Rich
addgroup -G Kings Rich
addgroup -G Men Rich
tc@box:~$

Overriding the  Groups  list in the script:
Code: [Select]
tc@box:~$ ./addgroups2Groups "Thelma Louise" "Over The Cliff"
addgroup -G Over Thelma
addgroup -G The Thelma
addgroup -G Cliff Thelma
addgroup -G Over Louise
addgroup -G The Louise
addgroup -G Cliff Louise
tc@box:~$

If you place the script in  ~/.local/bin  it will be included in the path.

    [EDIT]: Corrected syntax. Replaced all instances of adduser with addgroup.  Rich
« Last Edit: May 16, 2018, 09:28:12 AM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Adding a user to multiple groups in one command
« Reply #3 on: May 14, 2018, 01:25:38 PM »
Hi remus
I feel this is very much on topic and may be of interest to others so I moved it to  Programming & Scripting - Unofficial.

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Re: Adding a user to multiple groups in one command
« Reply #4 on: May 15, 2018, 09:57:45 PM »
Thanks Rich.

I'll try this out asap.
Live long and prosper.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Adding a user to multiple groups in one command
« Reply #5 on: May 15, 2018, 10:12:08 PM »
Hi remus
Just modify this line at the end of the script to meet your needs:
Code: [Select]
echo "addgroup $User $Group"Remove the  echo  and the  quotes.

    [EDIT]: Corrected syntax. Replaced all instances of adduser with addgroup.  Rich
« Last Edit: May 16, 2018, 09:29:49 AM by Rich »

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Re: Adding a user to multiple groups in one command
« Reply #6 on: May 16, 2018, 02:19:27 AM »
Hi Rich,

Have just experimented with mc9.0 and can report a little problem.

Code: [Select]
tc@afstest:~$ sudo adduser -s /bin/sh TestUser
Changing password for TestUser
New password:
Bad password: too weak
Retype password:
passwd: password for TestUser changed by root
tc@afstest:~$ sudo cat /etc/passwd
root:x:0:0:root:/root:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/false
tc:x:1001:50:Linux User,,,:/home/tc:/bin/sh
----Content-Removed------
TestUser:x:1012:1012:Linux User,,,:/home/TestUser:/bin/sh
tc@afstest:~$ sudo cat /etc/group
root:x:0:
lp:x:7:lp
nogroup:x:65534:
staff:x:50:
----Content-Removed------
TestUser:x:1012:
tc@afstest:~$ sudo adduser -G admin TestUser
adduser: user 'TestUser' in use

Code: [Select]
$ sudo AddUsers2Groups TestUser admin
adduser: user 'TestUser' in use

I've removed the echo and qoutes from the end of your script, and got multiple "adduser: user 'TestUser' in use" errors when i ran it.
Live long and prosper.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Adding a user to multiple groups in one command
« Reply #7 on: May 16, 2018, 09:21:45 AM »
Hi remus
Sorry, my screwup. In you original post you correctly listed using  addgroup  which I promptly misread as  adduser. :-[
That line should read:
Code: [Select]
addgroup $User $GroupI will correct my other posts to reflect this.

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Re: Adding a user to multiple groups in one command
« Reply #8 on: May 16, 2018, 10:14:54 PM »
Hi Rich

Further testing -

I've changed the command in the script to
Code: [Select]
addgroup -G $Group $User
Code: [Select]
tc@afstest:~$ sudo AddUsers2Groups TestUser "admin manager reception opshop shared_drive"
addgroup: invalid option -- 'G'
BusyBox v1.27.2 (2018-03-19 10:57:49 UTC) multi-call binary.

Usage: addgroup [-g GID] [-S] [USER] GROUP

Add a group or add a user to a group

        -g GID  Group id
        -S      Create a system group
addgroup: invalid option -- 'G'
BusyBox v1.27.2 (2018-03-19 10:57:49 UTC) multi-call binary.

Usage: addgroup [-g GID] [-S] [USER] GROUP

Add a group or add a user to a group

        -g GID  Group id
        -S      Create a system group
These errors repeat for the rest of the groups I was trying to add to

I foolishly didn't pay to much attention and just tried
Code: [Select]
addgroup -g $Group $User :-[

Code: [Select]
tc@afstest:~$ sudo AddUsers2Groups TestUser "admin manager reception opshop shared_drive"
addgroup: invalid number 'admin'
addgroup: invalid number 'manager'
addgroup: invalid number 'reception'
addgroup: invalid number 'opshop'
addgroup: invalid number 'shared_drive'

Well of course right ? "addgroup -g" is asking for a group ID isn't it ?

So I tried removing the -g. The script now looks like this

Code: [Select]
#!/bin/sh
# Script for adding user(s) to group(s) by Richard Rost May 14.2018

Groups="All The Kings Men"
Users=$1

SyntaxMessage()
{
        echo -e "Usage: $0 User [\"Group1 Group2 Group3 etc\"]\n"
        echo -e "   Or: $0 \"User1 User2 User3 etc\" [\"Group1 Group2 Group3 etc\"]\n"
}

if [ -z "$Users" ]
        then    # No Users parameter was supplied
        SyntaxMessage
        exit
fi

if [[ ${Users:0:1} == "-" ]] # Test $Users:Character 0:Length 1
        then    # Parameter with a leading dash was passed. Assume it's -h or --help
        SyntaxMessage
        exit
fi

if [ ! -z "$2" ]
        then    # Groups list from command line
        Groups=$2
fi

for User in $Users
        do
        for Group in $Groups
                do
                # ****** Modify the following line for your needs ******
                addgroup $Group $User
                done
        done

This also produced errors

Code: [Select]
tc@afstest:~$ AddUsers2Groups -h
Usage: /usr/bin/AddUsers2Groups User ["Group1 Group2 Group3 etc"]

   Or: /usr/bin/AddUsers2Groups "User1 User2 User3 etc" ["Group1 Group2 Group3 etc"]

tc@afstest:~$ sudo AddUsers2Groups TestUser "admin reception shared_drive"
addgroup: unknown user admin
addgroup: unknown user reception
addgroup: unknown user shared_drive

Turn's out all I had to do was to change the action line of the script from
Code: [Select]
addgroup $Group $Userto
Code: [Select]
addgroup $Users $Group
The script works great like this now.

Thanks a bunch for writing it up. Its cool to have a script that does the same things as installing a bunch of programs :)
Live long and prosper.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Adding a user to multiple groups in one command
« Reply #9 on: May 16, 2018, 10:29:16 PM »
Hi remus
Glad it's working for you now. If you have default groups you always add users to there is a  Groups  variable for that. If you change:
Code: [Select]
Groups="All The Kings Men"to:
Code: [Select]
Groups="admin manager reception opshop shared_drive"Then running this:
Code: [Select]
sudo AddUsers2Groups TestUserwill add  TestUser  to those 5 groups.

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
Re: Adding a user to multiple groups in one command
« Reply #10 on: May 16, 2018, 10:32:30 PM »
Thanks Rich, Thats a handy feature.
Live long and prosper.