Shell memo for querying user information in Linux

Source: Internet
Author: User

Author:North South
Sponsorship:
From:Linuxsir. org
Abstract:This document describes several tools used in user management, including ID, finger, groups, W, WHO, and users;


Directory Index

1. Principles of user query tools;

2. user and group query tools;

    1. ID tool: query the user's uid, GID, and GID user groups;
    2. Finger tool: Used to query user information, focusing on the user's home directory and logon shell;
    3. user tools used to query logon hosts: W, WHO, and users
    4. query the user group to which the groups user belongs;

Iii. postscript;
4. participate in correcting this article;
5. Thank you;
Vi. related documents;


++ ++
Body
++ ++


1. Principles of user query tools;

In user management, user queries are performed using several common tools, such as ID, finger, groups, users ......, we all know that the user configuration file is/etc/passwd, and the user group configuration file is the/etc/groups file. In addition to the query tool, we can also directly view the configuration files of users and user groups for query purposes;

The principle of the user query tool is to read configuration files related to users and user groups, and then output the files according to certain rules and conditions. The user and user group configuration files are very important, therefore, you must first read the user-managed documents:

Linux User and user group management overview
User and group configuration file details


2. user and group query tools;


1. ID tool: query the user's uid, GID, and GID user groups;

The ID tool is used to query user information, such as the user group, uid, and GID that the user belongs to. The ID usage is extremely simple. Let's give an example to illustrate it;

 

Syntax format: Id [parameter] [user name]

 

For which parameters are available, check the id -- help or man ID. If the ID is not followed by any parameter or user, by default, the user name, user group, uid, and gid of the current user are displayed;

Instance 1: no parameters or user names are added;

[beinan@localhost ~]$ id
uid=500(beinan) gid=500(beinan) groups=500(beinan)

 

Note:If no parameters are added, the user name, uid, GID, and primary and affiliated user groups of the current user are queried. In this example, the user name is beinan, UID is 500, the owner main user group is beinan, and GID is 500;


Instance 2: Id followed by the user name;

If you want to query the user's UID and GID, you can directly connect the user name, but the user name must be real and can be found in/etc/passwd;

[beinan@localhost ~]$ id linuxsir
uid=505(linuxsir) gid=502(linuxsir) groups=502(linuxsir),0(root),500(beinan)

 

Note:Query the user's linuxsir information. The user's linuxsir, uid is 505, the owner's main user group is linuxsir, And the GID of the main user group is 502. At the same time, the linuxsir user is also a member of the root user group whose GID is 0, it is also a member of the beinan group whose GID is 500;

This example differs from instance 1 in terms of user groups. We have mentioned in Linux User and user group management overview. The ing between users and user groups is as follows, it can be a one-to-one, one-to-many, many-to-one, or many-to-many cross relationship. For more information, see user and group) configuration file details;


2. Finger tool: Used to query user information, focusing on the user's home directory and logon shell;

The finger tool focuses on user information query. The query content includes the user name (also known as the login name login), home directory, and real User Name (name )...... office address, office phone number, logon terminal, write status, empty closing time, etc;

We usually use finger to query the user's home directory, real user name, shell type, office address, and phone number, which are output in the-l long format; however, to modify the user's home directory, real name, office address, and office phone number, we generally need to run the chfn command;


Syntax format:

Finger [Parameter options] [user name]
-L uses the long format (default) to display all information contained by the-s option, as well as the Home Directory, office address, office phone number, logon shell, email status, and ,. plan ,. project and. forward;
-M: do not match the real user name;
-P: omit the content in the. Plan and. project files;
-S: short format, username (also called login), real name (name), logon terminal (TTY), write status, and idle time (idle) login Time, office location, office phone number, etc;

 

For the finger parameters, you can use finger -- help or man finger to obtain them. In this article, we will describe the most common parameters with examples;


Instance 1: no parameters are attached or the query user name is specified. The-S parameter is added by default;

