15.3 C
Amsterdam
angeloma
Senior Writer and partner

How to List users in Linux?

Hello friends. Normally when we are in the presence of a Linux server or computer at some point we will need to know how to list users. This is either for administrative or informative purposes but it becomes necessary. So, in this post, you will learn how to do it using the terminal quickly and easily.

I think you already know but there are commands to create a user, delete a user, list registered users, but at work needs always arise. And if you need to know which are the users of the system? What is that command?

Well, there are several commands, and each one has its own way of doing the job and you can even combine them to get better results.

Let’s go for it.

How to list users in Linux

Commands that use the /etc/passwd file

- Advertisement -

In the /etc/passwd file you can find information about the system users. However, it is not as readable as you would like it to be, it is a good option.

Open a terminal and run the following command

cat /etc/passwd

And you will get a screen output similar to this.

1.- Cat command
1.- Cat command

The first thing you may notice is that there are many users but most of them are system users required to perform many operations that have to do with the operation of the system.

The second thing is that the information is separated by columns where different fields are shown such as Username, encrypted password, UID, GID, and other information.

If you want to know only the users, you can filter the on-screen output of the cat command with the help of the cut command.

cat /etc/passwd | cut -d: -f1

This way only the users will be displayed without the rest of the information.

2.- List users in Linux
2.- List users in Linux

The same result can be obtained with the command awk.

cat /etc/passwd | awk -F: '{print $1}'

This way we will be able to list the system users.

List users in Linux with compgen

Although the above commands are easy to use, they are not so easy to memorize. Especially when you have to share them with others.

That is why there is the command compgen with which we can simply get all the users of the system.

To do this, just run

compgen -u

And in this simple way, we will be able to have all the users of the system.

Bonus: List the users connected to the system

On the other hand, it is convenient on many occasions to know which are the users that are connected in real-time with the system. To do this you only have to run

who
3.- Users connected to the system
3.- Users connected to the system

And you will be able to list the users that are using the system at this moment.

Conclusion

A terminal is a tool in which many professionals make their working life and that is full of tricks to take full advantage of it. In this post, you have learned how to list users in Linux quickly and easily.

- Advertisement -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Donate to Osradar

Latest article