文章目录
一、基础知识
Linux用户
管理员: root 0 (UID)
普通用户:1-65535
系统用户:1-499/1-999
登录用户: 500+ / 1000+
Linux组
管理员组: root 0
普通组:
系统组:1-499/1-999
登录用户组:500+ / 1000+
组类型:
基本组--主组: 组名会和用户名相同,且仅包含一个用户的组--私有组
附加组--额外组:一个用户可以属于多个附加组
Linux安全上下文
运行中的程序: 进程
进程能够访问的所有资源的权限在于进程发起者的用户身份
用户和组相关的配置文件
/etc/passwd : 用户及其属性信息(用户名称,用户ID,基本组ID等等)
/etc/group: 组及其属性信息
/etc/shadow: 用户密码及其属性信息
/etc/gshadow: 组密码及其属性信息
配置文件/etc/passwd:
root: x:0:0:root:/root:/bin/bash
username:password:UID:GID:GECOS(用户注释信息):home directory:shell
配置文件/etc/group
root: x:0:
gname:password:GID:user_list
配置文件/etc/shadow
root:xxxx::0:99999:7:::
登录名:加密密码:最近一次更改密码的日期:密码的最小使用期限:密码的最大使用期限:密码的告警时间段:密码的禁用日期:账户过期日期:保留字段
二、用户管理命令
- useradd命令:创建用户
[root@test ~]# useradd --help
Usage: useradd [options] LOGIN
常用选项:
-u: 指定uid
-g:指定gid
-c:指定用户注释信息
-d:指定家目录
-s:指定shell类型(默认是/bin/bash)
可用的shell类型
[root@test ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
-G:指定附加组
-r:指定为系统用户
- 默认设置:
[root@test ~]# useradd zhangsan
zhangsan:x:1000:1000::/home/zhangsan:/bin/bash
- 创建用户的默认配置:
[root@test ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
- 自定义创建用户
[root@test ~]# useradd -u 2000 -c "this is lisi" -d /home/ll lisi
lisi:x:2000:2000:this is lisi:/home/ll:/bin/bash
userdel命令:删除用户
常用选项:
-r:remove home directory and mail spool 删除用户家目录和邮件信息
usermod命令:编辑用户信息
常用选项:
- aG:将用户添加到附加组中
- dm:移动用户新登录目录(默认情况会登录到自己的家目录下)
- L: 锁定用户
- U: 解锁用户
passwd命令:编辑用户密码
常用选项:
-n mindays: 指定最短使用期限
-x maxdays:指定最大使用期限
-w warndays:提前多少天开始警告
-i Inactivedays: 非活动期限
–stdin: 从标准输入接收用户密码
[root@test ~]# echo "111111" | passwd --stdin zhangsan
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully.
案例1
创建用户gentoo、附加组为distro和linux,默认shell为/bin/csh,注释信息为“Gentoo Distribution”
[root@test ~]# useradd -G bin,root -c "Gentoo Distribution" -s /bin/csh gentoo
三、组管理命令
groupadd命令:创建组
常用选项:
-g: 指定gid
-r: 指定为系统组
-p, --password PASSWORD use this encrypted password for the new group
[root@test ~]# groupadd -g 2000 -r linux
groupdel命令:删除组
常用选项:
-r, --remove remove home directory and mail spool
-f, --force force some actions that would fail otherwise
groupmod命令:编辑组信息
常用选项:
-g, --gid GID change the group ID to GID 指定GID
-n, --new-name NEW_GROUP change the name to NEW_GROUP 修改组名称
-p, --password PASSWORD change the password to this (encrypted) 修改组密码
gpasswd命令:编辑组密码
常用选项:
-a, --add USER add USER to GROUP 添加用户到某个组中
-d, --delete USER remove USER from GROUP 移除用户从某个组
-r, --delete-password remove the GROUP’s password 删除组密码
-M, --members USER,… set the list of members of GROUP 设置组列表
-A, --administrators ADMIN,… 设置组中管理员
set the list of administrators for GROUP
四、用户/组相关命令
id命令
[root@test ~]# id --help
Usage: id [OPTION]... [USER]
常用选项:
-Z, --context print only the security context of the current user 只输出当前用户的安全上下文
-g, --group print only the effective group ID 只输出主组ID
-G, --groups print all group IDs 输出所有组ID
-n, --name print a name instead of a number, for -ugG 输出是名字代替ID号
-r, --real print the real ID instead of the effective ID, with -ugG 输出真实ID号
-u, --user print only the effective user ID
[root@test ~]# id zhangsan
uid=1000(zhangsan) gid=1000(zhangsan) groups=1000(zhangsan),0(root)
su命令:切换用户或提权
常用的切换方式:
su username : 切换用户,不读取用户配置信息
su - username: 切换用户,读取用户配置信息(完全切换)
su [-] username -c “Command”: 切换用户执行命令
[zhangsan@test ~]$ su root -c "cat /etc/shadow"