2

I need to create multiple thousands of users on a linux system. Currently I use the newusers command with a file as the source for all the users. The file contains around 10000 users. Running

$ newusers /tmp/users.txt

works and creates the users, but it takes very long to complete. The users don't need a shell, a home directory or a password. They also don’t need to be able to login. The users are needed for ssh tunnel purposes.

Is there a way to do this more quick?

  • 2
    Probably not an answer: import them into LDAP. – Jeff Schaller Mar 13 at 20:14
  • 2
    Have you thought about doing it the hard way? Take your user list, add the passwd fields to each user, then append that to the passwd file. You then make their homes using the original file with their list of names, copying /etc/skel over if needed. Absolutely not sure if that is faster than newusers. – number9 Mar 13 at 20:49
  • 2
    How often do you need to add 10000 users if 5 minutes is too long a time? – ilkkachu Mar 13 at 21:52
  • 1
    @p0fi if you need an ssh key for authentication, how is it you think you don't need a home directory? That said, I completely agree that you don't need a skel home directory setup, just /home/username/.ssh/authorized_keys. – Ed Grimm Mar 13 at 22:48
  • 1
    @EdGrimm because the user just needs to exist for a reverse ssh tunnel. The authorized_keys file in the home dir is not needed since the key lookup is done via a AuthorizedKeysCommand in a database – p0fi Mar 13 at 23:08
1

Here's the way I would do it, since they are all local accounts:
1. Determine the range of userIDs
2. Determine the group ID(s) for the users
3. Write a script that will:
a. create a temporary password file filled with the information you need
${username}:x:${userid}:${groupid}::${homedir}:/bin/false
b. create a temporary shadow file with the information you need
${username}:!!:1:0:99999:7:::
4. append the temporary shadow file to /etc/shadow and the temporary password file to /etc/passwd

NB1: I am not sure if the ${homedir} can be blank, /dev/null, or something else
NB2: If you are not using /etc/shadow, replace the :x: in passwd with :!!:

That ought to be fairly quick (however long it takes to create the temp files), quickly update passwd & shadow fileswithout any other password hashing, UID searching, etc.

  • 2
    ${homedir} cannot be blank, but it can be /. The maximum password age can be blank, however, and then there's no password aging. (This should only ever be done if there's no valid password, as in this case.) – Ed Grimm Mar 13 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.