How to Create a Custom Passwd script - LINUX
We have a Centos 7 machine whereby we need to add a lot of NoLogin passwd entries such as:
username:!!:1010:1001:1stName LastName:/home/username/:/sbin/nologin
We have a CSV list of the new users in the following format:
username,UID,1stName,LastName
Please help me find a way to create a user passwd entry that inserts the username,UID, 1stName LastName, Home Directory/username with the nologin suffix Thanks, Robert
Something like...
#!/bin/sh
# example pass.txt contains...
# username,UID,1stName,LastName
# john,1111,dave,smith
# colin,2222,henry,north
# freda,3333,susan,doig
if test $# -eq 1 && test "${1}" = "-u"
then
# Use userid in file
Do_UID_File=1
else
Do_UID_File=0
fi
IFS=,
while read username UIDnum FirstName LastName
do
# echo "User name is ${username}, UID is ${UIDnum}, 1st name is ${FirstName}, last name is ${LastName}"
$(id "${username}" > /dev/null 2>&1)
if test $? -eq 0
then
echo "Failed to add ${username}, UID is ${UIDnum}, 1st name is ${FirstName}, last name is ${LastName} - username already exists"
else
$(id "${UIDnum}" > /dev/null 2>&1)
if test $? -eq 0
then
# UID in use
if test ${Do_UID_File} -eq 0
then
# UID in use and do not use UID in file
useradd -m -N -s /sbin/nologin "${username}" 1
else
# UID in use and do use UID in file
echo "Failed to add ${username}, UID is ${UIDnum}, 1st name is ${FirstName}, last name is ${LastName} - user ID already exists" 2
fi
else
# UID not in use
if test ${Do_UID_File} -eq 0
then
# UID not in use and do not use UID in file
useradd -m -N -s /sbin/nologin "${username}" 3
else
# UID not in use and do use UID in file
useradd -m -N -s /sbin/nologin -u ${UIDnum} "${username}" 4
fi
fi
fi
done < pass.txt
As always, check, check, then check again.