2

On AIX 7, I am trying to craft a command that would check the user accounts on my system that have never logged in and to check if they are locked. If a user account that has never logged in exists and is not in the locked state, to output this information.

Also, I am trying to only query the user accounts with UID greater than 100.

I've successfully crafted a command that will check the list of users with UID greater than 100 but I am now struggling to craft the command that will check the lastlogon/locked state:

cat /etc/passwd | awk -F: '$3 > 100 { print $1 }'

I have tried to experiment with the "lsuser -a account_locked " & "lsuser -a time_last_login " commands, but this has been unsuccessful.

Can anyone help me, please?

2
  • What you try with lsuser? Jun 9, 2021 at 15:41
  • Hi @RomeoNinov, I tried to create a loop that would insert the users listed into the lsuser command to check the information I have listed above in my query. However, I failed to do so, do you know how to do this type of check? Not sure if lsuer is the right approach?
    – Help
    Jun 9, 2021 at 16:45

2 Answers 2

2

I would adapt a previous solution into:

lsuser -C -a account_locked time_last_login id ALL | awk -F: '$4 > 100'

This gives you colon-delimited output of the form:

#name:account_locked:time_last_login:id
abcdef:false:1517991441:1594
ghijkl:false:1466418488:3430
mnopqr:false:1437508829:1081
stuvwx:false::2393
yz:false::2616

Empty fields indicate no value (such as for the "account_locked" field in the last two). I left the header line so that could see the correlation with the data, but if you want to skip that, adjust the awk script to skip the first line:

lsuser -C -a account_locked time_last_login id ALL | awk -F: 'NR > 1 && $4 > 100'

If you don't want the UIDs in the report, adjust the awk statement to print only the first three fields:

lsuser -C -a account_locked time_last_login id ALL | awk -F: '$4 > 100 { print $1":"$2":"$3 }'

To finally answer the question of which accounts are not locked and have never logged in:

lsuser -C -a account_locked time_last_login id ALL | awk -F: '$4 > 100 && $2 == "false" && $3 == "" { print $1 }'

If you really wanted to start from the existing command, you could use it to generate usernames as arguments for lsuser and then use the same awk filters as above:

for username in $(awk -F: '$3 > 100 { print $1 }' < /etc/passwd)
do
  lsuser -C -a account_locked time_last_login "$username" | awk -F: '$2 == "false" && $3 == "" { print $1 }'
done

AIX usernames cannot contain spaces, tabs, or newlines, so as long as your IFS variable is set sanely, there's no risk of accidental splitting.

0
2

You can use this command to get list of all locked accounts:

lsuser -a account_locked ALL | grep '=true$' | awk '{ print $1 }'

With this command you can see the last login of all users and filter those never logged

lsuser -a time_last_login ALL|awk -F"[= ]" '{ if(int($3)==0) print $1,"never"; else print $1,$3;}'

For print only never logged users you can simplify the command:

lsuser -a time_last_login ALL|awk -F"[= ]" '{ if(int($3)==0) print $1}'

To get the list of both not logged and locked you should redirect the result from both commands and exec:

comm -13 file.locked file.notlogged

So the script should be something like:

lsuser -a account_locked ALL | grep '=true$' | awk '{ print $1 }' >file.locked
lsuser -a time_last_login ALL|awk -F"[= ]" '{ if(int($3)==0) print $1}'>file.notlogged
comm -13 file.locked file.notlogged
6
  • Hi Romeo, thank you for the feedback, when I tried to run the command to see the users that never logged in and are not locked out, I got this error: syntax error The source line is 1. The error context is { if(int($3)==0) print $1,"never" >>> else <<< print $1,$3} awk: The statement cannot be correctly parsed. The source line is 1.
    – Help
    Jun 10, 2021 at 8:45
  • 1
    @Help, I edit it, should work now Jun 10, 2021 at 9:23
  • the command runs fine now, thank you. Is there a way to filter the output so that it only displays user accounts that never logged in and are not in the locked state?
    – Help
    Jun 10, 2021 at 9:52
  • @help, i edit the answer, please check if this work for you. Jun 10, 2021 at 10:05
  • 1
    @help, i add sample script Jun 10, 2021 at 13:39

You must log in to answer this question.

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