0

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

2
  • 1
    Loop through the list, use awk to separate the fields out, use useradd with the -s switch and specify /sbin/nologin. You may also look at the manpage for users for additional switches. Not difficult. Not sure you want to specify the uid unless you are sure that there are no clashes. Commented Aug 18, 2021 at 18:08
  • Bib - Would you mind putting this into a more detailed answer? I agree ... if we could use useradd in a script that would be ideal that way we don't have to increment the UID in the passwd file.
    – 3dalliance
    Commented Aug 19, 2021 at 13:00

1 Answer 1

1

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.

4
  • A very bad example. useradd has all the necessary checks and error handling. No need to write all those custom ifs. I don't see OP wanting to use the system's auto-increment ids if the UIDs already exist.
    – Fanatique
    Commented Aug 19, 2021 at 16:07
  • Good grief... Read his comments regarding uid's. 'if we could use useradd in a script that would be ideal that way we don't have to increment the UID in the passwd file'. And if there are thousands of them, then I think the op would want to limit the amount of info returned or give it in a different format. Really, it's just an example from which the op can build upon. This is the worst thing about stackexchange, some users always want appear superior. Commented Aug 19, 2021 at 16:28
  • Thanks @Bib I created this shell on a non-production machine and ran it with --> useradd -M -N -s because i don't want them to have a home directory. It executed producing this line of text: "User name is testuser1, UID is 1543, 1st name is 1st-name1, last name is last-name1" and with errors ... which looked like the man page of the useradd command. The passwd file was not modified.
    – 3dalliance
    Commented Aug 25, 2021 at 19:10
  • It actually does complete a loop incrementing through my list.
    – 3dalliance
    Commented Aug 26, 2021 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .