Linux find all users

15+ examples for listing users in Linux

In this post, you will learn about listing users in Linux. Besides this, you will know other tricks about Linux users’ characteristics.

There are two types of users in Linux, system users who are created by default with the system. On the other hand, there are regular users who are created by system administrators and can log in to the system and use it.

Table of Contents

What file contains the list of the users on Linux?

Before we start listing users, we need to know where these users saved on Linux?

The users are stored in a text file on the system called the passwd file. This file is located in the /etc directory.

The file is located on the following path:

In this file, you can find all the information about the users in the system.

List all users

Listing users is the first step to manage them. This way, we will know how many they are and who they are. In Linux, almost everything can be done in various ways, and this is no exception.

To list all users, you can use the cat command:

As you can see in the image, there is all the information about the users.

1- In the first field, you will see the user name.

2- The second field (The x character) is a representation of the encrypted password. The encrypted password is stored in /etc/shadow file.

3- The UID or the user ID.

4- The next field refers to the primary group of the user.

5- Then, it shows user ID info such as the address, email, etc.

6- After this, you will see the home directory of the user.

7- The last field is the shell used by that user.

However, although the information is quite useful, if you only want to list users’ names in a basic way, you can use this command:

Now we have the names only by printing the first field of the file only.

List & sort users by name

The above command serves the purpose of listing users on Linux. But what about listing the users in alphabetical order?

To do this, we will use the previous command, but we will add the sort command.

So, the command will be like this:

As you can see in the image, the users are shown sorted.

Linux list users without password

It is important to know users who have no password and to take appropriate action. To list users who do not have a password, just use the following command:

The used regex will list all users with no password.

List users by disk usage

If you have a big directory and you want to know which user is flooding it, you can use the du command to get the disk usage.

With this, you can detect which of these users are misusing the disk space.

For it, it is enough to use the following command:

In this way, you will have the users ordered by the disk usage for the /home directory.

We used the -n for the sort command to sort the output by numbers.

List the currently logged users

To list the currently logged in users, we have several ways to do it. The first method we can use the users command:

It will list the users with open sessions in the system.

But this information is a little basic; however, we have another command that gives more details. The command is simply w.

Читайте также:  Linux сменить раскладку через терминал

With this command, we can have more information, such as the exact time when the session was started and the terminal session he has available.

Finally, there is a command called who. It is available to the entire Unix family. So you can use it on other systems like FreeBSD.

With who command, we also have some information about currently logged in users. Of course, we can add the option -a and show all the details.

This way, you know everything about the logged in users.

Linux list of users who recently logged into the system

We saw how to get the currently logged in users, what about listing the login history of users?

You can use the last command to get more info about the logins that took place:

Or the logins of a particular user

These are the user login activity and when it was started and how long it took.

List users’ logins on a specific date or time

What about listing users’ logins on a specific date or time? To achieve this, we use the last command but with the -t parameter:

And now, all you have to do is choose an exact date & time to list who logged at that time.

List all users in a group

There are two ways to list the members of a group in Linux, the easiest and most direct way is to get the users from the /etc/group file like this:

This command will list users in the likegeeks group.

The other way is by using commands like the members command in Debian based distros. However, it is not installed by default in Linux distributions.

To install it in Ubuntu / Linux Mint 19, just use APT:

Or in the case of CentOS:

Once it’s installed, you can run the command then the name of the group you want to list the users to:

This way, you can list users for a group in a Debian based distro. What about a RedHat based distro like CentOS?

You can use the following command:

List users with UID

In Unix systems, each user has a user identifier or ID. It serves to manage and administer accounts internally in the operating system.

Generally, UIDs from 0 to 1000 are for system users. And thereafter for regular users. Always on Unix systems, UID zero belongs to the root users (You can have more than one user with UID of zero).

So now, we will list the users with their respective UID using Awk.

The command that performs the task is the following:

As you can see, each user with his UID.

List root users

In a Unix-like system like Linux, there is usually only one root user. If there are many, how to list them?

To do this, we can use this command:

Here we are filtering the file to get users with UID of zero (root users).

Another way by checking the /etc/group file:

Here we are getting users in the group root from the /etc/group file.

Also, you can check if any user can execute commands as root by checking the /etc/sudoers file:

Get the total number of users

To get the total number of users in Linux, you can count lines in /etc/passwd file using the wc command like this:

Great! 43 users. But this includes system and regular users. What about getting the number of regular users only?

Easy! Since we know from above that regular users have UID of 1000 or greater, we can use Awk to get them:

List sudo users

Linux systems have a utility called sudo that allows you to execute commands as if you were another user who is usually the root user.

This should be handled with care in a professional environment.

Also, it is very important to know which users can run the sudo command. For this, it is enough to list the users that belong to the sudo group.

Users in this group can execute commands as super users.

List users who have SSH access

SSH allows users to access remote computers over a network. It is quite secure and was born as a replacement for Telnet.

By default, all regular users can log in and use SSH. If you want to limit this, you can use the SSH configuration file (/etc/ssh/ssh_config) and add the following directive:

Читайте также:  Включить стикеры windows 10

Also, you can allow groups instead of allowing users only using the AllowGroups directive:

These directives define who can access the service. Don’t forget to restart the SSH service.

List users who have permissions to a file or directory

We can give more than one user permission to access or modify files & directories in two ways.

The first method is by adding users to the group of the file or the directory.

This way, we can list the group members using the members utility as shown above.

