1

Can a who command be integrated to find the current logged in users password file entry?

  • 1
    While it might not be available everywhere, users might be a good alternative to who here, as it provides less information (you don't actually need more than usernames). – John WH Smith Mar 10 '15 at 22:53
2

Yes, you can make a script like this:

for who_user in $(who|cut -f1 -d' '|uniq)
do
    getent passwd|grep ${who_user}
done

If you have enough permission you could look for the password at your shadow file like this:

for who_user in $(who|cut -f1 -d' '|uniq)
do
    cat /etc/passwd|grep ${who_user}
    cat /etc/shadow|grep ${who_user}
done

As mentioned by @John WH Smith no need of grep:

for who_user in $(who|cut -f1 -d' '|uniq)
do
    getent passwd ${who_user}
done

Thanks John

  • 2
    Note: getent passwd can be passed non-numeric keys in order to lookup specific usernames. You don't need to grep: getent passwd ${who_user}. See getent(1) (your edit however is yet another useless use of cat: grep can handle filenames as arguments ; no need for the shadow search either) – John WH Smith Mar 10 '15 at 22:50
0

Maybe something like this:

who | awk '{print $1}' | grep -f - /etc/passwd

awk '{print $1}' will output the usernames, which is passed to grep using the -f - option telling it to take the grep pattern from stdin. This will print out the /etc/passwd entry of the logged in users:

aliaa@host:~$ who
aliaa    tty7         2015-03-10 13:02
root     tty1         2015-03-10 18:19
aliaa    pts/0        2015-03-10 14:39 (:0)
aliaa@host:~$ who | awk '{print $1}' | grep -f - /etc/passwd
root:x:0:0:root:/root:/bin/bash
aliaa:x:1000:1001:Alaa Ali,,,:/home/aliaa:/bin/bash

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.