Batch process creating new users
Q I have been running CentOS since version 3 in the school where I work. I have input 500 pupils into the system one by one. I have set up a domain logon with Samba. Is there any way to bulk input so many user accounts in one go?
A Of course, there is, this is one of the areas in which textual command line tools excel, in being able to use data from one file or program in another. Are you trying to create system users or Samba users, or both? Use the useradd command to create system users and smbpasswd to create Samba users. Both of these commands can be used in a script to read a list of user names from a file. Put your new user names in a file called newusers, one per line, like this
username1 password1 real name1 username2 password2 real name 2 ...
You can create this file manually or extract the information from another source, such as using sed or awk on an existing file or running an SQL query to pull the information from your pupil database. Then run this to add them as system users.
cat newusers | while read u p n do useradd --comment "$n" --password $(pwcrypt $p) --create-home $u done
You may need to install pwcrypt before you can do this, it is a command line interface to the system crypt() function. You could use the crypt command from the older cli-crypt package, but pwcrypt is more up to date and considered a better option nowadays. If you want to force the user to change their password immediately, which is generally a good idea, add
passwd -e $u
after the useradd command. This expires their password, so they need to change it when they next login. To set the Samba passwords, use smbpasswd like this
echo -e "$p\n$p" | smbpasswd -as $u
The -s option tells smbpasswd to accept the password from standard input instead of prompting for it, the echo command is in that format because smbpasswd still requires the password to be given twice when used like this. So all you need to add the users to both the system and Samba is
cat newusers | while read u p n do useradd --comment "$n" --password $(pwcrypt $p) --create-home $u passwd -e $u echo -e "$p\n$p" | smbpasswd -as $u done
You must add the users to the system password file before trying to add them to Samba, or smbpasswd will complain. Similarly, when you delete users, run smbpasswd -x username before userdel username.
- Creating a backup email server on RHEL 4
- Restrict SSH users: limit them to their own directory
- Creating a CD duplicator
- Safe for users to upload PHP scripts?
- Letting users edit pages on a CMS