Marwan Alsabbagh Marwan Alsabbagh - 1 year ago 54
Linux Question

Linux server list users who can login

I would like either a one-liner shell command or a short Bash/Python script that will list all the users on a Linux server that can login to that machine. So users like www-data and mail would be excluded of course, but

would be part of the list.

This code, for example, works. I found it here. But I'm looking for something simpler cleaner.

awk -F: '$2 != "*" && $2 !~ /^!/ { print $1, " can log in" }' /etc/shadow

Answer Source

You can use this oneliner to list all users that have a password set in /etc/shadow, one per line:

getent shadow | egrep '^[^:]*:[*!]:' -v | cut -f1 -d:

This only works for local users and not when using an LDAP database or other methods.

If you are using LDAP and you want to list all users in the directory you can try:

getent passwd | cut -f1 -d:

Although, this will list all users, including 'non-user' accounts, such as www-data

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download