[beinan@localhost ~]$ finger
Login     Name            Tty      Idle  Login Time   Office     Office Phone
beinan    beinan sun    tty1     1:39  Nov  2 08:27
linuxsir  linuxsir open   tty2        2  Nov  2 10:03 linuxsir o +1-389-866-771

 

Equivalent command

 

[beinan@localhost ~]$ finger  -s

Note:Without adding any parameters or specifying the user to be queried, finger outputs the usernames (also known as login) of all users logging on to the local machine using the short grid-S by default) real name, terminal (TTY), write status, idle, login time, office location, office phone number, etc;

 

In this example, a beinan user logs on and the real name is beinan Sun (this name is the real name of the user. It is not displayed if it is not set when a user is added ), log on to the tty1 terminal. The empty closing time is 1 minute 39 seconds. The logon time is Nov/2/. There is no office name or office phone number;

Let's take a look at the linuxsir user information in this example based on the description recorded by the beinan user. It should not be difficult.

For the write status, if there is no output after tty, it indicates writing is in progress. If * appears, it indicates no writing or is forbidden. For example, in the following example, the FTP user did not log on to the system through the terminal because the TTY is * and there is a * behind the TTY, which indicates that writing is prohibited or not written (when the user is not logged on );

[beinan@localhost ~]$ finger -s ftp
Login     Name       Tty      Idle  Login Time   Office     Office Phone
ftp       FTP User    *     *  No logins

 

We can query a user's information in short format and output it in short format, for example, the following example;

[beinan@localhost ~]$ finger  -s   beinan

 

Example 2:Example of output-l parameter for user information in long format;

Finger-l if you do not add a user name, you can list all user information that is logged on through TTY. If you want to query a user, you can specify one or more users; what is tty login? If you operate on the full-screen text interface, you can log on to the host by pressing CTRL + F2 or Ctrl + F3 or Ctrl + F4, you will see that each user has a different tty;

 

[Beinan @ localhost ~] $ Finger-l
[Beinan @ localhost ~] $ Finger-l beinan linuxsir Note: several user information can be queried at the same time and output in long format;

 

 

[beinan@localhost ~]$ finger beinan
Login: beinan                           Name: beinan sun
Directory: /home/beinan                 Shell: /bin/bash
On since Wed Nov  2 08:27 (CST) on tty1    2 hours 29 minutes idle
On since Wed Nov  2 10:50 (CST) on pts/0 from :0.0
No mail.
No Plan.

 

In this example, the user to be queried is beinan and the real name is beinan sun. The home directory is located at/home/beinan, and the shell type is Bash. Then, the user logs on to the database through which terminal, logon Time, whether there are emails or plans;

Example 3:Example of parameter combination;

 

[beinan@localhost ~]$ finger -lp   beinan
Login: beinan                           Name: beinan sun
Directory: /home/beinan                 Shell: /bin/bash
On since Wed Nov  2 08:27 (CST) on tty1    2 hours 36 minutes idle
On since Wed Nov  2 10:50 (CST) on pts/0 from :0.0
No mail.

 

Note:Query beinan user information, which is output in long format without outputting. Plan and. project content;

Example 4:Comparison between finger-s and W and WHO;

For finger, there are so many simple tools. When using the-S parameter, you 'd better compare it with W and who tools, let's take a look at the similarities and differences between finger-s and W and who outputs. W and who query which users log on to the host. What about finger-s, users can check whether they log on or do not log on. However, the content is different. Let's take a look at the example;

 

[beinan@localhost ~]$ finger -s
Login     Name            Tty      Idle  Login Time   Office     Office Phone
beinan    beinan sun      tty1     3:03  Nov  2 08:27
beinan    beinan sun      pts/0          Nov  2 10:50 (:0.0)
linuxsir  linuxsir open   tty2     1:26  Nov  2 10:03 linuxsir o +1-389-866-771

