Linux get all users list

3 Ways to List Users in Linux

As a sysadmin, you’ll often need to list all the users on your Linux system. Learn how to list all users in the Linux command line.

Today different Operating Systems have the capability to use multiple users, each one with their settings and custom configurations to make things easier for administrators and operators to work in together on the same system.

Linux on the other hand is very strong on this matter as it allows multiple users to work at the same time on the system in an independent way. It can even allow a single user to open several sessions even from different locations in order to work on the system.

Here are some hints & tricks to handle users in Linux.

List all the users on Linux

Let’s say you want to create a sudo user in Linux. Probably, the very first thing to know is how to know what users are in my system. There are several ways you can obtain the list of users in Linux.

1. Show users in Linux using less /etc/passwd

This command allows sysops to list the the users that are locally stored in the system. It will give the listing in structured way as:

root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin johndoe:x:1000:1000:John Doe. /home/helder:/bin/bash davmail:x:127:65534::/var/lib/davmail:/usr/sbin/nologin statd:x:128:65534::/var/lib/nfs:/usr/sbin/nologin /etc/passwd (END)

The structure in the above output goes as:

  • User name
  • Encrypted password ( x represents password is stored)
  • User ID number (UID)
  • User’s group ID number (GID)
  • Full name
  • User’s home directory
  • User’s login shell (default is bash shell)

Why so many users? Which ones are ‘real’?

The list shows a lot more users than you expected because it lists all the system users too.

Now if you want to distinguish the normal users from the system users, you can refer to the User identifier (UID) number.

Generally, a normal user has UID greater or equal to 1000. This gives you a hint that the user with UID >=1000 is a normal user and users with UID

You’ll also notice that some of the users have ‘nologin’ at the end of their line. This means that these users cannot login to the system. These users are also referred as pseudo-users.

2. View users using getent passwd

This command will give you a similar output as “less /etc/passwd” however, this one actually queries the GNU Name Service Switch functionality configuration file (located at /etc/nsswitch.conf).

This conf includes passwd, so that’s why it will display very similar but if you use LDAP for authentication it will include that as well.

Читайте также:  Linux создать файл большого размера

getent passwd command lists the users

3. List Linux users with compgen

If you just want to list all the usernames without any additional information, you can use the compgen command with -u option.

The output would be like this:

compgen -u root daemon bin sys sync games man lp mail news uucp proxy www-data backup list irc gnats nobody systemd-network systemd-resolve syslog messagebus _apt uuidd avahi-autoipd usbmux dnsmasq rtkit cups-pk-helper speech-dispatcher whoopsie kernoops saned pulse avahi colord hplip geoclue gnome-initial-setup gdm abhishek 

You can use compgen command with -c option to list all the commands available to you. This is helpful when you are not the admin on a Linux system and don’t have sudo access.

A few tips about listing users in Linux

You just saw three ways to view users in Linux. Here are a few tips to help you with the users listing.

List only the usernames

You already have the compgen command for that but you don’t have to remember it all the time.

If you would like to only get a list of the usernames in the system, you can use the awk command or the cut command to filter the output of the other two commands we saw earlier.

Any of these will give us a filtered list of users, showing only the very first column which is username:

root daemon bin sys sync games man lp mail news johndoe davmail statd

Check if a username already exists in the system

This might be useful if you want to know if a particular username already exists in the system:

getent passwd | grep johndoe
johndoe:x:1000:1000:John Doe. /home/johndoe:/bin/bash

List all the connected users

If you want to know what users are currently logged into your Linux system, then you need to use a simple who command and this will immediately list current usernames with an active session to your system

[email protected]:~$ who johndoe :0 2019-01-28 21:35 (:0) harrysmith pts/0 2019-02-01 09:51 (192.168.1.1) stevejones pts/1 2019-02-02 09:51 (192.168.1.173)

In this case, the listing will give you not only the list of usernames connected but also how they are connected, since when they are connected and from where they are connected.

The very first column will tell you what username is it.

The second column will give you what type of connection it is: if it’s represented with a “:X” where X is a number, it means it is using a Graphical User Interface (GUI) or Desktop session such as Gnome, XDE, etc; if it says “pts/X” where X is a number, it means it’s a connection made through SSH protocol (command line).

The third column will tell you since when this session has been connected to the server (date and time). The fourth and last column will give you the location from where it’s connected, if remote it will display the IP address from where the connection is made if local (like the GUI) it will display “(:X)” where X is the number of the session in this case and will match the number in the second column for that row.

Wrapping up

As you can see, listing users in Linux is not difficult at all. It consists of simple commands which will output all the information for you, whatever you want to do or obtain of that information is something you need to filter depending on what you want to check on the system.

