In my environment I constantly have to attach an NFS server running on Linux. The Linux client system has user names created in it and they are in a sequential order. But in order to set the sharing permission for each user individually, I have to create them using useradd -u 2001 -g 1000 -d /home/app/mnt/1 user1 in my NFS server. If my client system has 1000 users I have to individually crate them on my NFS server too.

I want to create multiple user names and assign an UID (not random) using shell script in my Linux NFS server. I also want to assign them a home directory automatically. For example I want to create users like

| Username| UID |Home Directory|
|  user1  |20001|/home/users/1 |
|  user2  |20002|/home/users/2 |
|  user3  |20003|/home/users/3 |
|    .    |  .  |       .      |
|    .    |  .  |       .      |
|    .    |  .  |       .      |
|  userX  |2000X|/home/users/X |

User name always starts with "user", it remains the same. X could be 100 to 1000.

These users are necessary but short lived hence I don't want to go for a central user management platform. Is it possible that a shell script can create users in bulk?

I went through this article. This starts with exactly what I want, but later goes into things which makes no sense to me.

  • 1
    "necessary but short lived" To me, that sounds like an excellent reason to use a user database other than files—a lot of them are far easier to interact with programmatically (e.g., your favorite programming/scripting language has LDAP and SQL interfaces), and you don't have to worry about syncing between systems. – derobert Jun 1 '16 at 19:54

As suggested, this is fairly easy to accomplish. On the terminal you could use a for loop like this:

for i in {20001..20100}; do useradd -u $i -g 1000 -d /home/app/mnt/$i "user${i}"; done

The loop is incremented up to 20100 and then it quits.

| improve this answer | |

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.