[beinan@localhost ~]$ w
11:30:36 up  3:04,  3 users,  load average: 0.30, 0.15, 0.10
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
beinan   tty1     -                08:27    3:03m  2:52   0.00s /bin/sh /usr/X11R6/bin/startx
linuxsir tty2     -                10:03    1:26m  0.01s  0.01s -bash
beinan   pts/0    :0.0             10:50    0.00s  0.16s  0.00s w

[beinan@localhost ~]$ who
beinan   tty1         Nov  2 08:27
linuxsir tty2         Nov  2 10:03
beinan   pts/0        Nov  2 10:50 (:0.0)

 


3. user tools used to query logon hosts: W, WHO, and users

The W, WHO, and users tools are used to query users who have logged on to the current host. In addition, finger-S can also be used to query data. The focus is different. Please compare them with others. After all, they are simple, this is just an introduction;

 

[beinan@localhost ~]$ w
12:09:56 up  3:43,  7 users,  load average: 0.16, 0.10, 0.04
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
beinan   tty1     -                08:27    3:42m  3:09   0.00s /bin/sh /usr/X11R6/bin/startx
linuxsir tty2     -                10:03    2:06m  0.01s  0.01s -bash
beinan   pts/0    :0.0             11:36    1:09   0.15s  0.15s bash
beinan   pts/1    :0.0             11:37    1:12   0.21s  0.21s bash
beinan   pts/2    :0.0             12:02    6:52   0.09s  0.09s bash
beinan   pts/3    :0.0             12:05   12.00s  0.11s  0.06s ssh xmbnnbdl@linuxsir.org -p 17007
beinan   pts/4    :0.0             12:06    0.00s  0.21s  0.00s w

[beinan@localhost ~]$ who
beinan   tty1         Nov  2 08:27
linuxsir tty2         Nov  2 10:03
beinan   pts/0        Nov  2 11:36 (:0.0)
beinan   pts/1        Nov  2 11:37 (:0.0)
beinan   pts/2        Nov  2 12:02 (:0.0)
beinan   pts/3        Nov  2 12:05 (:0.0)
beinan   pts/4        Nov  2 12:06 (:0.0)

[beinan@localhost ~]$ users
beinan beinan beinan beinan beinan beinan linuxsir

 


4. query the user group to which the groups user belongs;

Groups is easy to use, that is, to query which user group or which user group the user belongs;

 

Syntax format: groups User Name

 

Instance:

 

[Beinan @ localhost ~] $ Groups beinan Note: queries the user group to which beinan belongs;

Beinan: beinan Note: beinan is a member of the beinan user group;
[Beinan @ localhost ~] $ Groups linuxsir Note: query the user group to which the linuxsir user belongs;
Linuxsir: linuxsir root beinan Note: The linuxsir user is a member of the linuxsir user group, beinan user group, and root user group;

 

Groups is mainly used to query the names of user groups to which users belong. It is best to compare them with the id command. This gives you an understanding of both tools;


Iii. postscript;

This article is a very simple document that can be viewed in ten minutes. But for me, if I want to write an entry-level series of documents, I have to simply tell beginners, isn't it? At the time of writing this article, I listed the tool used to query users as the document "Introduction to Linux User and User Group Management Tools", but it was too long to write and found the document, therefore, we have to make the user's query an independent article first;

My next document plan is to write related tools for user management, such as adding, deleting, and modifying tools. If it succeeds, it will be completed in the next two days, however, this is only a plan; I also said that it would take one or two days to write super permission control, but I did write it for twelve days;

4. participate in correcting this article;

In the future this document, I would like you to take part in the correction. After all, there will certainly be problems with what a person writes. Several Heads will always be much better than one head. Please give me more advice; for example, the working principle of the user query tool in this article may be inaccurate or insufficient. In addition, the principle of searching online users by W and who is not mentioned, if any of you have time, you can list the tools used to query online users as documents. Thank you;


5. Thank you;


Vi. related documents;

Linux User and user group management overview
User and group configuration file details
Linux user query
Introduction to Linux User management tools
Control of super permissions in Linux
Procedure for batch adding users in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.