Tiny Core Linux
General TC => Programming & Scripting - Unofficial => Topic started by: remus 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
sudo addgroup User1 Group1
sudo addgroup User1 Group2
sudo addgroup User1 Group3
sudo addgroup User1 Group4
sudo addgroup User1 Group_drive5
Thx
-
Hi remus,
might this work?
https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/ (https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/)
See the section 'usermod' ....
best regards
xyz-worx
-
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:
#!/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:
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:
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:
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
-
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.
-
Thanks Rich.
I'll try this out asap.
-
Hi remus
Just modify this line at the end of the script to meet your needs:
echo "addgroup $User $Group"
Remove the echo and the quotes.
[EDIT]: Corrected syntax. Replaced all instances of adduser with addgroup. Rich
-
Hi Rich,
Have just experimented with mc9.0 and can report a little problem.
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
$ 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.
-
Hi remus
Sorry, my screwup. In you original post you correctly listed using addgroup which I promptly misread as adduser. :-[
That line should read:
addgroup $User $Group
I will correct my other posts to reflect this.
-
Hi Rich
Further testing -
I've changed the command in the script to
addgroup -G $Group $User
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
addgroup -g $Group $User
:-[
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
#!/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
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
addgroup $Group $User
to
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 :)
-
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:
Groups="All The Kings Men"
to:
Groups="admin manager reception opshop shared_drive"
Then running this:
sudo AddUsers2Groups TestUser
will add TestUser to those 5 groups.
-
Thanks Rich, Thats a handy feature.