There are plenty of people that already answered to the question "how to execute useradd properly in a script?"

My question is: "is it possible to tell to useradd to execute one script when it is called?"

I am writing a script to push identities files (ssh-keys) in an HPC cluster when a user is created, but I think the "cleanest" way would be to write a script that is executed by useradd.

The script should do something like this:

# sshpush.sh
# a script to SSH without passwords around the cluster nodes

mkdir $HOME/.ssh
cd $HOME/.ssh

ssh-keygen -q

cat id_rsa.pub >> authorized_keys

cat > config <<EOF
StrictHostKeyChecking no
FallBackToRsh no
BatchMode yes
ConnectionAttempts 5
UsePrivilegedPort no
Compression no
Cipher blowfish
CheckHostIP no

cpush /etc/passwd
cpush /etc/shadow
cpush /etc/groups

notice that the /home directory is mounted as NFS on all nodes.

Ideally, the script should be executed at the end of the user creation process, after that $HOMEis created and that the user as a valid group and id.

3 Answers 3


It looks that that isn't an option here (Fedora 19, shadow-utils- It sounds like a very useful extension, though. Please report this as a request for enhancements in your distribution's bugtracker (or where that should go).

  • mmmh, It seems like it isn't. I have looked in the source code of Useradd and I don't see anything like that. This is sad. Maybe I try to write it once I got a few hours...
    – Danduk82
    Feb 7, 2014 at 8:26
  • Does the skel directory (files to add to a new account) help?
    – vonbrand
    Feb 7, 2014 at 17:20

I don't know if useradd can call another script and execute it with sudo privileges, but you could add a script under /etc/profile.d/ to run only the first time a newly created user logs in. Again, this is a work-around and I am not sure if this script can be run as sudo.


I haven't done a lot of testing with your case, but sometimes you can use alias command= to change what command does. Try it with ls first and play around with it to learn how aliasing can work. When you're done, just say unalias ls and the shell builtin will resume working.

You must log in to answer this question.

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