


How to check how many users there are in Linux
linux查看有多少用户的方法:1、打开终端命令窗口;2、通过查看“/etc/passwd”文件即可看到所有用户信息了。
本文操作环境:linux5.9.8系统、Dell G3。
linux怎么查看有多少用户?
查看linux中所有用户的三种方式
通过使用 /etc/passwd 文件,getent 命令,compgen 命令这三种方法查看系统中用户的信息。
Linux里查看所有用户,在终端里.其实只需要查看 /etc/passwd文件就行了.
看第三个参数:500以上的,就是后面建的用户了.其它则为系统的用户.或者用cat /etc/passwd |cut -f 1 -d :
大家都知道,Linux 系统中用户信息存放在 /etc/passwd 文件中。
这是一个包含每个用户基本信息的文本文件。当我们在系统中创建一个用户,新用户的详细信息就会被添加到这个文件中。
/etc/passwd 文件将每个用户的基本信息记录为文件中的一行,一行中包含 7 个字段。
/etc/passwd 文件的一行代表一个单独的用户。该文件将用户的信息分为 3 个部分。
* 第 1 部分:`root` 用户信息
* 第 2 部分:系统定义的账号信息
* 第 3 部分:真实用户的账户信息
第一部分是 root 账户,这代表管理员账户,对系统的每个方面都有完全的权力。
第二部分是系统定义的群组和账户,这些群组和账号是正确安装和更新系统软件所必需的。
第三部分在最后,代表一个使用系统的真实用户。
在创建新用户时,将修改以下 4 个文件。
* `/etc/passwd`: 用户账户的详细信息在此文件中更新。
* `/etc/shadow`: 用户账户密码在此文件中更新。
* `/etc/group`: 新用户群组的详细信息在此文件中更新。
* `/etc/gshadow`: 新用户群组密码在此文件中更新。
方法 1 :使用 /etc/passwd 文件
使用任何一个像 cat、more、less 等文件操作命令来打印 Linux 系统上创建的用户列表。
/etc/passwd 是一个文本文件,其中包含了登录 Linux 系统所必需的每个用户的信息。它保存用户的有用信息,如用户名、密码、用户 ID、群组 ID、用户 ID 信息、用户的家目录和 Shell 。
/etc/passwd 文件将每个用户的详细信息写为一行,其中包含七个字段,每个字段之间用冒号 : 分隔:
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin 2gadmin:x:500:10::/home/viadmin:/bin/bash apache:x:48:48:Apache:/var/www:/sbin/nologin zabbix:x:498:499:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin mysql:x:497:502::/home/mysql:/bin/bash zend:x:502:503::/u01/zend/zend/gui/lighttpd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin 2daygeek:x:503:504::/home/2daygeek:/bin/bash named:x:25:25:Named:/var/named:/sbin/nologin mageshm:x:506:507:2g Admin - Magesh M:/home/mageshm:/bin/bash
7 个字段的详细信息如下。
用户名 (magesh): 已创建用户的用户名,字符长度 1 个到 12 个字符。
密码(x):代表加密密码保存在 `/etc/shadow 文件中。
**用户 ID(506):代表用户的 ID 号,每个用户都要有一个唯一的 ID 。UID 号为 0 的是为 root 用户保留的,UID 号 1 到 99 是为系统用户保留的,UID 号 100-999 是为系统账户和群组保留的。
**群组 ID (507):代表群组的 ID 号,每个群组都要有一个唯一的 GID ,保存在 /etc/group文件中。
**用户信息(2g Admin - Magesh M):代表描述字段,可以用来描述用户的信息(LCTT 译注:此处原文疑有误)。
**家目录(/home/mageshm):代表用户的家目录。
**Shell(/bin/bash):代表用户使用的 shell 类型。
你可以使用 awk 或 cut 命令仅打印出 Linux 系统中所有用户的用户名列表。显示的结果是相同的。
# awk -F':' '{ print $1}' /etc/passwd or # cut -d: -f1 /etc/passwd root bin daemon adm lp sync shutdown halt mail ftp postfix sshd tcpdump 2gadmin apache zabbix mysql zend rpc 2daygeek named mageshm
方法 2 :使用 getent 命令
getent 命令显示 Name Service Switch 库支持的数据库中的条目。这些库的配置文件为 /etc/nsswitch.conf。
getent 命令显示类似于 /etc/passwd 文件的用户详细信息,它将每个用户详细信息显示为包含七个字段的单行。
# getent passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin 2gadmin:x:500:10::/home/viadmin:/bin/bash apache:x:48:48:Apache:/var/www:/sbin/nologin zabbix:x:498:499:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin mysql:x:497:502::/home/mysql:/bin/bash zend:x:502:503::/u01/zend/zend/gui/lighttpd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin 2daygeek:x:503:504::/home/2daygeek:/bin/bash named:x:25:25:Named:/var/named:/sbin/nologin mageshm:x:506:507:2g Admin - Magesh M:/home/mageshm:/bin/bash
7 个字段的详细信息如上所述。(LCTT 译注:此处内容重复,删节)
你同样可以使用 awk 或 cut 命令仅打印出 Linux 系统中所有用户的用户名列表。显示的结果是相同的。
方法 3 :使用 compgen 命令
compgen 是 bash 的内置命令,它将显示所有可用的命令,别名和函数。
# compgen -u root bin daemon adm lp sync shutdown halt mail ftp postfix sshd tcpdump 2gadmin apache zabbix mysql zend rpc 2daygeek named mageshm
推荐学习:《linux视频教程》
The above is the detailed content of How to check how many users there are in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.