Okay, but what if we just want this user to have access to this specific file only (Not all the group permissions)?

Here we can set the ACL for this file using setfacl command like this:

Here we give the user called newser the permission for the file called myfile the permissions of read & write & execute.

Now the file can be accessed or modified by the owner, and the user called newuser. So how to list them?

We can list them using the getfacl command like this:

This command will list all users who have permissions for the file with their corresponding permissions.

List locked (disabled) users

In Linux, as a security measure, we can lock users. This as a precaution if it is suspected that the user is doing things wrong, and you don’t want to completely remove the user and just lock him for investigation.

To lock a user, you can use the following command:

Now the user named myuser will no longer be able to login or use the system.

To list all locked users of the system, just use the following command:

This will print all locked users, including system users. What about listing regular users only?

As we saw above, using Awk, we can get locked regular users like this:

Listing remote users (LDAP)

Okay, now can list all system users (local users), but what about remote users or LDAP users? Well, we can use a tool like ldapsearch, but is there any other way?

Luckily yes! You can list local & remote users with one command called getent

This command lists both local system users and LDAP or NIS users or any other network users.

You can pipe the results of this command to any of the above-mentioned commands the same way.

Also, the getent command can list group accounts like this:

You can check the man page of the command to know the other databases the command can search in.

Conclusion

Listing users in the Linux system was fun! Besides this, we have learned some tips about users and how to manage them in different ways.

Finally, this knowledge will allow a better administration of the users of the system.

I hope you find the tutorial useful. Keep coming back.

Источник

How to List Users on Ubuntu Linux VPS

In this article, we will show you how to list users on Ubuntu VPS. By the end of this tutorial, you’ll be able to perform some basic commands to view all registered users and groups to manage your system administration. So, without further ado, let’s get started.

Reasons to View System Users in Ubuntu

Linux is popular among tech research groups and development communities because it is suitable for team-based projects. This means that oftentimes, a Linux-based system is used by multiple users at the same time.

For that reason, managing these users and groups is a day-to-day task. System administrators can take advantage of Ubuntu’s complete features to control how users and groups interact with the system.

For instance, you can grant certain users and groups to access specific files and folders. What’s more, you can adjust configuration and administrative rights to modify your system and secure your data by preventing any harmful anonymous process.

However, before configuring those settings, it is better to learn how to list users on Ubuntu VPS or server. The reason is simple: you will need to see their permissions and activities to verify every operation on your machine.

Do note that since we will list users on Ubuntu VPS, we have to use the command line to access it. You can work with the terminal shell (Linux and macOS) or PuTTY (Windows). If you’re a Hostinger user, the login credentials are located in the hPanel’s Servers tab.

Читайте также:  Amd vision engine control center для windows 10 64 bit

How to List Users on Ubuntu

We are going to divide this tutorial into three parts — viewing all users, viewing groups, and viewing logged in users.

Viewing All Users on Linux

Linux stores all the information about user registrations in a file called passwd, which is located at /etc/passwd.

  1. To access the content of the file, open your terminal and type the following command:
  2. The script will return a list that looks like this:

In the output above, each line corresponds to a user. The number of lines will depend on the number of registered users.

Each line contains seven fields that are separated by colons (:). These fields hold specific information about a particular user.

Let’s take a look at the first line:

Here are the meanings of each field, from left to right:

  • root — the login name of the user.
  • bplaceholder for the password (the actual password is stored in a separate file).
  • 0User ID, which is unique for each registered user. By default, root has the User ID of 0.
  • 0Group ID. Like User ID, it is unique for every user.
  • rootComment field. Contains a short description of the user, usually filled out with users’ full name.
  • /rootHome directory. This is the main directory of the users. For users other than root, the folder will be /home/username.
  • /bin/bashUser shell. Refers to the shell that users use to sign in to the system.

If you only want to view the name of the users, you can run this special command:

Viewing Groups on Linux

In Linux, you also have the option to create groups. You basically gather several users and grant them joint privileges or access. It is particularly useful if multiple teams are using the same system.

Unlike users, the group information is stored in a file named group that can be found at /etc/group.

  1. To view the contents of this file, simply open up the terminal and type the following command:
  2. The line above should give this output:

Notice that the group names are exactly the same as the name of our users. This is the result of user private groups (UPG), a scheme employed by Linux where each user will have their own private group.

Just like the previous one, it is possible to view the group names only:

Viewing Logged In Users

To list logged in users, simply type the following letter in your terminal:

Besides showing the active users, the command also provides some additional information like login time, remote host, and idle time. The full results will look like this:

Let’s breakdown the results of the w command:

  • User ⁠— username.
  • TTY ⁠— terminal name
  • From ⁠— the name of the remote host.
  • Login@ ⁠— login time.
  • Idle ⁠— idle time.
  • JCPU ⁠— the amount of time used by processes attached to the TTY.
  • PCPU ⁠— the time used by the process displayed in the WHAT field.
  • WHAT ⁠— the user’s current process.

Alternatively, there is another command that performs a similar function to w:

However, the results won’t be as detailed as the w command:

Conclusion

Linux is a perfect OS for team-based projects because many people can log in to the same system. Due to this nature, it is important to learn how to manage users for better system administration.

The first lesson you need to know is to list the registered users, which will be useful to verify all operations and protect your data.

In this guide, we have shown you how to list users on Ubuntu VPS using simple commands. In addition, you’ve also learned how to view all groups and monitor every logged-in user.

Good luck and be sure to check other VPS tutorials!

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.

Источник

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