3

I want to check for all users who do not have a password set.

For example, a user with the following entry in /etc/passwd

test01::15164: ...* 

has no password set. I would like to list all such users.

declare -a liste=(test 01 test02)
for a in `echo ${liste[@]}`
do
    for b in `cat /etc/shadow`
    do
        if [[ "$a" == "$b" ]]
        then
            echo $a
        fi
    done
done

The code doesn't give any output or errors

Solution

The entry in the shadow file witout a password are usertest01 & tecusertest01

declare -a liste=(usertest01 usertest 02 tecusertest01 tecusertest02)

for sdw in `awk -F: '$2 == ""' /etc/shadow`

        do

                user=`echo "$sdw" | awk -F: '{print $1}'`

                echo "$user"

                echo  "${liste[@]}" | grep "$user" > /dev/null

                if [ $? -eq 0 ]
                        then

                                echo "Human user"

                                echo "$user"  | passwd "$user" --stdin

                        else

                                echo "Technical User"

                fi

        done

Output

usertest01
Human user
usertest01
Changing password for user usertest01.
passwd: all authentication tokens updated successfully.
tecusertest01
Technical User
6

You can simply check using :

Note:- Run following command using sudo, because /etc/shadow has only read access to root user & group.

awk -F: '$2 == ""' /etc/shadow

OR

awk -F: 'length($2) == 0' /etc/shadow

Using Bash

#!/usr/bin/env bash

set -f  # do not expand * 

while IFS=":" read -a user
do 
    [[ -z ${user[1]} ]] && 
    echo "User : ${user[0]} has no password"

done < /etc/shadow 

Update 1:

Can i output the users or give them a temporary password?

Above Bash Script is already giving output usernames.

you can setup temporary password for user using :

echo "username:password" | chpasswd 

Then you can forcefully tell your uses to change password at first log-in :

chage -d 0 username

Update 2: Some information to check 2nd filed based on, because OP mention that second filed is blank but this is not possible unless it's edited by hand.

/etc/passwd file 2nd field denote x means password is in /etc/shadow file, and in

/etc/shadow 2nd field has :

  • !{ecrypted passwd} == user is locked
  • * or ! == * is mostly for system/daemon account. the user will not be able to use a unix password to log in (but the user may log in the system by other means).
3
  • Or, in the absence of root access, you can check $2 in /etc/passwd.
    – Joseph R.
    Jan 9 '14 at 12:19
  • How do i use this in a script? Can i output the users or give them a temporary password? Thanks for your help
    – BlueFox
    Jan 9 '14 at 12:31
  • 1
    @BlueFox Please once check updated answer and let me know if you have any doubts Jan 9 '14 at 12:43

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.