Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to grep all the logged in users from a user list in Linux (Ubuntu) ?

So far I got to:

cat /etc/passwd | grep "/home" | cut -d: -f1
share|improve this question

migrated from stackoverflow.com May 12 '13 at 12:37

This question came from our site for professional and enthusiast programmers.

3  
Take a look at man who and man w. –  NPE May 12 '13 at 11:15

2 Answers 2

up vote 4 down vote accepted

as commented above w or who shows you who is logged in..

You could also look at historical/current data looking at lastlog

and lastb for last bad logins.

w|grep pts|awk '{print $1}'



for ids in $(w|grep pts|awk '{print $1"_"$2}'); do id=${ids%%_*}; pts=${ids##*_}; actualperson=$(getent passwd $id|awk '{print $5}'); echo "Username: $id  is $actualperson and is logged into $pts";  done;

Username: xxx is and is logged into pts/0

Username: xxx is and is logged into pts/5

 for ids in $(w|grep pts|awk '{print $1"_"$2}'); do 
   id=${ids%%_*};
   pts=${ids##*_}; 
   actualperson=$(getent passwd $id|awk '{print $5}'); 
   # echo to your console the persons details
   echo "Username: $id  is $actualperson and is logged into $pts"; 
   # Send a message to person logged in telling them you know their logged in
   echo "I know your logged in $actualperson"|tee /dev/$pts 2>&1>/dev/null;  
 done;
share|improve this answer

The users command will probably be helpful. From the man page:

users  -  print the user names of users currently logged in to the current host
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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