Posted: . At: 2:59 PM. This was 1 year ago. Post ID: 17243
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


List users in the /etc/passwd file with awk very easily.


Listing users in the /etc/passwd file is very easy, it is possible to list users by the shell that they use.

Listing all users in the /etc/passwd file with an ID greater than 1000 and using the /usr/local/cpanel/bin/jailshell.

(base) jason@jason-Lenovo-H50-55:~/Documents$ awk -F: '$3 > 1000 && $7 == "/usr/local/cpanel/bin/jailshell" {print $1}' passwd 
contentstrategy
golfhous
inturac
inturactive
prolink2
rvadmin
saascommunity
taggg
vianews
saasinvaders

Print all users with an ID greater than 999 that have the bash shell set in their entry.

(base) jason@jason-Lenovo-H50-55:~/Documents$ awk -F: '$3 > 999 && $7 == "/bin/bash" {print $1, $7, $6}' /etc/passwd
jason /bin/bash /home/jason
cudauser /bin/bash /home/cudauser
mike /bin/bash /home/mike

Print some information about the users in the /etc/passwd file.

(base) jason@jason-Lenovo-H50-55:~/Documents$ awk -F: '$3 > 999 && $7 == "/bin/bash" {print $1, $7, $6, $3,"-",$4}' /etc/passwd
jason /bin/bash /home/jason 1000 - 1000
cudauser /bin/bash /home/cudauser 1001 - 1001
mike /bin/bash /home/mike 1002 - 1002

This prints the username, the shell, home directory and group and user ID #.

This should be very useful to get a listing of users on your Linux system.

To list all users in the /etc/passwd file that have a UID number over 995, use this C code.

passwd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
/********************************************************************
* Description: List all usernames and home directories in the /etc/passwd
* file.
* Author: jason,,, <>
* Created at: Fri Dec 30 08:35:50 AEDT 2022
* Computer: jason-Lenovo-H50-55
* System: Linux 5.4.0-135-generic on x86_64
*
* Copyright (c) 2022 jason,,,  All rights reserved.
*
********************************************************************/
 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <pwd.h>
 
int main(void) {
    struct passwd *pwd;
    char num[] = {51, 57, 53, '\0'};
    int foome = atoi(num);
  /* Open the /etc/passwd file */
  setpwent();
  /* Iterate over the records in the file */
  while((pwd = getpwent()) != NULL) {
        if (pwd->pw_uid > foome && strcmp(pwd->pw_dir,"/")) {
                /* Print the username and home directory
                 * If the user ID is over 995.
                */
                printf("Username: %s\nHome directory: %s\n", pwd->pw_name, pwd->pw_dir);
        }
  }
  /* Close the file */
  endpwent();
  return 0;
}
Compile it like this: (base) jason@jason-Lenovo-H50-55:~/Documents$ gcc -Wall passwd.c -o passwd

This is what the output looks like.

(base) jason@jason-Lenovo-H50-55:~/Documents$ ./passwd 
Username: nobody
Home directory: /nonexistent
Username: jason
Home directory: /home/jason
Username: systemd-coredump
Home directory: /
Username: cudauser
Home directory: /home/cudauser
Username: mike
Home directory: /home/mike


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.