How can I query all users on a box and force password expiration?
Currently, I am querying all users:
getent shadow | awk -F: '$2 ~ /^\$/ || $2 ~ /^!\$/ {print $1} {print $3}'
And this gets me the user name as well as the last password change, but I only need to force the passwd -e
on users who haven't changed their password since before March 1, 2022 - anyone who has changed their password after March 1, 2022 I can leave those alone (I believe this would be a value of 19052 - so any value greater than or equal to that I can skip).
I like the approach @KamilCuk took. To add to that I would include a minimum
UID
and maximumUID
on parsing/etc/passwd
to exclude system accounts. (note: some distributions start the first non-system UID at differing values, usually either500
or1000
-- check your distro). The maximumUID
can exclude generic user accounts placed at the top of the range like thenobody
account on openSUSE withUID == 65534
To determine whether to expire an account with a password change older that March 1, 2022, it is fairly easy to convert that date and the date returned by
chage
to seconds-since-Epoch. That way you can use a simple comparison of if the last password change is less than the number of seconds since Epoch for March 1, 2022 -- expire the account.Below is one approach you can take to put it all together.
xargs
is another option to build the list instead of expiring accounts one-by-one. The actual expiration is commented out below and instead the command that would be run is printed tostdout
to allow testing before actual expiration of accounts.(note: you can use Process Substitution to feed the
while
loop in bash rather than piping the results to it -- up to you. If you are stuck with POSIX shell, then piping will work in both instances)When satisfied, uncomment the final line in the loop, and optionally remove the line that simply outputs the command that would be run.