linux设置用户密码、查看linux用户信息、root用户管理普通用户(passwd命令使用)

1 切换root用户和给root用户设置密码

1.1 切换到root用户

1、使用su 命令 或 su root命令切换到root用户的时候报错:su: Authentication failure

在这里插入图片描述

之所以会报上面的错误,是因为root用户没有设置密码

2、如果仅仅想切换到root用户不设置密码,可以用如下方式:

  • sudo su:只切换到root用户
  • sudo -i:只切换到root用户

在这里插入图片描述

sudo的更多命令参数

(base) shl@zhihui-mint:~$ sudo -h
sudo - execute a command as another user

usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ...

Options:
  -A, --askpass                 use a helper program for password prompting
  -b, --background              run command in the background
  -C, --close-from=num          close all file descriptors >= num
  -E, --preserve-env            preserve user environment when running command
      --preserve-env=list       preserve specific environment variables
  -e, --edit                    edit files instead of running a command
  -g, --group=group             run command as the specified group name or ID
  -H, --set-home                set HOME variable to target user's home dir
  -h, --help                    display help message and exit
  -h, --host=host               run command on host (if supported by plugin)
  -i, --login                   run login shell as the target user; a command may also be specified
  -K, --remove-timestamp        remove timestamp file completely
  -k, --reset-timestamp         invalidate timestamp file
  -l, --list                    list user's privileges or check a specific command; use twice for longer format
  -n, --non-interactive         non-interactive mode, no prompts are used
  -P, --preserve-groups         preserve group vector instead of setting to target's
  -p, --prompt=prompt           use the specified password prompt
  -r, --role=role               create SELinux security context with specified role
  -S, --stdin                   read password from standard input
  -s, --shell                   run shell as the target user; a command may also be specified
  -t, --type=type               create SELinux security context with specified type
  -T, --command-timeout=timeout terminate command after the specified time limit
  -U, --other-user=user         in list mode, display privileges for user
  -u, --user=user               run command (or edit file) as specified user name or ID
  -V, --version                 display version information and exit
  -v, --validate                update user's timestamp without running a command
  --                            stop processing command line arguments
(base) shl@zhihui-mint:~$ 

1.2 给root用户设置密码

3、如果想给root用户设置密码,设置密码后再使用su命令,然后输入刚刚设置的root用户密码,即可切换到root用户了

root用户设置密码(参考):

sudo passwd root

在这里插入图片描述

2 给普通用户重新设置密码

2.1 给普通用户重新设置密码:pawwd username

1、给普通用户设置密码,我自己常常用的用户是shl,现在我想修改用户shl的密码,命令为:

passwd username

2.2 普通用户重新设置密码报错:passwd: Authentication token manipulation error

但是我在修改密码的时候报错:passwd: Authentication token manipulation error

在这里插入图片描述

尝试解决passwd: Authentication token manipulation error错误(参考1参考2参考3):

网上给出的解决方案大概有两种:

  • 查看存放用户和密码的文件是否存在-i属性,如果有取消该属性
  • 系统磁盘或者inode 100%满了,导致修改用户报错,通过df -h及df -i查看(参考),也没有给出解决方法

2.2.1 解决方法一:取消-i属性

1、查看是否有-i属性

