[Linux] Displaying All Currently Logged-In Usernames with the users Command

目次

Overview

The users command is a simple tool designed to list the usernames of everyone currently logged into the host system. Unlike the who or w commands, it does not display additional information such as login terminals or times. Instead, it outputs only the usernames in a single line, separated by spaces and sorted alphabetically. This format makes it particularly suitable for script processing where retrieving the list of logged-in users as an array is required.

Specifications (Arguments and Options)

Syntax

users [filename]

Note: Aside from help and version information, no unique options exist. A login history file (such as wtmp) can be specified as an argument.

Basic Usage

Executing the command without options displays a list of currently logged-in usernames. If a single user is logged in from multiple terminals, the name appears for each active session.

BASH

# Display logged-in users
users

Example Output

hayashi mori mori root

Practical Commands

Comparing with the w Command for Details

Comparing users with the w command highlights its simplicity. While w provides detailed information about “who” is logged in, “where” they are from, and “what” they are doing, users simply returns “who is present.”

BASH

# Simple list display
users

# Detailed status display
w

Example Output

# Result of users
hayashi mori mori

# Result of w
 12:45:01 up 10 days,  3:20,  3 users,  load average: 0.05, 0.03, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
mori     pts/0    192.168.1.10     10:00    1.00s  0.10s  0.02s w
mori     pts/1    192.168.1.10     10:05    2:40   0.05s  0.05s -bash
hayashi  pts/2    192.168.1.11     11:30   10:00   0.02s  0.02s vi report.txt

Referencing Past Login History Files

By default, the command references current login information (/var/run/utmp). However, specifying a file like /var/log/wtmp as an argument allows for the output of past user login history in the same format.

BASH

# Display using the login history file (wtmp)
users /var/log/wtmp

Summary

The users command is a specialized tool dedicated solely to extracting logged-in usernames. While the w or who commands offer more utility for system administrators performing visual checks due to the richness of information, users is invaluable for shell scripting. Since it requires no formatting, it is ideal for scenarios requiring batch processing for all logged-in users, such as iterating through the list with for user in $(users); do ....

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次