查看用户、组相关列表
# 查看用户列表 以冒号分隔 分别表示
# 用户名:password:UID:GID:描述信息:用户家目录:shell
[root@m-2 ~]# tail -3 /etc/passwd
openvswitch:x:997:995:Open vSwitch Daemons:/:/sbin/nologin
chrony:x:996:994::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
# 查看组列表 冒号分隔 分别表示
# 组名:密码占位符:GID:组内用户列表
[root@m-2 ~]# tail -3 /etc/group
tcpdump:x:72:
cgred:x:993:
docker:x:992:
# 查看用户密码相关信息 冒号分隔 分别表示
# 用户名:密码:最近一次次修改密码时间:密码最短使用期限:最长使用期限:警告时段:非活动时段:过期时间:保留字段
[root@m-2 ~]# head -3 /etc/shadow
root:$6$adtoO3JJcJzzbzyt$3q4BMuZdgcoAAbDot2IpY2Gz::0:99999:7:::
bin:*:17703:0:99999:7:::
daemon:*:17703:0:99999:7:::
组操作
创建组:groupadd -g GID -p PASSWORD -r GROUP_NAME
[root@m-2 ~]# groupadd --help
Usage: groupadd [options] GROUP
Options:
-g, --gid GID use GID for the new group
-p, --password PASSWORD use this encrypted password for the new group
-r, --system create a system account
# 创建组
[root@m-2 ~]# groupadd -g 800 -p 123 -r testgrp
[root@m-2 ~]# tail -1 /etc/group
testgrp:x:800:
[root@m-2 ~]# tail -1 /etc/gshadow
testgrp:123::
修改组:groupmod -g GID -n NEW_GROUP -p PASSWORD
[root@m-2 home]# groupmod --help
Usage: groupmod [options] GROUP
Options:
-g, --gid GID change the group ID to GID
-n, --new-name NEW_GROUP change the name to NEW_GROUP
-p, --password PASSWORD change the password to this (encrypted)
PASSWORD
[root@m-2 ~]# tail -1 /etc/group
testgrp2:x:810:
[root@m-2 ~]# tail -1 /etc/gshadow
testgrp2:456::
删除组:groupdel GROUP
用户操作
添加用户:useradd
# adduser 链接到了useradd
[root@m-2 ~]# which adduser
/usr/sbin/adduser
[root@m-2 ~]# file `which adduser`
/usr/sbin/adduser: symbolic link to `useradd'
# 添加用户
[root@m-2 ~]# useradd --help
Usage: useradd [options] LOGIN
useradd -D # 显示用户默认配置
useradd -D [options] # 修改配置
Options:
-d, --home-dir HOME_DIR home directory of the new account
# 添加用户 不指定用户家目录 默认从/etc/skel克隆
[root@m-2 ~]# useradd testuser
[root@m-2 ~]# tail -1 /etc/passwd
testuser:x:1000:1000::/home/testuser:/bin/bash
[root@m-2 ~]# tail -1 /etc/group
[root@m-2 ~]# ls -a /home/testuser/
. .. .bash_logout .bash_profile .bashrc
# 和上面新建用户家目录一样
[root@m-2 ~]# ls -a /etc/skel
. .. .bash_logout .bash_profile .bashrc
# 指定家目录
[root@m-2 ~]# useradd -d /tmp/test test2
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
# 可以看到 创建成功
[root@m-2 ~]# tail -2 /etc/passwd
testuser:x:1000:1000::/home/testuser:/bin/bash
test2:x:1001:1001::/tmp/test:/bin/bash
[root@m-2 ~]# tail -2 /etc/group
testuser:x:1000:
test2:x:1001:
修改用户:usermod
[root@m-2 ~]# usermod --help
Usage: usermod [options] LOGIN
# -m -d 一起使用 家目录原有文件可保留
# -aG 原有属组不会被删除
Options:
-d, --home HOME_DIR new home directory for the user account
-G, --groups GROUPS new list of supplementary GROUPS
-a, --append append the user to the supplemental GROUPS
mentioned by the -G option without removing
him/her from other groups
-m, --move-home move contents of the home directory to the
new location (use only with -d)
删除用户:userdel
[root@m-2 ~]# userdel --help
Usage: userdel [options] LOGIN
# -r 一并删除用户家目录
Options:
-r, --remove remove home directory and mail spool