本章内命令很多需要 root 权限,且具有破坏性和危险性。
不同发行版中,本章内的命令有一些差别。我尽量讲解 Ubuntu 和 CentOS 之间,这些命令之间的差别,但不代表面面俱到。
参考的两个发行版:
- Ubuntu 20.04 LTS (shadow-utils 4.8.1)
- CentOS Linux release 7.9.2009
操作前最好查看对应命令的文档,以免误操作。
一些命令的操作权限受配置文件影响,如无说明均为默认情况。
用户与用户组是多对多的关系。
一个用户所属的用户组,按建立时的情况可分为:
用户同一会话中会有一个有效用户组。
创建用户时,默认会创建一个同名的用户组,作为初始用户组,也是用户登录时的有效用户组。
用户可以在会话中更改有效用户组(实质上是另外登录)。
系统中只记录 UID 和 GID,一般不会记录其名称。
用户名和组名不能以数字开头。
类型 | 文件 | 查看 | 新增 | 修改(全部) | 删除 | 设定(部分) | 切换 |
---|---|---|---|---|---|---|---|
用户 | /etc/passwd /etc/shadow /etc/sudoers /etc/default/useradd /etc/login.defs /etc/skel/ ****… |
id finger w who whoami |
useradd adduser *newusers *** |
usermod newusers *** |
userdel deluser ** |
passwd chpasswd ***chage chfn chsh visudo |
su sudo |
用户组 | /etc/group /etc/gshadow |
groups |
groupadd addgroup ** |
groupmod |
groupdel delgroup ** |
gpasswd |
newgrp |
useradd
/etc/default/useradd
内参数的变化而变化。对比版本:
用户部分只讲 /etc/passwd
、/etc/shadow
。
# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
foxconn@ding-server:~$ ll /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/sudoers
-rw-r--r-- 1 root root 969 May 20 10:31 /etc/group
-rw-r----- 1 root shadow 805 May 20 10:31 /etc/gshadow
-rw-r--r-- 1 root root 2368 May 20 10:31 /etc/passwd
-rw-r----- 1 root shadow 1834 May 20 10:32 /etc/shadow
-r--r----- 1 root root 755 Feb 3 2020 /etc/sudoers
# CentOS Linux release 7.9.2009
foxconn@ding-server:~$ ll /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/sudoers
-rw-r--r--. 1 root root 1.2K May 19 11:28 /etc/group
----------. 1 root root 925 May 19 11:28 /etc/gshadow
-rw-r--r--. 1 root root 2.9K May 20 10:14 /etc/passwd
----------. 1 root root 1.5K May 20 10:14 /etc/shadow
-r--r-----. 1 root root 4.4K Dec 28 2018 /etc/sudoers
root 基本上不受普通权限的影响,故仍然可以控制。
/etc/passwd
foxconn@ding-server:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...
foxconn:x:1000:1000:foxconn:/home/foxconn:/bin/bash
hello:x:1003:1003:Hello World,114514,1919-810,12345678901,This is an example:/home/hello:/bin/bash
各列以 :
分隔,每列含义:
x
表示密码记录在 /etc/shadow
0
,表示是系统管理员1
~999
之间,表示是保留给系统用的用户(一些网络和后台服务不希望使用 root 权限运行,故这么设置。一般设置为不可登录)1000
,表示是一般用户姓名,门牌号,办公室电话,家庭电话,其他说明
姓名,门牌号,办公室电话,家庭电话
/usr/sbin/nologin
表示登录时无法获得 Shell 环境,也就是不可登录/etc/shadow
foxconn@ding-server:~$ sudo cat /etc/shadow
root:*:19046:0:99999:7:::
daemon:*:19046:0:99999:7:::
foxconn:$6$W7f...jh0:19083:0:99999:7:::
...
各列含义:
3~8 项中,绝对天数从 1970-01-01 起计算。换算方法:
foxconn@ding-server:~$ date -d "1970-01-01 19083 days"
Fri 01 Apr 2022 12:00:00 AM CST
foxconn@ding-server:~$ echo $(($(date -d "2022-04-01" +%s) / 86400 + 1 ))
19083
/etc/group
foxconn@ding-server:~$ cat /etc/group
root:x:0:
daemon:x:1:
...
docker:x:117:foxconn,root
foxconn:x:1000:
各列含义:
/etc/gshadow
/etc/gshadow
foxconn@ding-server:~$ sudo cat /etc/gshadow
root:*::
adm:*::syslog,foxconn
foxconn:!::
docker:!::foxconn,root
...
各列含义:
id
- 查看用户信息id [选项] [用户名]
不添加用户名,表示查看自己的信息。
选项:
-u
:仅显示 UID-g
:仅显示初始用户组 GID-G
:仅显示所有用户组 GID-n
:与上述选项合用,显示名称而非 ID例:
foxconn@ding-server:~$ id
uid=1000(foxconn) gid=1000(foxconn) groups=1000(foxconn),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lxd),117(docker)
foxconn@ding-server:~$ id -u
1000
foxconn@ding-server:~$ id -g
1000
foxconn@ding-server:~$ id -G
1000 4 24 27 30 46 116 117
foxconn@ding-server:~$ id -Gn
foxconn adm cdrom sudo dip plugdev lxd docker
finger
- 用户详细信息查看能够查看的信息:
~/.plan
的内容)例:
foxconn@ding-server:~$ finger hello
Login: hello Name: Hello World
Directory: /home/hello Shell: /bin/bash
Office: 114514, 1919-810 Home Phone: +1-234-567-8901
On since Thu May 19 16:57 (CST) on pts/2 from 10.94.5.157
4 seconds idle
New mail received Thu May 19 16:57 2022 (CST)
Unread since Thu May 19 16:56 2022 (CST)
Plan:
- Watch movie
- Eat
- Sleep
useradd
- 添加用户useradd [选项] 用户名
需 root 权限。
选项:
-D
:显示默认设置(此时不用写用户名)-u UID
:自定义 UID-g 组名
:自定义初始用户组(passwd:4
)-G 组名
:自定义附加用户组(group:4
)-M
:不创建家目录(系统账号默认值;Debian 系默认值)-m
:创建家目录(普通账号默认值)-c 说明
:添加用户信息说明(passwd:5
)-n
:不建立与用户名同名的用户组-d 绝对路径
:自定义家目录路径(passwd:6
)-r
:建立系统账号-s Shell路径
:自定义默认 Shell(passwd:7
)-e YYYY-MM-DD
:账号失效日期(shadow:8
)-f 数字
:指定密码是否失效(shadow:7
)
0
:立即失效-1
:永不失效这里的
passwd:4
指该选项影响/etc/passwd
文件中对应用户的第4
列,下同。
/etc/default/useradd
(用 useradd -D
也能查看)GROUP=100 # 默认用户组
HOME=/home # 默认家目录
INACTIVE=-1 # 密码失效日(shadow:7)
EXPIRE= # 账号失效日(shadow:8)
SHELL=/bin/sh # 默认 Shell
SKEL=/etc/skel # 用户家目录模板目录
CREATE_MAIL_SPOOL=no # 是否主动创建邮箱目录
/etc/login.defs
MAIL_DIR /var/mail # 用户邮箱目录
FAILLOG_ENAB yes
LOG_UNKFAIL_ENAB no
LOG_OK_LOGINS no
SYSLOG_SU_ENAB yes
SYSLOG_SG_ENAB yes
FTMP_FILE /var/log/btmp
SU_NAME su
HUSHLOGIN_FILE .hushlogin
ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
TTYGROUP tty
TTYPERM 0600
ERASECHAR 0177
KILLCHAR 025
UMASK 022 # 家目录建立时的 umask
PASS_MAX_DAYS 99999 # 密码需要重置的日期(shadow:5)
PASS_MIN_DAYS 0 # 密码不可被修改的天数(shadow:4)
PASS_WARN_AGE 7 # 密码变更期限前的警告日期(shadow:6)
UID_MIN 1000 # 普通用户 UID 最小值
UID_MAX 60000 # 普通用户 UID 最大值
GID_MIN 1000 # 普通用户组 GID 最小值
GID_MAX 60000 # 普通用户组 GID 最大值
LOGIN_RETRIES 5
LOGIN_TIMEOUT 60
CHFN_RESTRICT rwh
DEFAULT_HOME yes
USERGROUPS_ENAB yes # 用 userdel 删除用户时,是否会删除初始用户组
ENCRYPT_METHOD SHA512 # 密码加密机制
/etc/skel/
(用户家目录模板)usermod
- 修改用户usermod [选项] 用户名
需 root 权限,且被修改用户应当彻底退出登录。
选项:
-c 说明
:修改用户信息说明(passwd:5
)-d 绝对路径
:修改家目录路径(passwd:6
)-e YYYY-MM-DD
:修改账号失效日期(shadow:8
)-f 数字
:修改密码失效天数(shadow:7
)
0
:立即失效-1
:永不失效userdel
- 删除用户userdel [选项] 用户名
仅在确实不需要用户的情况下删除,如果只是想暂时禁用,请修改用户失效日期。
需 root 权限,且被删除用户应当彻底退出登录。
选项:
-r
:连同用户的家目录也一并删除影响范围:
/etc/passwd
/etc/shadow
/etc/group
/etc/gshadow
-r
时)passwd
- 修改密码passwd [选项] [用户名]
不填用户名,表示修改自己的密码等信息。
普通用户:仅可修改自己的密码;需要输入旧密码;部分选项不可用;可能有复杂性验证。
root 用户:可以修改所有人的密码;不需要输入旧密码;右边的全部选项可用;无复杂性验证。
如误进入该命令,按 Ctrl + D 退出,Ctrl + C 无效。
选项:
-S
:列出密码相关参数-l
:在加密后密码前加上 !
,以锁定用户-u
:解锁用户(与上一个互逆)-n 天数
:多久不可修改密码(shadow:4
)-x 天数
:多久内必须修改密码(shadow:5
)-w 天数
:密码过期前的警告天数(shadow:6
)-i 日期
:密码失效日期(shadow:7
)# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
foxconn@ding-server:~$ passwd -S
foxconn P 04/01/2022 0 99999 7 -1
foxconn@ding-server:~$ sudo passwd -S
root L 02/23/2022 0 99999 7 -1
hello NP 05/19/2022 0 99999 7 -1
# CentOS Linux release 7.9.2009
hello PS 2022-05-19 0 99999 7 -1 (Password set, SHA512 crypt.)
hello LK 2022-05-19 0 99999 7 -1 (Password locked.)
lp LK 2016-11-05 0 99999 7 -1 (Alternate authentication scheme in use.)
hello NP 2022-05-19 0 99999 7 -1 (Empty password.)
各列含义:
用户名
密码状态
L
/ LK
:锁定NP
:无密码P
/ PS
:有可用密码上次更改密码日期(shadow:3
,格式化日期)
密码不可修改日(shadow:4
)
密码需要重置的日期(shadow:5
)
密码变更期限前的警告日期(shadow:6
)
账号失效日(shadow:7
)
-1
:永不失效passwd
可以接受标准输入,故可以利用重定向和管道传入密码,可用于自动化脚本。
passwd
有 --stdin
选项,可以方便地进行该操作,但是不同发行版中不一定支持,可以查看帮助文档。
需要注意密码泄露问题。
passwd
passwd [用户名] < 带密码的文件
# 或(普通用户)
echo -e "旧密码\n新密码\n新密码" | passwd [用户名]
# 或(root)
echo -e "新密码\n新密码" | passwd [用户名]
对于其中的 带密码的文件
,格式如下:
普通用户:
旧密码
新密码
新密码
root:
新密码
新密码
passwd --stdin [用户名] < 带密码的文件
# 或
echo "新密码" | passwd --stdin [用户名]
对于其中的 带密码的文件
,只需写新密码。
chage
- 修改密码日期相关部分chage [选项] 用户名
普通用户只能看自己的信息;root 用户可以进行全部操作。
选项:
-l
:列出用户的详细参数-d YYYY-MM-DD
:修改最近一次修改密码日期(shadow:3
)-m 天数
:修改密码不可被修改的天数(shadow:4
)-M 天数
:修改密码需要重置的天数(shadow:5
)-W 天数
:修改密码变更期限前的警告日期(shadow:6
)-I 天数
:修改密码在过期之后的宽限日期(shadow:7
)-E YYYY-MM-DD
:修改账号失效日(shadow:8
)例:
foxconn@ding-server:~$ sudo chage -d 2022-05-19 hello
foxconn@ding-server:~$ sudo chage -E 2023-05-19 hello
foxconn@ding-server:~$ sudo chage -I 5 hello
foxconn@ding-server:~$ sudo chage -m 30 hello
foxconn@ding-server:~$ sudo chage -M 180 hello
foxconn@ding-server:~$ sudo chage -W 5 hello
foxconn@ding-server:~$ sudo chage -l hello
Last password change : May 19, 2022
Password expires : Nov 15, 2022
Password inactive : Nov 20, 2022
Account expires : May 19, 2023
Minimum number of days between password change : 30
Maximum number of days between password change : 180
Number of days of warning before password expires : 5
需要给用户一个初始密码,首次登录时必须修改密码,如何处理?
foxconn@ding-server:~$ sudo useradd -m -s /bin/bash 用户
useradd: warning: the home directory /home/用户 already exists.
useradd: Not copying any file from skel directory into it.
foxconn@ding-server:~$ echo -e "初始密码\n初始密码" | sudo passwd 用户 # 给初始密码
New password: Retype new password: passwd: password updated successfully
foxconn@ding-server:~$ sudo chage -d 0 用户 # 首次登录需修改密码
foxconn@ding-server:~$ sudo chage -l 用户 | head -n 3
Last password change : password must be changed
Password expires : password must be changed
Password inactive : password must be changed
# 发起 SSH 连接
用户@主机地址's password: # 此处输入初始密码
You are required to change your password immediately (administrator enforced)
...
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for 用户.
Current password: # 此处输入初始密码
New password: # 此处输入新密码
Retype new password: # 此处输入新密码
passwd: password updated successfully
Connection to 主机地址 closed.
# 发起 SSH 连接
用户@主机地址's password: # 此处输入新密码
...
用户@ding-server:~$
chfn
- 修改用户信息说明chfn [选项] [用户名]
普通用户只能修改自己的信息;root 用户可以修改任何人的全部信息。
选项:
-r 门牌号
:修改门牌号-w 电话
:修改办公室电话-h 电话
:修改家庭电话-f 姓名
:修改姓名-o 信息
:修改其他信息-f 姓名
:修改姓名-o 门牌号
:修改门牌号-p 电话
:修改办公室电话-h 电话
:修改家庭电话例:
# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
# 用 root 权限
foxconn@ding-server:~$ sudo chfn hello
Changing the user information for hello
Enter the new value, or press ENTER for the default
Full Name []: Hello World
Room Number [100]: 114514
Work Phone [200]: 1919-810
Home Phone [300]: 12345678901
Other []: Some info
# 用 hello 登录
hello@ding-server:~$ chfn
Password: # 此处填入自己的密码
Changing the user information for hello
Enter the new value, or press ENTER for the default
Full Name: Hello World
Room Number [100]: 114514
Work Phone [200]: 1919-810
Home Phone [300]: 12345678901
# CentOS Linux release 7.9.2009
foxconn@ding-server:~$ sudo chfn hello
Changing finger information for hello.
Name []: Hello World
Office []: 114514
Office Phone []: 1919-810
Home Phone []: 12345678901
Finger information changed.
chsh
- 修改默认 Shellchsh [选项] [用户名]
普通用户只能修改自己的默认 Shell;root 用户可以修改任何人的。
如果输入的 Shell 不在 /etc/shells
里面:
更改在用户下次登录时生效。
选项:
-s Shell路径
:设置 Shell-l
:列出系统上可用的 Shell(相当于输出 /etc/shells
)例:
# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
# 用 root 权限
foxconn@ding-server:~$ sudo chsh hello
Changing the login shell for hello
Enter the new value, or press ENTER for the default
Login Shell [/bin/bash]: /bin/rbash
# 用 hello 登录
hello@ding-server:~$ chsh
Password: # 此处填入自己的密码
Changing the login shell for foxconn
Enter the new value, or press ENTER for the default
Login Shell [/bin/bash]: /bin/rbash
# CentOS Linux release 7.9.2009
foxconn@ding-server:~$ sudo chsh hello
Changing shell for hello.
New shell [/bin/fish]: /bin/zsh
Shell changed.
su
- 切换用户su [选项] [用户名]
root 切换到其他用户,不须输入密码;普通用户切换到其他用户,需要输入其他用户的密码。
如不指定用户名,则切换到 root。
选项:
-
:使用 Login Shell 的变量文件读取方式登录系统-l 用户名
:同上(用该参数,后面的用户名就不用再写了)-m
:使用目前的环境配置文件,而不读取新用户的配置文件-p
:同上-c 命令
:仅进行一次命令;命令执行完后回来# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
foxconn@ding-server:~$ sudo useradd hahaha
foxconn@ding-server:~$ echo -e "ha\nha" | sudo passwd hahaha
New password: Retype new password: passwd: password updated successfully
foxconn@ding-server:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \[\e]1337;CurrentDir=$(pwd)\a\]
foxconn@ding-server:~$ echo "${PWD} ${USER} ${SHELL}"
/home/foxconn foxconn /bin/bash
foxconn@ding-server:~$ su hahaha
Password:
\[\e]0;\u@\h: \w\a\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$ \[\e]1337;CurrentDir=/home/foxconn\a\] echo "${PWD} ${USER} ${SHELL}"
/home/foxconn hahaha /bin/sh
\[\e]0;\u@\h: \w\a\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$ \[\e]1337;CurrentDir=/home/foxconn\a\] # 这里按了 Ctrl + D
# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
foxconn@ding-server:~$ su - hahaha
Password:
su: warning: cannot change directory to /home/hahaha: No such file or directory
$ echo "${PWD} ${USER} ${SHELL}"
/home/foxconn hahaha /bin/sh
$ # 这里按了 Ctrl + D
foxconn@ding-server:~$ sudo mkdir -p /home/hahaha
foxconn@ding-server:~$ sudo chown -R hahaha:hahaha /home/hahaha
foxconn@ding-server:~$ su - hahaha
Password:
$ echo "${PWD} ${USER} ${SHELL}"
/home/hahaha hahaha /bin/sh
$ # 这里按了 Ctrl + D
foxconn@ding-server:~$ su -m hahaha
Password:
hahaha@ding-server:~$ echo "${PWD} ${USER} ${SHELL}"
/home/foxconn foxconn /bin/bash
总结:
命令 \ 变量 | PS1 |
PWD |
USER |
SHELL |
---|---|---|---|---|
su |
不变 | 不变 | 变 | 变 |
su - |
变 | 存在时变 | 变 | 变 |
su -m |
不变 | 不变 | 不变 | 不变 |
PS1
的变化# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
foxconn@ding-server:~$ sudo su -
[sudo] password for foxconn:
root@ding-server:~# logout
foxconn@ding-server:~$
一般情况,默认配置下的 bash 中,PS1
的最后一个字符
$
表示普通用户#
表示 rootsudo
- 以其他用户身份执行命令sudo [选项] [命令]
操作者为普通用户,且操作者和要切换到的用户不同时,需要输入自己的密码;之后一段时间内(默认是 5 min)再执行,就不需要输入密码了。
操作者为 root,或操作者和要切换到的用户相同时,不需输入密码。
操作前,会检查 /etc/sudoers
中对应用户是否有权限执行 sudo
。
选项:
-b
:将后续命令放在后台中运行-u 用户名或UID
:要切换到的用户(如不写则为 root)-g 用户组名或GID
:要切换到的用户组(如不写则为用户的默认用户组)-l
:查看自己有什么 sudo
相关的权限(这时候后面不加命令)-U 用户名
:与 -l
配合,查看对应用户有什么 sudo
相关的权限-k
:下次使用 sudo
时,一定需要输入密码;如果后面有命令,这次也要输入密码/etc/sudoers
两例与 visudo
/etc/sudoers
:
# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
root ALL=(ALL:ALL) ALL
%admin ALL=(ALL) ALL
%sudo ALL=(ALL:ALL) ALL
# CentOS Linux release 7.9.2009
Defaults !visiblepw
Defaults always_set_home
Defaults match_group_by_gid
Defaults always_query_group_plugin
Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
Defaults env_keep += "http_proxy https_proxy ftp_prxoy no_proxy"
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
root ALL=(ALL) ALL
foxconn ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
不推荐直接编辑该文件,而是用 visudo
编辑(需 root 权限)。
实质上是用 Vim 编辑该文件,但是保存时会检查语法。
/etc/sudoers
配置用户名或%用户组名 登录来源主机名=(可切换用户:可切换用户组) 可执行命令
:可切换用户组
不必填,如果不填,则 sudo
时不能使用 -g
。
使某用户可在任意位置,以任意用户执行 sudo
,使用所有有权限的命令:
用户名 ALL=(ALL) ALL
使某用户组可在任意位置,以任意用户执行 sudo,使用所有有权限的命令:
%用户组名 ALL=(ALL) ALL
默认情况下,Ubuntu 中有 admin
、sudo
组,CentOS 中有 wheel
用户组,将用户加入该组即可获得上述权限。
免密码使用 sudo
:
用户名或%用户组名 登录来源主机名=(可切换身份) NOPASSWD: 可执行命令
只能以 root 权限使用 passwd
命令(要用绝对路径):
用户名或%用户组名 登录来源主机名=(root) /usr/bin/passwd
想让用户不能修改 root 密码,命令部分(即上例中 /usr/bin/passwd
部分)应该写:
!/usr/bin/passwd, /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root
其中 !
表示不能执行。
别名要用大写。
User_Alias UA = user1, user2, user3
Cmnd_Alias CA = !/usr/bin/passwd, /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root
Host_Alias HA = ding-server
UA HA=(root) CA
w
、who
- 查看当前登录用户和状态w
foxconn@ding-server:~$ w
14:25:11 up 25 min, 3 users, load average: 1.79, 1.66, 3.02
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
foxconn pts/0 10.94.5.157 14:22 0.00s 0.21s 0.02s w
hello pts/1 10.94.5.157 14:23 47.00s 0.18s 0.01s ping 10.94.0.154
hi pts/2 10.94.5.157 14:24 1:06 0.17s 0.17s -bash
选项:
-h
:不显示 uptime 和表头who
foxconn@ding-server:~$ who
foxconn pts/0 2022-05-23 14:22 (10.94.5.157)
hello pts/1 2022-05-23 14:23 (10.94.5.157)
hi pts/2 2022-05-23 14:24 (10.94.5.157)
后加任意两个单词,可查看自己的信息:
foxconn@ding-server:~$ who am i
foxconn pts/0 2022-05-23 14:22 (10.94.5.157)
groupadd
- 新增用户组groupadd [选项] 组名
选项:
-g GID
:定义 GID-r
:建立系统用户组groupmod
- 修改用户组groupmod [选项] 组名
选项:
-g GID
:修改 GID-n 新组名
:修改组名groupdel
- 删除用户组groupdel 组名
gpasswd
- 设定、修改用户组gpasswd [选项] 组名
选项:
-a 用户名
:将用户加入组-d 用户名
:将用户从组中删除-r
:删除密码-R
:失效密码-A 用户名
:将用户设置为组管理员-M 用户1,用户2...
:设置组成员groups
- 查看用户所属用户组foxconn@ding-server:~$ groups
foxconn adm cdrom sudo dip plugdev lxd docker
foxconn@ding-server:~$ groups root
root : root docker
第一项为有效用户组。
newgrp
- 切换有效用户组实质上是以另一个 Shell 登录,故变量等的情况与子进程的情况一致。
foxconn@ding-server:~$ groups
foxconn adm cdrom sudo dip plugdev lxd docker
foxconn@ding-server:~$ newgrp docker
# 这里实际上新登录了一个 bash
foxconn@ding-server:~$ groups
docker adm cdrom sudo dip plugdev lxd foxconn
foxconn@ding-server:~$ exit
# 退出,进入原来的 bash
foxconn@ding-server:~$ groups
foxconn adm cdrom sudo dip plugdev lxd docker
可以接受标准输入;需 root 权限。
newusers
- 批量添加 / 修改用户newusers [选项] [文件名]
文件中的每一行:
用户名:密码:UID:GID:GECOS:家目录:默认Shell
UID
、GID
新建时留空则为随机。
chpasswd
- 批量修改密码chpasswd < 带用户名和密码的文件
文件的每一行:
用户名:密码
其他形式:
echo "用户名:密码" | chpasswd
# 或
echo -e "用户名1:密码1\n用户名2:密码2" | chpasswd
在该发行版下,推荐使用这些命令;需 root 权限;仅介绍基本的使用方法。
adduser
- 交互式添加用户adduser [选项] 用户名
选项:
--uid UID
:自定义 UID--ingroup 组名
:定义初始用户组(passwd:4
)--no-create-home
:不要创建家目录--home 绝对路径
:自定义家目录路径--system
:建立系统账号--shell Shell路径
:自定义 Shell(passwd:6
)--disabled-login
:不设置密码,使创建的用户无法登录--disabled-password
:不设置密码,但可以使用非密码方式登录例:
# Ubuntu 20.04 LTS (shadow-utils 4.8.1)
foxconn@ding-server:~$ sudo adduser hello
Adding user `hello' ...
Adding new group `hello' (1003) ...
Adding new user `hello' (1003) with group `hello' ...
Creating home directory `/home/hello' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for hello
Enter the new value, or press ENTER for the default
Full Name []: Hello World
Room Number []: 114514
Work Phone []: 1919-810
Home Phone []: 12345678901
Other []: This is an example
Is the information correct? [Y/n] y
deluser
- 删除用户deluser [选项] 用户名
选项:
--remove-home
:删除用户的家目录和邮件目录--remove-all-files
:删除属于该用户的全部文件--backup
:删除上面的文件前,备份、打包文件至 /用户名.tar.bz2
或 /用户名.tar.gz
--backup-to 目录
:同上,但是放在指定的目录--system
:仅用户为系统用户时删除--force
:删除 root 用户(UID 为 0
)时需要加上这个addgroup
- 添加用户组addgroup [选项] 用户组名
选项:
--system
:建立系统用户组-gid GID
:自定义 GIDdelgroup
- 删除用户组delgroup [选项] 用户组名
如果存在使用该用户组为初始用户组的用户,则无法删除。
选项:
--system
:仅用户组为系统用户组时删除--only-if-empty
:仅用户组为空时删除root ALL=(ALL:ALL) ALL
and root ALL=(ALL) ALL
? - Ask Ubuntu