[Linux] 現在ログイン中のユーザーを表示する who コマンド

Linux で現在ログイン中のユーザーの一覧を表示する who コマンドをご紹介します。

Linux

who コマンド

who コマンドは、現在ログインしているユーザーの情報を表示することができます。

最初は表示項目が分からないと思うので –heading オプションをつけて使うと表示項目が分かりやすくておすすめです。

$ who --help
Usage: who [OPTION]... [ FILE | ARG1 ARG2 ]
Print information about users who are currently logged in.
 
  -a, --all         same as -b -d --login -p -r -t -T -u
  -b, --boot        time of last system boot
  -d, --dead        print dead processes
  -H, --heading     print line of column headings
  -l, --login       print system login processes
      --lookup      attempt to canonicalize hostnames via DNS
  -m                only hostname and user associated with stdin
  -p, --process     print active processes spawned by init
  -q, --count       all login names and number of users logged on
  -r, --runlevel    print current runlevel
  -s, --short       print only name, line, and time (default)
  -t, --time        print last system clock change
  -T, -w, --mesg    add user's message status as +, - or ?
  -u, --users       list users logged in
      --message     same as -T
      --writable    same as -T
      --help     display this help and exit
      --version  output version information and exit
 
If FILE is not specified, use /var/run/utmp.  /var/log/wtmp as FILE is common.
If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual.
 
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'who invocation'

who コマンド利用例

たまに ssh ログインしたまま残り続けている process があるので、それを who コマンドを使って調べてみます。

以下のように who –heading –users で ec2-user が 2016-11-21, 2016-11-24 にログインした process が IDLE タイム old って表示されていることがわかります。

$ who --heading --users
NAME     LINE         TIME             IDLE          PID COMMENT
ec2-user pts/0        2017-10-17 02:41 01:06       22973 (forward_proxy1)
ec2-user pts/1        2017-10-17 03:41   .          3667 (forward_proxy1)
ec2-user pts/2        2016-11-21 07:21  old        19734 (forward_proxy1)
ec2-user pts/3        2016-11-24 07:11  old         2202 (forward_proxy1)

念のため、ログイン中のユーザー名と処理内容を表示する w コマンドで old な process が何を実行しているのか確認します。

$ w
 03:52:38 up 452 days, 18:59,  4 users,  load average: 8.06, 8.80, 9.44
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
ec2-user pts/0    forward_proxy1   02:41    1:10m  0.02s  0.01s sshd: ec2-user [priv]
ec2-user pts/1    forward_proxy1   03:41    1.00s  0.05s  0.00s w
ec2-user pts/2    forward_proxy1   21Nov16 329days  0.00s  0.00s -bash
ec2-user pts/3    forward_proxy1   24Nov16 326days  0.01s  0.01s -bash

2つとも WHAT の項目が -bash でした。

bash や ssh なら何も動かしてないので、process は消してしまって大丈夫です。

以上、サーバー管理もしている現場からお送りしました。