Читайте также:  Не загружается alt linux

For example, if you want to list users in a group in Linux, you can do that as well. In a related topic, you may also read about changing users in Linux command line.

I hope you liked this tutorial. Please let us know in the comments if you have any questions or suggestions.

Источник

How to List Users in Linux

User management is a critical Linux system administration task. In large organizations, having insight into who has access to the system is crucial to correctly add users, remove users, and assign new user privileges.

This tutorial will show you how to list users on a Linux-based system. The guide provides four listing methods and explains essential concepts related to user administration.

How to list users in Linux.

Listing Users in Linux

Linux stores information about local users in the /etc/passwd file. Each line in the file contains information about a single user, including their username, user ID number (UID), home directory, and the login shell.

The following sections present multiple ways to access the data in /etc/passwd and list users on Linux distributions.

The commands used in the tutorial are:

Note: To display a list of the logged-on users and the information such as boot time, processes, hostnames, and more, use the who command.

List Users with cat Command

The cat command provides a straightforward way to list the contents of the /etc/passwd file.

The system outputs the entire file with all the users on the system.

The output of the cat command used on passwd file.

To view the number of users only, pipe the output of the previous command to the wc command and make it count the number of lines:

The number of lines in /etc/passwd corresponds to the total number of users.

Counting the number of lines in the passwd file with the cat and wc commands.

List Users with Terminal Pagers less and more

On systems with many users, it is useful to limit the /etc/passwd file output displayed at once. Use a terminal pager command, such as less or more , to browse through the file content line by line or page by page.

To open /etc/passwd using less , enter:

The first page of the file appears in the output. The list stops when it reaches the end of the terminal screen. Use the keyboard to navigate through the file.

Using the less command to control the amount of output on the screen.

Use more to get a similar result. This command is older and has a more limited set of functionalities:

Using the more command to control the amount of output on the screen.

List Users with awk Command

Use the awk command to list the usernames only, without additional information about each user. Since the data fields in /etc/passwd are separated by a colon symbol, the following syntax tells awk to output only the first field in each line:

Displaying only the usernames using the awk command.

Combine awk and less for a page-by-page view of the results.

List Users with getent Command

The getent command searches and displays system database entries. The searchable databases are listed in the /etc/nsswitch.conf file. By default, the file includes the passwd database.

List the entire contents of the passwd database by typing:

The output is the same as the output of the cat command.

The ouput of the getent passwd command.

However, you can use getent to look up specific users. To do so, use the following syntax:

Читайте также:  Linux packages installed directory

If the user exists on the system, the command shows the related passwd entry line.

Searching the passwd base by username using the getent command to list users in Linux.

Listing Normal and System users in Linux

Linux-based systems have two types of users — system and normal users.

  • System users are entities created by the system to run non-interactive processes, i.e., the processes that run in the background and do not require human interaction. The most important system user is root, which possesses administrative privileges.
  • Normal users are human users created by root or another user with root privileges. Each normal user has a login shell and a home directory to store their files.

Both system and normal users in Linux have a unique user ID (UID) to identify them. System users have UIDs in the range from 0 (root user) to 999. Normal users typically receive UIDs from 1000 onwards, with each newly created user receiving the next smallest unused UID.

To check the UID range for normal users, use the grep command to search for the information stored in /etc/login.defs :

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

The output in this example shows that the smallest UID a normal user can receive is 1000, and the largest is 60000.

Using the grep command to check the smallest and largest UID a normal user can receive.

Use getent to search the passwd database by UID:

The output shows the user entry related to the UID.

Searching the user according to their UID using getent.

Use UIDs in combination with getent to search for users in a range:

The command now lists all the users within the specified UID range.

Searching the passwd database by providing a UID range, using getent to list users in Linux.

This guide showed you how to list all Linux users, search for users, and find the number of Linux users in any Linux distribution.

Источник

How can I display all users and groups with a command?

users and groups commands display users currently logged in, and groups a user belongs to respectively.

How to display a list of all users and all groups by command-line?

2 Answers 2

You can display with the help of compgen builtin command as follows:

    To display all users run following command:

However you can also display all users by cut -d «:» -f 1 /etc/passwd .

Nice! it might be preferable to use getent passwd / getent group instead of cat’ing the local files ( getent should work for non-local accounts as well)

Well, on my ubuntu, I have some files created by docker mount with 999:999 as user:group , but unfortunately none of the above commands prints them.

Here we are going to use getent for the detailed the info

We can list the user with the following command:

We can list the group as follows:

To fetch detail a specific user

Replace the lalit with your user name. Lalit will not be in every system 🙂

You can read the more into about getent here

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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