3

I googled a bit for this (maybe incorrect keywords), but I didn't find how to do that.

Consider the following commands:

useradd -s /bin/bash -m user1
useradd -s /bin/bash -m user2
useradd -s /bin/bash -m user3

I want any new user created this way to also be added to a group called test, but without needing to specify it each time I run useradd. I want this to happen by default.

I mean after creating user1, when I run grep user1 /etc/group, it should be in two groups: test and user1.

Is that possible? I am using CentOS.

10
  • 1
    well, you do need to change the command in order to add additional groups. per the man pages, use the -G option to list additional groups eg: useradd -s /bin/bash -m user1 -G test linux.die.net/man/8/useradd Commented Feb 27, 2023 at 18:22
  • 2
    "I want to add all of these users to a group called test but I do not want to change the above command." – Please make sure there is no XY problem here. Why cannot you adjust the commands? or use few extra commands? Commented Feb 27, 2023 at 18:23
  • @FrankThomas I just want to know for curiosity. It's not a question for a production environment.
    – Saeed
    Commented Feb 27, 2023 at 18:26
  • 1
    Unix does not work this way. you need to change your command line (with -G) or add additional commands to commit users to group. your only "default" group is your primary group which matches the user name. Commented Feb 27, 2023 at 18:31
  • 1
    What Linux are you asking about? Do you also have adduser available? Also, if you want to know how to make this happen by default, you need to mention that in your question, so please edit and explain exactly what you need.
    – terdon
    Commented Feb 27, 2023 at 18:45

2 Answers 2

5

Yes, you can set a default action to be carried out after the new user is created. This is, admittedly rather obliquely, documented in man useradd near the end, in the FILES section:

/etc/shadow-maint/useradd-pre.d/, /etc/shadow-maint/useradd-post.d/

Run-part files to execute during user addition. The environment variable ACTION will be populated with useradd and SUBJECT with the username. useradd-pre.d will be executed prior to any user addition. useradd-post.d will execute after user addition. If a script exits non-zero then execution will terminate.

This means that any scripts placed in the /etc/shadow-maint/useradd-post.d/ directory will be executed after a new user is created. In those scripts, any use of the environment variable $SUBJECT will be replaced with the user name of the newly created used. Now, run-parts is a bit strange and requires very specific naming schemes. However, in your case, since all you want is that one script, it should be enough to name it something like 01groups. So, first, create the directories:

sudo mkdir -p /etc/shadow-maint/useradd-post.d/

Next, create a text file with the following contents:

#!/bin/sh
usermod -a -G test "$SUBJECT"

Make sure the test group exists. If it doesn't, create it first with sudo groupadd test.

Finally, save that file as /etc/shadow-maint/useradd-post.d/01groups and make it executable:

sudo chmod a+x /etc/shadow-maint/useradd-post.d/01groups

And that's it. If you now create a new user, that user will also be added to your extra group. For example, I tested it on my system using this command:

sudo useradd -d /home/bib1 -m -s /bin/bash bib1

And then:

$ grep bib1 /etc/group
test:x:1003:bib1
bib1:x:1004:

As you can see, the new user was automatically added to the extra test group. Note that this will now happen for all users added to the system by useradd. It's probably not the best idea.

0

I don't know why you want to do this but this is one way. I think the easiest way is to extend the $PATH variable. So in bash you do it like this (please replace the path):

export PATH="/home/user1/myscripts:$PATH"

Put it at the end of .bashrc or .bash_profile.

Then place a script called useradd inside the myscripts folder. You have to write a short script which does what you want. Call the original useradd binary by using an absolute path.

Another way is to replace the original useradd binary by a bash script and rename the original. You can run

which useradd 

to see where it is. But your system might get unstable. I recommand using the first way.

1
  • 1
    This isn't really answering the question. You are basically saying "it is possible to write something that can do it, but I am not showing you how". A script actually demonstrating how this would work would be an answer, but as it stands, you just have a diversion into the PATH variable and a statement that it would be possible.
    – terdon
    Commented Feb 27, 2023 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .