Linux user management command

1. Add a new user and password

useradd: add user name
passwd: add user password

[root@localhost ~]# useradd xieyitang
[root@localhost ~]# passwd root
更改用户 root 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
抱歉,密码不匹配。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
抱歉,密码不匹配。
新的 密码:

[1]+  已停止               passwd root
[root@localhost ~]# passwd root
更改用户 root 的密码 。
新的 密码:

[2]+  已停止               passwd root
[root@localhost ~]# passwd xieyitang
更改用户 xieyitang 的密码 。
新的 密码:
无效的密码: 密码包含用户名在某些地方
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# 
  • Determine whether the user exists
[root@localhost ~]# id xieyitang
uid=1000(xieyitang) gid=1000(xieyitang)=1000(xieyitang)

Two switch users

Get the user's execution permission, but not the environment variable

[root@localhost ~]# su xieyitang
[xieyitang@localhost root]$ 

Get environment variables and execute permissions

[xieyitang@localhost root]$ su - xieyitang
密码:
上一次登录:三 53 11:47:39 CST 2023pts/0 上
最后一次失败的登录:三 53 11:49:55 CST 2023pts/0 上
最有一次成功登录后有 1 次失败的登录尝试。
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/home/xieyitang"
declare -x HOSTNAME="localhost.localdomain"

Three userdel delete user

add two users

[root@localhost ~]# useradd jack
[root@localhost ~]# cd home
-bash: cd: home: 没有那个文件或目录
[root@localhost ~]# cd /home
[root@localhost home]# ll
总用量 0
drwx------. 2 jack      jack      62 53 11:54 jack
drwx------. 2 xieyitang xieyitang 62 53 11:23 xieyitang
[root@localhost home]# useradd susan
[root@localhost home]# ll
总用量 0
drwx------. 2 jack      jack      62 53 11:54 jack
drwx------. 2 susan     susan     62 53 11:54 susan
drwx------. 2 xieyitang xieyitang 62 53 11:23 xieyitang

Delete the jack user, and find that the user still exists
userdel -r user name, delete the information in the home directory together

[root@localhost home]# ll
总用量 0
drwx------. 2 jack      jack      62 53 11:54 jack
drwx------. 2 susan     susan     62 53 11:54 susan
drwx------. 2 xieyitang xieyitang 62 53 11:23 xieyitang
[root@localhost home]# userdel -r susan
[root@localhost home]# ll
总用量 0
drwx------. 2 jack      jack      62 53 11:54 jack
drwx------. 2 xieyitang xieyitang 62 53 11:23 xieyitang
[root@localhost home]# userdel jack
[root@localhost home]# ll
总用量 0
drwx------. 2      1001      1001 62 53 11:54 jack
drwx------. 2 xieyitang xieyitang 62 53 11:23 xieyitang

Four view user login information

whoami: Display your own user name
who am i Display the logged-in user name
who: See which devices are currently logged into this device

[root@localhost home]# whoami
root
[root@localhost home]# who am i
root     pts/0        2023-05-03 11:37 (192.168.80.1)
[root@localhost home]# who
root     pts/0        2023-05-03 11:37 (192.168.80.1)

5. Let ordinary users become administrators

Reduce the login and management of root users and improve system security

1. Modify the environment configuration file

insert image description here

[root@localhost home]# vim /etc/sudoers
[root@localhost home]# su - xieyitang
上一次登录:三 53 11:50:05 CST 2023pts/0 上

2. Set user and password

[xieyitang@localhost ~]$ sudo useradd lisi

我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:

    #1) 尊重别人的隐私。
    #2) 输入前要先考虑(后果和风险)。
    #3) 权力越大,责任越大。

[sudo] xieyitang 的密码:
对不起,请重试。
[sudo] xieyitang 的密码:
[xieyitang@localhost ~]$ 

Set the password to lisi

[xieyitang@localhost ~]$ sudo passwd lisi
更改用户 lisi 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[xieyitang@localhost ~]$ 

6. Check which users are created

[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
xieyitang:x:1000:1000::/home/xieyitang:/bin/bash
lisi:x:1001:1001::/home/lisi:/bin/bash

The file is a plain text file, each line follows the same format:
name:password:uid:gid:comment:home:shell

Name: User login name
Password: User password. Passwords in this field are encrypted. When a user logs in to the system, the system uses the same algorithm for the entered password and compares it with the content in this field. If this field is blank, the user does not require a password to log in.
Uid: Specifies the UID of the user. After the user logs into the system, the system identifies the user by this value instead of the user name.
gid: GID. Use this value if the system is to give the same rights to the same group of people.

comment: used to save the user's real name and personal details.
home: Specifies the absolute path of the user's home directory.
shell: If the user logs in successfully, the absolute path of the command to be executed is placed in this area. It can be any command.

insert image description here

Guess you like

Origin blog.csdn.net/CNMBZY/article/details/130472283
Recommended