I would like to create some new users on the system with a different naming schema:
- old:
firstname+initial of the second name(eg :johnk) - new:
firstname.surname(eg :john.kennedy)
I am stuck with the password change for the new users. Ideally I would like to pull the shadow password from the file but I am still learning the BASH scripting and I think it’s too advanced , so for now i have just created a list with old users and the corresponding shadow passwords.
I was thinking to update the new user passwords with: /usr/sbin/usermod $u -p "$HASH" but I’m not sure how to list and match the old users to new users for this command.
#!/usr/bin/env bash
OLD="
johnk="shadow_hash"
...
"
NEWUSERS="
john.kennedy
...
"
for u in $NEWUSERS; do
if [ -z "$(getent passwd $u)" ]; then
echo "User $u does NOT exist, creating..."
/usr/sbin/useradd $u
# /usr/sbin/usermod $u -p "$HASH"
else
echo "User $un DOES exist."
fi
done
Do you really want to create new users, or just add a login alias for an existing user? (Or, even simpler, change the user name?)
If you create a new user, then you’ll need to fix a lot of file permissions, which could be annoying.
You also have to deal with the name of the home directory, which is probably related to the old user name. You could just create a symlink from the new name to the old name (or rename the directory and symlink the old name to the new name; but make sure they’re not doing anything with their files while you do that).
In any event, however you do it, you can use the numerical user id to relate the two names. For an alias, you would create the new name with the same numerical user id (you need to specify a flag to usermod to allow this to work) and you still need to deal with the name of the home directory and possibly other names, but you don’t need to fix permissions. If you don’t want to create aliases, you could create all the new user names with numerical user ids at a fixed offset from the old one (like 200 higher, for some value of 200). Note that
getentallows lookup by either user name or numerical user id.