User management
User part
Add new user
Basic command line
1
useradd username
此方法虽然添加了用户,但是有可能没有默认创建文件夹,如果没有创建
1 useradd -m username可以解决这个问题
注意如果刚刚执行完上面的语句,需要先删除刚刚的user才能再次创建他,没有办法有两个同名的user
1 adduser username这个也可以,创建完user后也会生成文件夹,更加推荐
example :
Example
- add a user
milan
, default user directory is /home/milan
- add a user
detail
when create user successful, auto create directory for user at same time
if we use
1
useradd -d The specified directory username # useradd -d 指定目录 新加用户名
also create a user but this user will use the specified directory instead of generating a new directory
此方法同上
1
useradd -m -d The specified directory username# useradd -m -d 指定目录 新加用户名
可以解决这个问题
给创建的用户指定了家目录
Set password for new user
1 | passwd username |
This operation also need under root
Delete user
1 | userdel username |
only delete this user
1 | userdel -r username |
Not only delete the user, but also delete the directory
Querying User Information
1 | id username |
examples :
Switch users
1 | su - username |
In Linux, if the current user has insufficient rights, you can switch to a user with higher right
A password is not required to change from a high-privilege user to a low-privilege user
Otherwise you need a password
To return to the original user, use the exit/logout command
在操作linux中,如果当前权限不够,可以切换至更高权限的用户
从高权限用户到低权限用户不需要密码
反之需要密码
如果想回到原来用户使用exit/logout指令
examples :
Who am i
1 | who am i |
1 | whoami |
it will show the initial login user’s information
examples :
i am at root currently, but it will show the init login user – liangjunyi
User Group part
Unified management of users with common/permission in the system
系统对有 共性/权限 的用户进行统一管理
Add new group
1 | groupadd groupname |
Delete a group
1 | groupdel groupname |
Add group when add user
1 | useradd -g groupname username |
Change user’s group
1 | usermod -g groupname username |
File for user and group
1 | /etc/passwd 文件 |
user的配置文件,记录用户的各种信息
每行的含义:
- 用户名:口令:用户标识号:组标识号:注释性描述:主目录:登陆shell
1 | /etc/shadow 文件 |
口令的配置文件
每行的含义:
- 登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间:失效时间:标志
- 加密口令:设置了密码的用户会有很长一段的密码,没设置的是叹号
1 | /etc/group 文件 |
group的配置文件,记录linux包含组的信息
每行含义:
- 组名:口令:组标识号:组内用户列表
Find all users, groups
find all users
1 | cat /etc/passwd |
or
1 | cut -d : -f 1 /etc/passwd |
or
1 | cat /etc/passwd |awk -F \: '{print $1}' |
or
1 | cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F":" '{print $1"|"$3"|"$4}'|more |
find all groups
1 | cat /etc/group |
or
1 | getent group |