User & User Group management


User management

User part

Add new user

  • Basic command line

    useradd username

此方法虽然添加了用户,但是有可能没有默认创建文件夹,如果没有创建

useradd -m username

可以解决这个问题

注意如果刚刚执行完上面的语句,需要先删除刚刚的user才能再次创建他,没有办法有两个同名的user

adduser username

这个也可以,创建完user后也会生成文件夹,更加推荐

example :

image-20220522213056132

  • Example

    • add a user milan, default user directory is /home/milan
  • detail

    • when create user successful, auto create directory for user at same time

    • if we use

      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

      此方法同上

      useradd -m -d The specified directory username# useradd -m -d 指定目录 新加用户名

      可以解决这个问题

      image-20220522213953461

      给创建的用户指定了家目录

Set password for new user

passwd username 

This operation also need under root

Delete user

userdel username

only delete this user

userdel -r username

Not only delete the user, but also delete the directory

Querying User Information

id username

examples :

image-20220523174054780

image-20220523174403469

Switch users

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 :

image-20220523175242905

Who am i

who am i
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

image-20220523180109271

image-20220523180147131

User Group part

Unified management of users with common/permission in the system

系统对有 共性/权限 的用户进行统一管理

Add new group

groupadd groupname

Delete a group

groupdel groupname

Add group when add user

useradd -g groupname username

Change user’s group

usermod -g groupname username

File for user and group

/etc/passwd 文件

user的配置文件,记录用户的各种信息

每行的含义:

  • 用户名:口令:用户标识号:组标识号:注释性描述:主目录:登陆shell
/etc/shadow 文件

口令的配置文件

每行的含义:

  • 登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间:失效时间:标志
    • 加密口令:设置了密码的用户会有很长一段的密码,没设置的是叹号
/etc/group 文件

group的配置文件,记录linux包含组的信息

每行含义:

  • 组名:口令:组标识号:组内用户列表

Find all users, groups

find all users

cat /etc/passwd

or

cut -d : -f 1 /etc/passwd

or

cat /etc/passwd |awk -F \: '{print $1}'

or

cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F":" '{print $1"|"$3"|"$4}'|more

find all groups

cat /etc/group

or

getent group

Author: Liang Junyi
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Liang Junyi !
  TOC