Interviewer: talk about all the ways to view the information of the currently logged-in user under Linux

Like and follow, you won’t get lost!     

background

As a system administrator, you may often (at some point) need to see which users are active in the system. Sometimes, you even need to know what they are doing. This article summarizes 4 ways to view system user information (by number (ID)).

text

1. Use the w command to view the process information that the logged-in user is using

The w command is used to display the names of users who have logged in to the system and what they are doing. The information used by this command comes from the /var/run/utmp file. The information output by the w command includes:

  • user name
  • User's machine name or tty number
  • Remote host address
  • The time the user logged in to the system
  • Free time (not very useful)
  • The time (JCPU time) used by the process attached to the tty (terminal)
  • The time used by the current process (PCPU time)
  • The command currently being used by the user

The w command can also use the following options

  • -h ignore header file information
  • -u shows the load time of the results
  • -s does not display JCPU, PCPU, login time
$ w

23:04:27 up 29 days, 7:51, 3 users, load average: 0.04, 0.06, 0.02

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

ramesh pts/0 dev-db-server 22:57 8.00s 0.05s 0.01s sshd: ramesh [priv]

jason pts/1 dev-db-server 23:01 2:53 0.01s 0.01s -bash

john pts/2 dev-db-server 23:04 0.00s 0.00s 0.00s w


$ w -h

ramesh pts/0 dev-db-server 22:57 17:43 2.52s 0.01s sshd: ramesh [priv]

jason pts/1 dev-db-server 23:01 20:28 0.01s 0.01s -bash

john pts/2 dev-db-server 23:04 0.00s 0.03s 0.00s w -h


$ w -u

23:22:06 up 29 days, 8:08, 3 users, load average: 0.00, 0.00, 0.00

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

ramesh pts/0 dev-db-server 22:57 17:47 2.52s 2.49s top

jason pts/1 dev-db-server 23:01 20:32 0.01s 0.01s -bash

john pts/2 dev-db-server 23:04 0.00s 0.03s 0.00s w -u


$ w -s

23:22:10 up 29 days, 8:08, 3 users, load average: 0.00, 0.00, 0.00

USER TTY FROM IDLE WHAT

ramesh pts/0 dev-db-server 17:51 sshd: ramesh [priv]

jason pts/1 dev-db-server 20:36 -bash

john pts/2 dev-db-server 1.00s w -s

2. Use the who command to view (log in) the user name and the process started

The who command is used to list the names of users currently logged in to the system. The output is: user name, tty number, time and date, host address.

$ who

ramesh pts/0 2009-03-28 22:57 (dev-db-server)

jason pts/1 2009-03-28 23:01 (dev-db-server)

john pts/2 2009-03-28 23:04 (dev-db-server)

If you only want to list users, you can use the following statement:

$ who | cut -d' ' -f1 | sort | uniq

john

jason

ramesh

Supplement : The users command can be used to print out the name of the user logging in to the server. Apart from the help and version options, this command has no other options. If a user uses multiple terminals, multiple duplicate user names will be displayed accordingly.

 

 

 
$ users

john jason ramesh

3.  Use the whoami command to view the login name you are using

The whoami command is used to display the login user name.

$ whoami

john

The execution effect of whoami command is exactly the same as that of id -un, for example:

$ id -un

john

The whoami command can display the name of the currently logged in user and the tty information currently in use. The output of this command includes the following contents: user name, tty name, current time and date, and the link address used by the user to log in to the system.

$ who am i

john pts/2 2009-03-28 23:04 (dev-db-server)


$ who mom likes

john pts/2 2009-03-28 23:04 (dev-db-server)


Warning: Don't try "who mom hates" command.

Of course, if you use the su command to change the user, the result displayed by the command (whoami) will change accordingly.

 

 

4. View the historical information of the system at any time (information of users who have used the system)

The last command can be used to display the history of a specific user's login system. If no parameters are specified, the historical information of all users is displayed. By default, this information (the displayed information) will come from the /var/log/wtmp file. The output of this command contains the following columns of information:

  • user name
  • tty device number
  • Historical login time and date
  • Logout date
  • Total working hours
$ last jason

jason pts/0 dev-db-server Fri Mar 27 22:57 still logged in

jason pts/0 dev-db-server Fri Mar 27 22:09 - 22:54 (00:45)

jason pts/0 dev-db-server Wed Mar 25 19:58 - 22:26 (02:28)

jason pts/1 dev-db-server Mon Mar 16 20:10 - 21:44 (01:33)

jason pts/0 192.168.201.11 Fri Mar 13 08:35 - 16:46 (08:11)

jason pts/1 192.168.201.12 Thu Mar 12 09:03 - 09:19 (00:15)

jason pts/0 dev-db-server Wed Mar 11 20:11 - 20:50 (00:39

At last

The content is over here, if you need more, you can click "learn together" below. There will be more different content in the follow-up, I hope it will be helpful to everyone.

Finally, I want to say something to you. I have worked for so many years and have interviewed some people for others. Whether it is from the perspective of the interviewer or the leader, in addition to interview skills and experience, great technology and project experience are also their trump cards and confidence. Core technology sharing of first-tier manufacturers

 It took me a long time to sort out some learning materials. What I posted above is the tip of the iceberg in the materials. I hope I can help you! Click to learn the secret code together: csdn

                         

  I will share more pure dry goods articles in the follow-up, and hope to really help you. Your support is my biggest motivation! Welcome to follow and like!

                                                       

Guess you like

Origin blog.csdn.net/weixin_50333534/article/details/109085717