This is the 28th day of my participation in Gwen Challenge


A lifelong learner, practitioner and sharer committed to the path of technology, a busy and occasionally lazy original blogger, an occasionally boring and occasionally humorous teenager.

Welcome to dig friends wechat search “Jie Ge’s IT journey” attention!

Five methods to view the information about the current system login user

preface

At work, whenever a user logs in to the system, the program searches for the UID number of the user in the lastlog file. If the UID number is found, the user’s last login time, exit time, host name and other information will be output, and then the latest login time will be recorded in the lastlog file. When a user logs in successfully, the login program inserts the user’s login information into the UTMP file and saves the information until the user logs out.

As the system administrator of us, no matter when we need to view the system which users in the operation of what, we will use today I share with you the topic: 5 ways to view the current system login user information and related usage and basic introduction.

Run the w command

The w command looks at the UTMP file and displays information about each user in the current system and the processes it is running, as well as the CPU usage of those processes.

-u: displays the loading time of the result

The result is as follows:

  • USER: indicates the USER name
  • TTY: Indicates the name or TTY number of the user’s device
  • FROM: indicates the address of the remote host
  • Login@ : indicates the LOGIN time
  • IDLE: indicates the IDLE time
  • JCPU: Time spent by a process attached to a TTY terminal
  • PCPU: time spent by the current process
  • WHAT: Indicates the command that the user is currently using
[root@localhost ~]# w -u 16:30:00 up 2 days, 23:45, 4 users, load average: 0.00, 0.00, 0.00 USER TTY FROM login@idle JCPU PCPU WHAT root tty7:0 Mon17 1088days 14.76s 14.76s /usr/bin/xorg :0 -br-verbose - Audit 4 -auth /var/run/gdm/auth-for-gdM-f08l6u/database-nolisten TCP root PTS /0:0.0 02:28 2:06m 0.23s 0.23s /bin/bash Root PTS /1 192.168.1.119 14:24 0.00s 0.17s 0.06s w root tty1-mon17 22:53m 0.03s 0.03s-bashCopy the code

-h: ignores header file information

[root@localhost ~]# w -h root tty7:0 Mon17 1088days 14.76s 14.76s /usr/bin/xorg :0 -br-verbose -audit 4 -auth /var/run/gdm/auth-for-gdM-f08l6u/database-nolisten TCP root PTS /0 :0.0 02:28 2:07m 0.23s 0.23s /bin/bash root PTS /1 192.168.1.119 14:24 0.00s 0.11s 0.00s w -h root tty1-mon17 22:54m 0.03s 0.03s-bashCopy the code

-s: JCPU, PCPU, and login time are not displayed

[root@localhost ~]# w -s 16:33:21 up 2 days, 23:49, 4 users, load average: 0.00, 0.00, 0.00 USER TTY FROM IDLE WHAT root tty7:0 1088days /usr/bin/xorg :0 -br-verbose -audit 4-auth /var/run/gdm/auth-for-gdM-f08l6u/database-nolisten TCP root PTS /0:0.0 2:09m /bin/bash root PTS /1 192.168.1.119 0.00s w  -s root tty1 - 22:57m -bashCopy the code

-f: enables or disables the display of where the user logs in to the system

[root@localhost ~]# w -f 23:37:48 up 3 days, 6:53, 5 users, load average: 0.00, 0.00, 0.00 USER TTY login@idle JCPU PCPU WHAT root tty7 Mon17 1088days 19.45s 19.45s /usr/bin/xorg :0 -br-verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-f08l6u/database-nolisten TCP root PTS /0 02:28 1:26m 0.35s 0.35s /bin/bash root PTS /1 22:11 1.00s 0.66s 0.59s W-f root PTS /2 21:27 1:39m 0.16s 0.08s bash root tty1 Mon17 30:01m 0.03s 0.03s-bashCopy the code

-l: indicates the detailed format list. This is the default value

[root@localhost ~]# w -l 23:39:00 up 3 days, 6:54, 5 users, load average: 0.00, 0.00, 0.00 USER TTY FROM login@idle JCPU PCPU WHAT root tty7:0 Mon17 1088days 19.45s 19.45s /usr/bin/xorg :0 -br-verbose - Audit 4 -auth /var/run/gdm/auth-for-gdM-f08l6u/database-nolisten TCP root PTS /0:0.0 02:28 1:28m 0.35s 0.35s /bin/bash Root PTS /1 192.168.1.3 22:11 1.00s 0.10s 0.01s W -L Root PTS /2 192.168.1.3 21:27 1:40m 0.16s 0.08s bash root TTy1 - Mon17 30:02m 0.03s 0.03s -bashCopy the code

2. Run the who command

The who command is used to list the names of users who have logged in to the system

Without any arguments, the following output is displayed:

  • Column 1: user name;
  • Column 2: Terminal name;
  • Column 3: login time, and the contents in parentheses indicate which host is logged in from;
[root@localhost ~]# who root tty7 2019-05-27 17:36 (:0) root PTS /0 2019-05-28 02:28 (:0.0) root PTS /1 2019-05-28 14:24 (192.168.1.119) root tty1 2019-05-27 17:36Copy the code

-h: Displays the title of each column

[root@localhost ~]# who-h roottty7 2019-05-27 17:36 (:0) root PTS /0 2019-05-28 02:28 (:0.0) root PTS /1 2019-05-28 14:24 (192.168.1.119) root PTS /2 2019-05-28 21:27 (192.168.1.3) root tty1 2019-05-27 17:36Copy the code

