TransWikia.com

Command to list all usernames on a Linux server

Unix & Linux Asked by dac2002 on December 1, 2021

What is the command that allows one to generate a list of all the usernames of authorised users on a Linux server? I have tried

getent passwd | cut -d : -f 1 | xargs groups 

and

less /etc/passwd 

and

grep 'x:0:' /etc/passwd 

and

getent passwd

and

grep "/bin/bash" /etc/passwd | cut -d: -f1

but none of the above gives me exactly what I want, which is simply a list of usernames.

Any help would be greatly appreciated

4 Answers

You have asked for a list of authorized users, and indicated that you mean for this to be a subset of the list of all users, but you have not identified what criteria you want to use to distinguish between authorized users and unauthorized users.

Systems (Unix and Windows) often come with standard, builtin users predefined: root, bin, mail and news in Unix; Administrator, SYSTEM, LOCAL SERVICE and TrustedInstaller on Windows.  On Unix, “real” users (assigned to real people) typically have a GID ≥ 500 and a home directory in /home.  The system users usually fail both these criteria.  So we can list the users in /etc/passwd that meet these criteria with

awk -F: '$6 ~ /^/home/ && $3 >= 500 {print $1}' /etc/passwd

If getent passwd produces users that aren’t in /etc/passwd, you can just hook them up:

getent passwd | awk -F: '$6 ~ /^/home/ && $3 >= 500 {print $1}'

or combine them:

getent passwd | sort -u - /etc/passwd | awk -F: '$6 ~ /^/home/ && $3 >= 500 {print $1}'

Note: This answer is based on an answer (now deleted, 10K only) by Chema.

Answered by G-Man Says 'Reinstate Monica' on December 1, 2021

This will filter users by who has a shell:

getent passwd | egrep  '(/bin/bash)|(/bin/zsh)|(/bin/sh)' | cut -f1 -d:

Answered by 邵传明 on December 1, 2021

awk(1) is another useful tool. awk is able to parse individual fields from lines of text, based on a specified delimiter. In the case of /etc/passwd, that delimiter is a colon, hence we pass '-F:' on the awk command line. Likewise, since the user name is the first field of the line, and awk numbers fields beginning at 1, we stipulate 'print $1' to tell awk to print the first field of each line.

awk -F: '{ print $1 }' < /etc/passwd

This can easily be adapted to print any field from the passwd file. Here is a list of each user's name (field 1), followed by a space, followed by their login shell (field 7):

awk -F: '{ print $1, $7 }' < /etc/passwd

Answered by Jim L. on December 1, 2021

Use your first attempt, but remove the | xargs groups. Really, though, this is basic enough that it's not all that appropriate for this site...

Answered by John on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP