5

I'm am currently trying to figure out a command that will show the last password change of a user on a UNIX system.

I have tried running the command passwd -Sa and it gives the password change date formatted in 00/00/0000. However I want to only list the date of the last user from that command. I have tried piping the command to grep, but I can't seem to get my syntax correct.

I'm looking for just the date output as the result of this command.

Current command attempted:

grep $USER | passwd -Sa

Any help on my errors is appreciated!

4

Giving more information gets better answers. Offhand, on OpenSUSE, I see that

passwd -S -a

gives a record for each entry in /etc/passwd, like this:

thomas PS 03/01/2013 0 99999 7 -1

so you could filter that with awk:

passwd -S -a | awk -v user=$USER '{ if ( $1 == user ) { printf  "%s\n",  $3; } }'

However, the passwd command wants to do this as root, so your script would have to do something more elegant than using the $USER variable (that's for the current user). The reason it wants root is that it is reading the contents of /etc/shadow, which is not public.

You could use chage, which shows a different report — but in a quick check, it prompts for the user's password (which probably is not what you want):

The chage program requires a shadow password file to be available.

The chage command is restricted to the root user, except for the -l option, which may be used by an unprivileged user to determine when his/her password or account is due to expire.

Further reading:

|improve this answer|||||
  • Great read, and excellent help, solved my issue nicely. Sometimes it's just easier when someone is able to make sense of the man pages. Just a quick question too, is there a way of reading passwd as anything other than root, or will passwd only read with root access? – Mister123 Mar 27 '16 at 22:20
  • It's only as root because it is reading the contents of /etc/shadow (which is readable only by root). I seem to recall a different way for current user, may update the answer to note that... – Thomas Dickey Mar 27 '16 at 22:28
1

This sounds like a homework exercise, so:

  • sort can sort input numerically, and can sort by multiple defined columns if necessary.
  • cut can print a subset of the input columns, using a set of characters to define column separators.
  • a | b sends the output of command a to command b.
  • head and tail return the first and last lines of a file, respectively.
  • You generally can't inquire about other users without being root (that is, using sudo). You can avoid that (and any grepping) by simply using passwd --status to get the status of the current user.
  • If you want to make sure that users change their password regularly I'm sure there are already finished solutions out there, since Linux distros have had this functionality for many years.
|improve this answer|||||
  • It's not a homework, this is part of a openSUSE server that I'm currently configuring. I'm having a list of commands neatly formatted that show for each user on the system as they login, the last password change I just can't figure out and sort properly. – Mister123 Mar 27 '16 at 20:43
  • @Mister123 well your first problem is that your grep $USER | passwd -Sa is backwards. passwd -Sa | grep $USER is at least the right way around: passwd -Sa outputs some data and grep $USER selects what concerns $USER from that data. – Law29 Mar 27 '16 at 22:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.