这是我参与11月更文挑战的第6天,活动详情查看:2021最后一次更文挑战
Linux 小知识 丨用户相关命令
大约 200+ 命令
账号管理
su root
创建用户
useradd [选项] 用户名
# 验证
su 用户名
用户口令(密码)
passwd [选项] 用户名
# 密码不能是一个回文(正读反读都一样的)
# 长度大于8位, 字母数字结合
修改用户
usermod [选项] 用户名
usermod #查看所有选项
Options:
-c, --comment COMMENT new value of the GECOS field
-d, --home HOME_DIR new home directory for the user account
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP as new primary group
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
the user from other groups
-h, --help display this help message and exit
-l, --login NEW_LOGIN new value of the login name
-L, --lock lock the user account
-m, --move-home move contents of the home directory to the
new location (use only with -d)
-o, --non-unique allow using duplicate (non-unique) UID
-p, --password PASSWORD use encrypted password for the new password
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s, --shell SHELL new login shell for the user account
-u, --uid UID new UID for the user account
-U, --unlock unlock the user account
-v, --add-subuids FIRST-LAST add range of subordinate uids
-V, --del-subuids FIRST-LAST remove range of subordinate uids
-w, --add-subgids FIRST-LAST add range of subordinate gids
-W, --del-subgids FIRST-LAST remove range of subordinate gids
-Z, --selinux-user SEUSER new SELinux user mapping for the user account
# 修改用户名
usermod -l newUsername oldUsername
Ctrl + D # 退出用户
exit # 退出
删除用户
userdel [选项] 用户名
userdel -r -f username
Options:
-f, --force force some actions that would fail otherwise
e.g. removal of user still logged in
or files, even if not owned by the user # 强制删除
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool # 删除主目录和邮件池
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-Z, --selinux-user remove any SELinux user mapping for the user
用户组
管理员权限
创建用户组
groupadd (选项) 用户组名
修改用户组
groupmod (选项) 用户组名
Usage: groupmod [options] GROUP
Options:
-g, --gid GID change the group ID to GID
-h, --help display this help message and exit
-n, --new-name NEW_GROUP change the name to NEW_GROUP # 重命名
-o, --non-unique allow to use a duplicate (non-unique) GID
-p, --password PASSWORD change the password to this (encrypted)
PASSWORD
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
查询用户所属组
groups 用户名
[root@VM-8-10-centos ~]# groups root
root : root
删除用户组
groupdel 用户组名
管理用户组内成员 gpasswd
语法
gpasswd (可选项) 组名
gpasswd 是 Linux 下的管理工具, 用于将一个用户添加到组或者从组中删除。
[root@VM-8-10-centos ~]# gpasswd
Usage: gpasswd [option] GROUP
Options:
-a, --add USER add USER to GROUP # 添加用户到组
-d, --delete USER remove USER from GROUP # 从组删除用户
-h, --help display this help message and exit
-Q, --root CHROOT_DIR directory to chroot into
-r, --delete-password remove the GROUP's password # 删除密码
-R, --restrict restrict access to GROUP to its members # 限制用户登入组,只有组中成员才可以用newgrp加入该组
-M, --members USER,... set the list of members of GROUP # 指定组成员和-A用途相似
-A, --administrators ADMIN,...
set the list of administrators for GROUP # 指定管理员
Except for the -A and -M options, the options cannot be combined.
# 创建用户
[root@VM-8-10-centos ~]# useradd user1
[root@VM-8-10-centos ~]# useradd user2
[root@VM-8-10-centos ~]# useradd user3
# 添加用户到用户组 devgroup
[root@VM-8-10-centos ~]# gpasswd -a user1 devgroup
Adding user user1 to group devgroup
[root@VM-8-10-centos ~]# gpasswd -a user2 devgroup
Adding user user2 to group devgroup
[root@VM-8-10-centos ~]# gpasswd -a user3 devgroup
Adding user user3 to group devgroup
# 验证: grep [组名] /etc/group
[root@VM-8-10-centos ~]# grep 'devgroup' /etc/group
devgroup:x:1004:user1,user2,user3 # 验证成功