List all users on linux system

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.

Читайте также:  Установка времени linux debian

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.

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).

Читайте также:  Hearthstone decks tracker linux

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.

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

Have you ever wanted to list all users in your Linux system or to count the number of users in the system? There are commands to create a user, delete a user, list logged in users, but what is the command to list all users in Linux?

This tutorial will show you how to list users in Linux systems.

Get a List of All Users using the /etc/passwd File #

Local user information is stored in the /etc/passwd file. Each line in this file represents login information for one user. To open the file you can either use cat or less :

Each line in the file has seven fields delimited by colons that contain the following information:

  • User name.
  • Encrypted password ( x means that the password is stored in the /etc/shadow file).
  • User ID number (UID).
  • User’s group ID number (GID).
  • Full name of the user (GECOS).
  • User home directory.
  • Login shell (defaults to /bin/bash ).

If you want to display only the username you can use either awk or cut commands to print only the first field containing the username:

root daemon bin sys sync . . sshd vagrant jack anne 

Get a List of all Users using the getent Command #

The getent command displays entries from databases configured in /etc/nsswitch.conf file, including the passwd database, which can be used to query a list of all users.

To get a list of all Linux userr, enter the following command:

As you can see, the output is the same as when displaying the content of the /etc/passwd file. If you are using LDAP for user authentication, the getent will display all Linux users from both /etc/passwd file and LDAP database.

Читайте также:  Nvidia geforce 7300 linux

You can also use awk or cut to print only the first field containing the username:

Check whether a user exists in the Linux system #

Now that we know how to list all users, to check whether a user exists in our Linux box we, can simply filter the users’ list by piping the list to the grep command.

For example, to find out if a user with name jack exists in our Linux system we can use the following command:

If the user exists, the command above will print the user’s login information. No output that means the user doesn’t exist.

We can also check whether a user exists without using the grep command as shown below:

Same as before, if the user exists, the command will display the user’s login information.

If you want to find out how many users accounts you have on your system, pipe the getent passwd output to the wc command:

As you can see from the output above, my Linux system has 33 user accounts.

System and Normal Users #

There is no real technical difference between the system and regular (normal) users. Typically system users are created when installing the OS and new packages. In some cases, you can create a system user that will be used by some applications.

Normal users are the users created by the root or another user with sudo privileges. Usually, a normal user has a real login shell and a home directory.

Each user has a numeric user ID called UID. If not specified when creating a new user with the useradd command, the UID will be automatically selected from the /etc/login.defs file depending on the UID_MIN and UID_MAX values.

To check the UID_MIN and UID_MAX values on your system, you can use the following command:

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

From the output above, we can see that all normal users should have a UID between 1000 and 60000. Knowing the minimal and maximal value allow us to query a list of all normal users in our system.

The command below will list all normal users in our Linux system:

vagrant:x:1000:1000:vagrant. /home/vagrant:/bin/bash jack:x:1001:1001. /home/jack:/bin/bash anne:x:1002:1002:Anne Stone. /home/anne:/bin/bash patrick:x:1003:1003:Patrick Star. /home/patrick:/usr/sbin/nologin

Your system UID_MIN and UID_MIN values may be different so the more generic version of the command above would be:

eval getent passwd <$(awk '/^UID_MIN/ ' /etc/login.defs)..$(awk '/^UID_MAX/ ' /etc/login.defs)>

If you want to print only the usernames just pipe the output to the cut command:

eval getent passwd <$(awk '/^UID_MIN/ ' /etc/login.defs)..$(awk '/^UID_MAX/ ' /etc/login.defs)> | cut -d: -f1

Conclusion #

In this tutorial, you learned how to list and filter users in your Linux system and what are the main differences between system and normal Linux users.

The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.

Feel free to leave a comment if you have any questions.

Источник

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