Search all users in linux

How can I list all user names and/or home directories?

But I use it in a script that will be deployed on others’ machines and maybe on those machines they don’t call it home (e.g. myHome). So I want to generalize it to ls -l ~ . But it just lists my user’s home directory instead of all users’ home directories (basically I want to get a list of the users’ names on the machine). How can I generalize it?

There is in fact, no guarantee that all users’ home directories are subdirectories of any one directory.

@hobbs or that they even exist until the user logs in. This is one of those apparently simple issues that gets complex really quickly.

Keep in mind that ~ is generally the equivalent of /home/user , not of /home or /home/* (which seem to be closer to your intention).

@EightBitTony: . nor is there any guarantee that the home directory exists at all. For example, on my Ubuntu install, several system users (including nobody ) have their home directory set to /nonexistent , which, obviously, does not exist. (Of course, those users also have their password hash set to * and their shell set to /usr/sbin/nologin or /bin/false , so they really can’t log in in the normal sense to begin with.)

On the other hand, an (old school) NFS server might serve home directories for users that are not known by name to its OS.

4 Answers 4

Many systems have a getent command to list or query the content of the Name Service databases like passwd , group , services , protocols .

Would list the home directories (the 6 th colon delimited field) of all the users in databases that can be enumerated.

The user name itself is in the first field, so for the list of user names:

(note that it doesn’t mean those users can login to the system or their home directory have been created, but that they are known to the system, they can be translated to a user id).

For databases that can’t be enumerated, you can try and query each possible user id individually:

(here assuming uids stop at 65535 (some systems support more) and a shell that supports zsh’s form of brace expansion). But you wouldn’t want to do that often on systems where the user database is networked (and there’s limited local caching) like LDAP, NIS+, SQL. as that could imply a lot of network traffic (and load on the directory server) to make all those queries.

That also means that if there are several users sharing the same uid, you’ll only get one entry for each uid, so miss the others.

If you don’t have getent , you could resort to perl :

perl -le 'while (@e = getpwent) ' 

for getent passwd ( $e[0] for the user names), or:

for getent passwd with the same caveats.

In shells, you can use ~user to get the home directory of user , but in most shells, that only works for a limited set of user names (the list of allowed characters in user names supported for that ~ expansion operator varies from shell to shell) and with several shells (including bash ), ~$user won’t work (you’d need to resort to eval when the name of the user is stored in a variable there). And you’d still have to find a way to get the list of user names.

Читайте также:  Fern wifi cracker on kali linux

Some shells have builtin support to get that list of usernames.

  • bash : compgen -u would return the list of users in databases that can be enumerated.
  • zsh : the $userdirs associative array maps user names to their home directory (also limited to databases that can be enumerated, but if you do a ~user expansion for a user that is in a non-enumerable database, an entry will be added to $userdirs ). So you can do:

The challenge for the OP will be knowing if getent is going to be there, which I guess to some extent depends on the scope of their ‘others machines’.

@seaturtle, perl -le ‘while (@e = getpwent) ‘ works fine on macOS.

@FloHe, those are user names. Most users on Unix systems are special system users whose life is dedicated to running system services. getent passwd shows you the user database. The first field is the user name, the 6th field is the users home directory.

A better way to list user home directories, is to parse /etc/passwd and extract them from there, and not make any assumptions about where they may be.

And judging by your second comment, you actually want the user names, not their home directories, in which case, /etc/passwd is a better choice anyway. Note that some Linux / UNIX machines will have other user authentication mechanisms configured (e.g. LDAP), and so ultimately, your query is more complex than you might first imagine, but /etc/passwd is a good place to start.

Yes, getent is always better than parsing /etc/passwd (it addresses the concern with “other user authentication mechanisms”).

@Kusalananda The problem with a getent solution is it assumes the data source can be enumerated, and not just queried for a specific value. That’s detailed in Stéphane Chazelas answer to this question, but I though I’d add a comment for anyone skimming over this page.

The short answer:

The medium answer: Since you’re using bash , you can list all possible completions for ~ using compgen -A user . That’s such a common usage, it can be abbreviated compgen -u . As a shell builtin, compgen does not have its own man page. Instead see bash(1) for documentation and read the section on Programmable Completion.

A more thorough alternative

If you’re extremely concerned about portability, you might not even be able to rely on other machines having bash . In that case, do this:

(getent passwd || dscl . -ls /Users dsAttrTypeNative:homeDirectory || nidump passwd || cat /etc/passwd) 2>/dev/null | cut -d: -f6 

Explanation The long answer tries everything, so it will work on pretty much any UNIX system, regardless of whether it uses the newer /etc/nsswitch.conf (both GNU/Linux and BSD come with getent ), the traditional UNIX passwd flat file, MacOS’s Directory Services¹ ( dscl ), or even the older, cat-themed MacOS X releases and NeXTSTEP ( nidump ).

Simplicity But, how portable do you need? Unix has many ways of doing things and sometimes simpler suffices. If you have to pick one, I’d recommend getent passwd | cut -d: -f6 for shell scripts.²

Footnote ¹: I haven’t used MacOS in a bit, so if someone can confirm for me that I got the syntax right (and the output doesn’t include any stray colons that would mess up cut ), that’d be great. Thanks.

Footnote ²: What I recommend and what I do may differ. Personally, I’ll more often use the traditional cut -d: -f1 /etc/passwd at the command line. After decades of repetition, my fingers can type it while my mind is working on other things.

Источник

List all Users in Ubuntu

The Linux administrator can easily interact with the system and manage all users using useful commands in a Linux system.

Ubuntu is the most widely used Linux distribution that offers more useful features for system administrators to list all users and manage their daily tasks. Therefore, the administrator can easily grant privileges to a user on a file or folder and assign limited permission for each user to secure your system’s data.

We will explore in this article how to list all users in the Ubuntu Linux system using a command-line environment. All commands are implemented on the Ubuntu 20.04 LTS focal fossa in this article. Let us dive into the details!

Listing all Users in Ubuntu 20.04 LTS distribution

There are following two methods are available to list all users in the Ubuntu system:

  1. List all users by displaying the /etc/passwd file content
  2. List all users by using the getent Command

Method 1: List all users by displaying the /etc/passwd file content

All local users’ details are stored in the ‘/etc/passwd’ file. Each line of this file contains the login information about one local user. Using two following commands cat and less, the user can view the content of the ‘/etc/passwd’ file:

To display /etc/passwd content using the ‘cat’ command, use the following syntax:

Similarly, you can view the ‘/etc/passwd’ file content using the’ less’ command.

In the /etc/passwd’ file, each line consists of fields separated by a colon. The following important information displays of each user:

  1. The user name represents the login name.
  2. This field represents the encrypted password (x represents the password that is stored in the ‘/etc/shadow’ file).
  3. UID represents the user identification number
  4. GID used to represent the User’s group ID number
  5. User’s full name (GECOS)
  6. User’s main directory or home directory.
  7. It represents the ‘Login shell’ that is set the default to /bin/bash.

Display only user name using Awk and cut commands
Sometimes, you only require print the first field or the user name. In this case, the two useful commands, ‘awk’ and ‘cut’, help you display the first field, which means the user name on the terminal.

In the Ubuntu Linux system, the ‘Awk’ is a powerful text manipulation utility that can easily get the only first field from the /etc/passwd file. The syntax of the ‘awk’ command for displaying the first field is given below:

You can also use the ‘cut’ command for the same purpose as follows:

The above two commands will only display all user names on your terminal window.

Method 2: List all Ubuntu Users using the getent command

The getent command using Linux helps you to collect entries from the administrative database using specified search keys.

In the Ubuntu system, the getent command collects and displays the entries from the database configured in the ‘/etc/nsswitch.conf f’ file. Users can query to list all users by using the passwd database. So, to print the Ubuntu users list, use the ‘getent’ command in the following way:

You can also use the ‘awk’ and ‘cut’ parameters along with the getent command to only display the first field as follows:

Alternatively, to display the same output, you can use the cut command as follows:

Search for an existing Linux User using the getent command
Using the getent command, you can also find whether a user exists on your Ubuntu Linux system or not. To see the Linux user existence, use the getent command along with the grep as follows:

For example, we want to search for a user name ‘samreena’ that exists on our system or not. The above will change into the following form:

If the specified user exists on your Ubuntu system then, it will display the login information of this user. Otherwise, if no user with this name then, it will display no output.

You can also check the user exists or not without using the grep command as follows:

Display total number of Linux user’s accounts
If you want to search for how many numbers of accounts are available on your Ubuntu system then, you can use the ‘getent passwd’ followed by the ‘wc’ command as follows:

How differentiate between Normal and system Users?

A system user usually creates the normal users. The system user creates when you install a new Ubuntu or other Linux operating system. The system user is a root user. You can also create a system user for using particular applications. Whereas the normal users are those created by users who have sudo privileges or a root user. So, each normal and system user has a login account, home directory, and User ID (UID) number that automatically assigns between the range of minimum (UID_MIN) and maximum (UID_MAX) using the ‘/etc/login.defs’ file.

Check the Minimum (UID_MIN) and Maximum (UID_MAX) Limits
Using the following command, you can check the values range for normal users between the UID_MIN and UID_MAX:

The above output shows the normal users have a UID anywhere from 1000 to 60000.

List Normal Users in Ubuntu
The UID_MIN and UID_MAX help us to list the normal users in our system. So, UID ranges allow you to query the list of the normal user on your Linux system as follows:

Conclusion

You learned in this article how to list all Ubuntu or Linux users using the command line application. We investigated how to search for a specific user and the difference between normal vs. system users. Moreover, we discussed how to list normal using the UID ranges. The above commands can also apply to other Linux distributions such as CentOS, Debian, and LinuxMint for listing users.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.

Источник

Оцените статью
Adblock
detector