-u: displays the idle time. If the user has taken any action within the last minute, “.” is displayed. If the user has taken no action for more than 24 hours, “old” or “old” is displayed.

[root@localhost ~]# who-u root tty7 2019-05-27 17:36 (:0) root PTS /0 2019-05-28 02:28 00:30 33758 (:0.0) root PTS /1 2019-05-28 14:24 03:29 36188 (192.168.1.119) root PTS /2 2019-05-28 21:27.37350 (192.168.1.3) root TTy1 The old one is 31831Copy the code

-m: the effect is the same as the specified “am I “string

[root@localhost ~]# who-m root PTS /2 2019-05-28 21:27Copy the code

-q: displays only the login account name and user number

[root@localhost ~]# who -q root root rootCopy the code

-w: displays the user status bar

[root@localhost ~]# who-w root + tty7 2019-05-27 17:36 (:0) root + PTS /0 2019-05-28 02:28 (:0.0) root + PTS /1 2019-05-28 14:24 (192.168.1.119) root + PTS /2 2019-05-28 21:27 (192.168.1.3) root + tty1 2019-05-27 17:36Copy the code

Use the whoami command

The whoami command is used to display the login user name

[root@localhost ~]# whoami
root
Copy the code

Of course, in addition to the above, you can also execute the following two commands to display the name of the user currently logged in and the TTY information currently in use.

[root@localhost ~]# id -un root root [root@localhost ~]# who am I root PTS /2 2019-05-28 21:27 (192.168.1.3)Copy the code

Run the last command

The last command is used to display the latest login information of a user. If no parameter is specified, historical information about all users is displayed. By default, this information will be displayed in the /var/log/wtmp file.

Output results:

  • Column 1: user name
  • Column 2: Terminal name
  • Column 3: IP address of the client
  • Column 4: Login and logout time. The values in parentheses are: duration of login; Still logged in: Indicates that the user is still online.
[root@localhost ~]# last root PTS /1 192.168.1.119 Tue May 28 14:24 Still logged in root PTS /3 192.168.1.6 Tue May 28 12:04-14:12 (02:07) root PTS /2 Administrator Tue May 28 11:03-13:40 (02:37)Copy the code

-r: omits hostname information

[root@localhost ~]# last -R root pts/2 Tue May 28 21:27 still logged in root pts/1 Tue May 28 14:24 still logged in root  pts/3 Tue May 28 12:04 - 14:12 (02:07) root pts/2 Tue May 28 11:03 - 13:40 (02:37) root pts/1 Tue May 28 08:59 - 13:12 (04:13) root pts/2 Tue May 28 05:00 - 08:59 (03:59) root pts/1 Tue May 28 02:28 - 07:10 (04:41)Copy the code

-num parameter: Displays the first num. The num value can be changed. The last-r-3 and last-n 3 -r command output is the same

[root@localhost ~]# last-3 root PTS /2 192.168.1.3 Tue May 28 21:27 Still logged in root PTS /1 192.168.1.119 Tue May 28 14:24 Still logged in root PTS /3 192.168.1.6 Tue May 28 12:04-14:12 (02:07) WTMP begins Sat Jun 4 06:40:322016 [root@localhost ~]# last -R -3 [root@localhost ~]# last -n 3 -R root pts/2 Tue May 28 21:27 still logged in root pts/1 Tue May 28 14:24 still logged in root pts/3 Tue May 28 12:04 - 14:12 (02:07) wtmp begins Sat Jun 4 06:40:32 2016Copy the code

The host IP address is displayed in the last column

[root@localhost ~]# last-n 5-a -I root PTS /2 Tue May 28 21:27 Still logged in 192.168.1.3 Root PTS /1 Tue May 28 14:24 Still logged in 192.168.1.119 root PTS /3 Tue May 28 12:04-14:12 (02:07) 192.168.1.6 root PTS /2 Tue May 28 11:03 - 13:40 (02:37) 192.168.1.6 root PTS /1 Tue May 28 08:59-13:12 (04:13) 192.168.1.126 WTMP begins Sat Jun 4 06:40:32 2016 You have new mail in /var/spool/mail/rootCopy the code

To query information about only one user, run the following command, and the value -2 indicates num. For example, run the last root command.

[root@localhost ~]# last root -2 root PTS /1 192.168.1.3 Tue May 28 22:11 Still logged in root PTS /2 192.168.1.3 Tue May 28 21:27 still logged in wtmp begins Sat Jun 4 06:40:32 2016Copy the code

5. Use lastlog

The lastlog command is used to view the last login time of each user in the system

[root@localhost ~]# lastlog username port from last login time root PTS /1 192.168.1.119 2 May 28 14:24:05 +0800 2019 bin ** never logged in to ** daemon ** never logged in ** adm ** never logged in ** (other display results omitted)Copy the code

-u: displays the last record of the specified login after the user name

[root@localhost ~]# lastlog -u root user name port from last login time root PTS /1 192.168.1.3 2 May 28 22:11:07 +0800 2019Copy the code

-t: DAYS NUM is used to display the records of recent DAYS

[root@localhost ~]# lastlog -t 3
Copy the code

Original is not easy, if you think this article is useful to you, please kindly like, comment or forward this article, because this will be my power to output more high-quality articles, thank you!

By the way, please give me some free attention! In case you get lost and don’t find me next time.

See you next time!