sudo lsattr /etc/*shadow /etc/passwd /etc/group

如果有-i属性,显示如下图所示(参考),我的是没有!

在这里插入图片描述

2、取消-i属性

sudo chattr -i /etc/*shadow /etc/passwd /etc/group

3、再次设置用户密码

passwd shl

结果还是报错:passwd: Authentication token manipulation error,该方法对我无效
在这里插入图片描述

2.2.2 解决方法二:重新生成shadow文件

1、备份shadow文件

sudo mv /etc/shadow /etc/shadow_20210716_bak

2、重建

sudo pwconv

在这里插入图片描述

这种方法对于我来说,同样是没有效果!

2.2.3 解决方法三:直接用root用户权限去修改普通用户的密码

直接用root用户权限去修改普通用户的密码

sudo passwd shl

在这里插入图片描述

成功,成功,成功!!!

3 root用户管理普通用户(passwd命令使用)

1、我们来先看一下passwd命令有哪些参数

(base) shl@zhihui-mint:~$ passwd -h
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  -d, --delete                  delete the password for the named account
  -e, --expire                  force expire the password for the named account
  -h, --help                    display this help message and exit
  -k, --keep-tokens             change password only if expired
  -i, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -l, --lock                    lock the password of the named account
  -n, --mindays MIN_DAYS        set minimum number of days before password
                                change to MIN_DAYS
  -q, --quiet                   quiet mode
  -r, --repository REPOSITORY   change password in REPOSITORY repository
  -R, --root CHROOT_DIR         directory to chroot into
  -S, --status                  report password status on the named account
  -u, --unlock                  unlock the password of the named account
  -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS
  -x, --maxdays MAX_DAYS        set maximum number of days before password
                                change to MAX_DAYS

(base) shl@zhihui-mint:~$ 

下面对上面比较常用的一些参数进行介绍:

2、passwd一些参数介绍,其中shl是一个普通用户名(参考

  • k: 保留即将过期的用户在期满后能仍能使用;
  • d: 删除用户密码,仅能以root权限操作,删除后,用户再次登录时不需要输入密码;eg:sudo passwd -d shl
  • l:锁住用户无权更改其密码,仅能通过root权限操作;eg:sudo passwd -l shl
  • u:解除锁定;eg:sudo passwd -u shl
  • f: 强制操作;仅root权限才能操作;
  • x:两次密码修正的最大天数,后面接数字;仅能root权限操作;
  • n: 两次密码修改的最小天数,后面接数字,仅能root权限操作;
  • w: 在距多少天提醒用户修改密码;仅能root权限操作;
  • i: 在密码过期后多少天,用户被禁掉,仅能以root操作;
  • S:查询询用户的密码状态,仅能root用户操作;

4 查看linux系统中用户的信息

4.1 查看linux系统中用户信息的命令

通过使用 /etc/passwd 文件getent 命令compgen 命令这三种方法查看系统中用户的信息。

1、使用sudo cat /etc/passwd命令查看

在这里插入图片描述

2、Linuxgetent命令用来查看系统的数据库中的相关记录(参考

  • getent passwd:查看所有用户的信息
  • getent passwd username:查看指定用户的信息

在这里插入图片描述

在这里插入图片描述

3、compgen一个很棒的命令列出所有Linux命令,compgen是bash内置命令,它将显示所有可用的命令,别名和函数。(参考

  • compgen -c:列出所有的linux可用命令
  • compgen -u:列出linux系统中所有用户的信息

在这里插入图片描述

4.2 linux系统用户文件中存储信息说明

4.2.1 /etc/passwd 文件说明

1、先查看一下该文件中的内容(参考):

(base) shl@zhihui-mint:~$ sudo cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologin
syslog:x:102:106::/home/syslog:/usr/sbin/nologin
messagebus:x:103:107::/nonexistent:/usr/sbin/nologin
_apt:x:104:65534::/nonexistent:/usr/sbin/nologin
uuidd:x:105:111::/run/uuidd:/usr/sbin/nologin
cups-pk-helper:x:106:112:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin
kernoops:x:107:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin
rtkit:x:108:113:RealtimeKit,,,:/proc:/usr/sbin/nologin
avahi-autoipd:x:109:114:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin
usbmux:x:110:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin
systemd-coredump:x:111:117:systemd core dump processing,,,:/run/systemd:/usr/sbin/nologin
lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false
dnsmasq:x:113:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin
saned:x:114:121::/var/lib/saned:/usr/sbin/nologin
nm-openvpn:x:115:122:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin
avahi:x:116:123:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin
colord:x:117:124:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin
speech-dispatcher:x:118:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false
pulse:x:119:125:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin
hplip:x:120:7:HPLIP system user,,,:/var/run/hplip:/bin/false
geoclue:x:121:127::/var/lib/geoclue:/usr/sbin/nologin
shl:x:1000:1000:shl,,,:/home/shl:/bin/bash
sshd:x:123:65534::/run/sshd:/usr/sbin/nologin
gdm:x:122:129:Gnome Display Manager:/var/lib/gdm3:/bin/false
festival:x:124:29::/nonexistent:/usr/sbin/nologin
(base) shl@zhihui-mint:~$ 

2、/etc/passwd是一个文本文件,其中包含了登录 Linux 系统所必需的每个用户的信息。它保存用户的有用信息:如用户名、密码、用户 ID、群组 ID、用户 ID 信息、用户的家目录和 Shell 。

/etc/passwd 文件将每个用户的详细信息写为一行,其中包含七个字段,每个字段之间用冒号:分隔

3、下面以shl这个用户的信息为例说明,具体七个字段代表的含义:

(base) shl@zhihui-mint:~$ getent passwd shl
shl:x:1000:1000:shl,,,:/home/shl:/bin/bash
(base) shl@zhihui-mint:~$ 
  • 1)用户名 ——shl: 已创建用户的用户名,字符长度 1 个到 12 个字符。

  • 2)密码——x:代表加密密码保存在 /etc/shadow 文件中。

  • 3)用户ID——1000:代表用户的 ID 号,每个用户都要有一个唯一的 ID 。UID 号为0 的是为root 用户保留的,UID 号1 到 99是为系统用户保留的,UID 号100-999是为系统账户和群组保留的。

  • 4)群组ID ——1000:代表群组的 ID 号,每个群组都要有一个唯一的 GID ,保存在 /etc/group文件中。

  • 5)用户信息——shl,,,:代表描述字段,可以用来描述用户的信息(LCTT 译注:此处原文疑有误)。

  • 6)用户家目录——/home/shl:代表用户的家目录。

  • 7)Shell——/bin/bash:代表用户使用的 shell 类型。

参考:https://blog.csdn.net/hyfstyle/article/details/90904992 # 修改用户密码
参考:https://www.linuxprobe.com/list-all-users.html # 查看用户信息和密码的文件
参考:https://www.cnblogs.com/yeluowuti/p/12187095.html # linux下用户相关的命令

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值