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.
useradd -s /bin/bash -m user1 -G test
linux.die.net/man/8/useraddadduser
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.