Linux下Bash控制台查看有哪些用户
查看有哪些用户的方法有:
- compgen -u
- 查看 /etc/passwd 如: cat /etc/passwd 或者 more /etc/passwd 或者 less /etc/passwd
- getent passwd
题外: 查看有哪些用户登录控制台可以用 w
命令
方法1: compgen -u
compgen -u
compgen 是一个很棒的 bash 内置命令
compgen -u | 列出所有用户的别名(非uid) Names of userAlias names |
compgen -g | 列出所有组名 Names of groups |
compgen -c | 列出可以运行的所有命令,类似连续按两次TAB键 Names of all commands |
compgen -a | 列出所有别名 Names of alias |
compgen -ac ls 或 compgen -ca ls | 列出以'ls'开头的所有可用命令或别名 |
compgen -A function | 列出可以运行的所有函数 |
compgen -k | 显示shell保留关键字 Names of Shell reserved words |
compgen -s | 有哪些服务 Names of service |
compgen -v | 列出变量 Names of shell variables |
compgen -e | Names of exported shell variables |
compgen -f | 列出所有函数名 Names of file and functions |
compgen -j | 累出所有任务 Names of job |
compgen 命令语法:
compgen [option] [word]
compgen [-abcdefgjksuv] [-o 选项] [-A 动作] [-G 全局模式] [-W 词语列表] [-F 函数] [-C 命令] [-X 过滤模式] [-P 前缀] [-S 后缀] [词语]
compgen 命令选项:
-a means Names of alias
-b means Names of shell builtins
-c means Names of all commands
-d means Names of directory
-e means Names of exported shell variables
-f means Names of file and functions
-g means Names of groups
-j means Names of job
-k means Names of Shell reserved words
-s means Names of service
-u means Names of userAlias names
-v means Names of shell variables
方法2 查看 /etc/passwd
head /etc/passwd
tail /etc/passwd
cat /etc/passwd
more /etc/passwd
less /etc/passwd
vi /etc/passwd
例如以下片段👇 ,来自win10自带的Ubuntu
👇来自VMware最小化安装的CentOS7
👇来自VMware安装的CentOS7桌面版
👇来自VMware安装的Ubuntu22.04
解释:
root | x | 0 | 0 | root | /root | /bin/bash |
用户名 | 密码,x | uid 用户id | gid 用户组id | 描述说明 | 用户主目录 | 命令解释程序 |
/etc/passwd每一个分段的含义: 使用冒号:分隔
-
用户名:
-
密码: x代表已加密
现在的Unix/Linux系统中,口令不再直接保存在passwd文件中,通常将passwd文件中的口令字段使用一个“x”来代替,将/etc /shadow作为真正的口令文件,用于保存包括个人口令在内的数据。当然shadow文件是不能被普通用户读取的,只有超级用户才有权读取。
一些系统中,存放着加密后的用户口令字。虽然这个字段存放的只是用户口令的加密串,不是明文,但是由于/etc/passwd文件对所有用户都可读,所以这仍是一个安全隐患。因此,现在许多Linux系统(如SVR4)都使用了shadow技术,把真正的加密后的用户口令字存放到/etc/shadow文件中,而在/etc/passwd文件的口令字段中只存放一个特殊的字符,例如“x”或者“*”。 -
UID:用户id
这就是使用者标示符,UId有以下限制:
ID 该ID使用的者特性
0 UID是0时,代表这个账号是【系统管理员】!所以当你的其他啊账号名称也具有root
的权限是,就将该账号的UID改为0即可,一个系统上面的系统管理不见得只有root,
不过不建议有多个UID为0的账号。
1~499 保留给系统使用的ID,默认500以下的数字给系统作为保利账号只是一个习惯。
由于系统上面启动的服务希望使用较小的权限去运行,应此不希望使用root的身份去执行
这些服务,所以我们就得要提供这些运行中的程序的拥有者账号才行,这些系统账号通常是不可登陆的,
所以才会有/sbin/nologin这个特殊的shell的存在。
500~ 一般使用者 -
GID:组id
-
用户信息说明:
-
家目录:
-
命令解释程序(Shell):Shell是当用户登录系统时运行的程序名称,
通常是一个Shell程序的全路径名,如/bin/bash。
方法3 getent passwd
getent passwd 和 查看/etc/passwd 基本相同
顺便说说
查看有哪些用户登录控制台的命令是 w
例如
[root@FedoraServer36 sshd_config.d]# w
08:25:38 up 58 min, 4 users, load average: 0.00, 0.02, 0.08
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
z tty1 07:27 50:25 0.04s 0.04s -bash
z pts/0 07:31 46:26 0.01s 0.01s -bash
root pts/1 07:36 1.00s 0.03s 0.00s w
z pts/2 07:41 44:26 0.01s 0.01s -bash
[root@FedoraServer36 sshd_config.d]# type w
w 已被录入哈希表 (/usr/bin/w)
[root@FedoraServer36 sshd_config.d]# which w
/usr/bin/w
[root@FedoraServer36 sshd_config.d]# w --help
Usage:
w [options]
Options:
-h, --no-header do not print header
-u, --no-current ignore current process username
-s, --short short format
-f, --from show remote hostname field
-o, --old-style old style output
-i, --ip-addr display IP address instead of hostname (if possible)
--help display this help and exit
-V, --version output version information and exit
For more details see w(1).
[root@FedoraServer36 sshd_config.d]#
向其它用户发消息可以用 write
和 wall
write
向指定用户发消息
write user001 #回车
输入要发送的信息后回车
按 两次CTRL+D 或者 一次CTRL+C
wall
向所有用户发消息
wall hello world 回车
加双引号多行
wall "这是多行消
来自root
第一行
第二行"
先执行wall
命令, 然后输入文本, 再按 Ctrl+d
发送
wall 回车
输入消息
按 `Ctrl+d` 发送