Como você lista os usuários do Raspberry Pi?

How do you list Raspberry Pi users?

Raspberry Pi (RPi) is a popular microcomputer used for embedded systems design, hosting high-level applications running on an operating system. Many of these applications have multiple active users with different permissions and access to system resources.

As a result, there may be situations where it is important to know which users are sharing the device. Fortunately, there are several ways to get a list of registered RPi users on your Linux system.

In this article, we will discuss some of the common methods to get a list of RPi users.

Listing users on Raspberry Pi
There are several methods for listing users on RPi, including the following.

1. Using the 'cat' command
2. Using the 'getent' command
3. Using 'cut' and 'awk' commands
4. Using the 'id' command
5. Using the 'w' or 'who' command
6. Using '/etc/passwd' file
7. Using the lslogin command
8. Using the 'cut' command for group members
9. Using GUI

Using the 'cat' command
The cat command in RPi and other Linux or Unix-based operating systems is a basic utility that displays the contents of one or more text files in the terminal. Cat means concatenate, one of its main functions. The command has this syntax:

cat (options) (file(s))

The command can read data from a “passwd” file to obtain information about RPi users. The passwd file is present on all Linux-based systems, including Raspberry Pi. It contains information about all users with access to the system.

The following command can be used to target users:

$ cat /etc/password

The command can be modified to obtain information about users placed in the home directory by adding a filter using the 'grep' and 'cat' commands.

The grep command in RPi and other Linux/Unix-based operating systems is a powerful text search utility used to search for specific patterns or strings in text files or the output of other commands. Grep stands for Global Regular Expression Print, which reflects its main function of searching and printing lines that match a specified pattern.

The 'cat' and 'grep' commands can list home directory users as follows:

$ cat /etc/passwd grep home

Using the 'getent' command
The 'getent' command is another way to obtain information about RPi users. This command is a versatile utility that queries and retrieves information from various administrative databases. It can retrieve information about users, groups, hosts, networks and other system-related data. It is also particularly useful for getting data managed by database sources specified in the '/etc/nsswitch.conf' configuration file.

It has this syntax:

getent database(key)

The database specifies the type of recovery information available (e.g., the “passwd” for user information, the group for group information, the host for host information, etc.). The key is an optional parameter that can search for a specific entry in the database.

To get a list of RPi users, use the 'getent' and 'awk' commands as below.

$ getent password awk -F ':' '{print $1}'

To get information about a specific user, use this command:

$ getent password

This command can also be used to sort a list of users within a specific range when used with the 'awk' command as follows:

$ getent password {1000..6000}

Using the 'cut' and 'awk' commands
The 'cut' command in RPi and other Linux/Unix based operating systems is a text processing utility used to extract specific columns or fields from lines of text or delimited files. It is useful for manipulating and processing structured data. This command can separate text into columns based on a delimiter character and then select specific columns for output. It has this syntax:

cut (options) (file(s))

The '(options)' are the optional flags that modify the behavior of the 'cut' command, including specifying the delimiter and selecting fields and '(files)).' The '(files)' are the files that the 'cut' command will read data from.

To obtain information about RPi users, the command can be used to extract information from the 'passwd' file as follows:

$ cut -d: -f1 /etc/passwd

The 'awk' command in RPi and other Unix-like operating systems is a versatile text processing utility that allows you to process and manipulate text data in a highly flexible and programmable manner. This command is powerful when working with structured text files such as CSV files, log files, and other tabular or delimited data. The command has this syntax:

awk (options) 'program' (file(s))

The '(options)' are the optional flags that modify the behavior of the 'awk' command. The 'program' is a series of 'awk' commands enclosed in single quotes that define the actions to be performed on each line of input. The '(files)' are the files from which the 'awk' command will read data.

To obtain information about RPi users, this command can be used to extract information from the 'passwd' file as follows:

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

Using the 'id' command
The 'id' command on Raspberry Pi and other Unix-like operating systems displays information about a user or group, including their user and group IDs (UID and GID), supplemental group IDs, and more details related to the user's identity or group and privileges.

This command can help you quickly access information about the current user or a specific user or group on the system. The command has this syntax:

id (options) (username)

The '(options)' are the optional flags that modify the behavior of the 'id' command. The '(username)' is an optional argument that specifies the username you want to display. If this is not specified, it defaults to the current user. The command can be used to obtain information about a specific RPi user as follows:

$id

Using the 'w' or 'who' commands
The 'w' command on Raspberry Pi and other Unix-like operating systems displays information about currently logged in users and their activities. It provides a summary of who is logged into the system, what they are doing, and where they are logged in from. This command is useful for system administrators to monitor user sessions and resource usage. The command has this syntax:

w (options)

The '(options)' are the optional flags that modify the behavior of the 'w' command. It is executed as follows:

$w

The 'who' command on Raspberry Pi and other Unix-like operating systems displays information about currently logged in users. It provides a summary of who is logged into the system, including details about their login sessions — such as username, terminal or pseudoterminal, login time, and remote IP address or hostname (if applicable).

While the 'w' command provides detailed information including the terminal they are using, the 'who' command offers a simpler list. It has this syntax:

who (options)

The '(options)' are the optional flags that modify the behavior of the 'who' command. It is executed as follows:

$ who

Using the /etc/passwd file
Information about RPi users can also be obtained by directly accessing the 'passwd' file using a text editor or pager such as 'nano' or 'less'. It can be accessed as follows:

$ nano /etc/password

The 'passwd' file can be accessed using less pager as follows:

$ minus /etc/passwd

Using the 'lslogin' command
The 'lslogin' command lists all current login sessions of the RPi or any other Linux-based system. It can be used to obtain information about the users who are logged in, the terminals they are using, and the time they log in.

To list the current login sessions on your RPi, enter the following command:

$ lslogin -you

Using the 'cut' command for group members
To list users belonging to a specific group, use the 'cut' and 'grep' commands as follows:

grep '^ :' /etc/group cut -d: -f4

Using GUI
In desktop mode, a list of RPi users can be found by opening the file manager and navigating to the 'home' folder. In this folder there are additional folders for all RPi users.

Conclusion
There are several ways to obtain information about Raspberry Pi users. Most of these methods access the 'passwd' file and manipulate its contents. For example, information about users who are currently logged in can be obtained using commands such as 'w', 'who' and 'lslogins'. A folder for each user is in the RPi home directory, which includes the names of all users.

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.