700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > linux设置用户密码 查看linux用户信息 root用户管理普通用户(passwd命令使用)

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

时间:2022-09-04 05:19:08

相关推荐

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 -hsudo - execute a command as another userusage: sudo -h | -K | -k | -Vusage: 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=numclose all file descriptors >= num-E, --preserve-env preserve user environment when running command--preserve-env=list preserve specific environment variables-e, --editedit files instead of running a command-g, --group=group run command as the specified group name or ID-H, --set-homeset HOME variable to target user's home dir-h, --helpdisplay help message and exit-h, --host=hostrun 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, --listlist 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=rolecreate 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=typecreate 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=userrun command (or edit file) as specified user name or ID-V, --version display version information and exit-v, --validateupdate 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_0716_bak

2、重建

sudo pwconv

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

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

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

sudo passwd shl

成功,成功,成功!!!

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

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

(base) shl@zhihui-mint:~$ passwd -hUsage: 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, --helpdisplay this help message and exit-k, --keep-tokens change password only if expired-i, --inactive INACTIVE set password inactive after expirationto INACTIVE-l, --locklock the password of the named account-n, --mindays MIN_DAYS set minimum number of days before passwordchange 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_DAYSset expiration warning days to WARN_DAYS-x, --maxdays MAX_DAYS set maximum number of days before passwordchange to MAX_DAYS(base) shl@zhihui-mint:~$

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

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

k: 保留即将过期的用户在期满后能仍能使用;d: 删除用户密码,仅能以root权限操作,删除后,用户再次登录时不需要输入密码;eg:sudo passwd -d shll:锁住用户无权更改其密码,仅能通过root权限操作;eg:sudo passwd -l shlu:解除锁定;eg:sudo passwd -u shlf: 强制操作;仅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/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/usr/sbin/nologinman:x:6:12:man:/var/cache/man:/usr/sbin/nologinlp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/sbin/nologinnews:x:9:9:news:/var/spool/news:/usr/sbin/nologinuucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologinproxy:x:13:13:proxy:/bin:/usr/sbin/nologinwww-data:x:33:33:www-data:/var/www:/usr/sbin/nologinbackup:x:34:34:backup:/var/backups:/usr/sbin/nologinlist:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologinirc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologingnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologinnobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologinsystemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologinsystemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologinsyslog:x:102:106::/home/syslog:/usr/sbin/nologinmessagebus:x:103:107::/nonexistent:/usr/sbin/nologin_apt:x:104:65534::/nonexistent:/usr/sbin/nologinuuidd:x:105:111::/run/uuidd:/usr/sbin/nologincups-pk-helper:x:106:112:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologinkernoops:x:107:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologinrtkit:x:108:113:RealtimeKit,,,:/proc:/usr/sbin/nologinavahi-autoipd:x:109:114:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologinusbmux:x:110:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologinsystemd-coredump:x:111:117:systemd core dump processing,,,:/run/systemd:/usr/sbin/nologinlightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/falsednsmasq:x:113:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologinsaned:x:114:121::/var/lib/saned:/usr/sbin/nologinnm-openvpn:x:115:122:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologinavahi:x:116:123:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologincolord:x:117:124:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologinspeech-dispatcher:x:118:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/falsepulse:x:119:125:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologinhplip:x:120:7:HPLIP system user,,,:/var/run/hplip:/bin/falsegeoclue:x:121:127::/var/lib/geoclue:/usr/sbin/nologinshl:x:1000:1000:shl,,,:/home/shl:/bin/bashsshd:x:123:65534::/run/sshd:/usr/sbin/nologingdm:x:122:129:Gnome Display Manager:/var/lib/gdm3:/bin/falsefestival: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 shlshl: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 类型。

参考:/hyfstyle/article/details/90904992 # 修改用户密码

参考:/list-all-users.html # 查看用户信息和密码的文件

参考:/yeluowuti/p/12187095.html # linux下用户相关的命令

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。