介绍
密码和身份验证是每个用户在 Linux 环境中工作时必须处理的概念。这些主题涵盖许多不同的配置文件和工具。
在本指南中,我们将探索一些基本文件,例如“/etc/passwd”和“/etc/shadow”,以及用于配置身份验证的工具,例如名称恰当的“passwd”命令和“adduser”。
我们将使用 Ubuntu 12.04 VPS 来讨论这些主题,但任何现代 Linux 发行版都应该以类似的方式运行。
什么是“/etc/passwd”文件?
我们要查看的第一个文件称为“/etc/密码" 文件,实际上并不存储密码。
曾经,该文件存储了系统上每个用户的哈希密码。但是,出于安全原因,此责任已转移到单独的文件中。
让我们看看什么is在“/etc/passwd”文件中:
less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .
首先要注意的是,非特权用户可以访问该文件。
系统上的每个人都具有该文件的读取权限。这就是密码信息被移出该文件的原因。
我们来看看文件的格式。
如何读取“/etc/passwd”文件
文件中的每一行包含系统上单个用户的登录信息。其中一些用户可能是为守护程序和后台服务使用而创建的。
看一下一行,看看它包含哪些信息:
root:x:0:0:root:/root:/bin/bash
信息字段由冒号 (:) 字符分隔。典型的 Linux“/etc/passwd”文件中每行有七个字段:
-
root: 帐户用户名。
<li><strong>x</strong>: Placeholder for password information. The password is obtained from the "/etc/shadow" file.</li>
<li><strong>0</strong>: User ID. Each user has a unique ID that identifies them on the system. The root user is always referenced by user ID 0.</li>
<li><strong>0</strong>: Group ID. Each group has a unique group ID. Each user has a "primary" group that is used as the group by default. Again, the root group's ID is always 0.</li>
<li><strong>root</strong>: Comment field. This field can be used to describe the user or user's function. This can be anything from contact information for the user, to descriptions of the service the account was made for.</li>
<li><strong>/root</strong>: Home directory. For regular users, this would usually be "/home/<span class="highlight">username</span>". For root, this is "/root".</li>
<li><strong>/bin/bash</strong>: User shell. This field contains the shell that will be spawned or the command that will be run when the user logs in.</li>
当您使用“adduser" and "useradd”,或者当您安装更多服务时,该文件将会增大。新的用户名信息将添加到该文件的底部。
在大多数情况下,您不必手动编辑此文件。有一些工具可以操作该文件并确保维护正确的语法。
什么是“/etc/shadow”文件?
实际的密码数据存储在名为“/etc/影子".
这实际上并不包含纯文本的密码。相反,它使用密钥派生函数来创建哈希。这就是它存储在文件中的内容。
密钥派生函数基本上是一种算法,当给定相同的输入时,该算法始终会创建特定的哈希值。对身份验证期间给出的密码运行相同的算法,并将该值与该文件中的值进行比较。
请注意,该文件与“/etc/passwd”文件不同,非特权用户无法读取。
root 用户具有读写权限,“shadow”组(包含身份验证所需的用户)具有读取权限。
如何读取“/etc/shadow”文件
输入以下命令打开“/etc/shadow”文件:
sudo less /etc/shadow
root:$6$mJD3Rsj4$xUa7jru6EEGTXnhwTfTT26/j8M5XiQvUl6UH32cfAWT/6W9iSI5IuIw5OOw4khwrsOHPyMwfCLyayfYiVdhAq0:15952:0:99999:7:::
daemon:*:15455:0:99999:7:::
bin:*:15455:0:99999:7:::
sys:*:15455:0:99999:7:::
sync:*:15455:0:99999:7:::
games:*:15455:0:99999:7:::
man:*:15455:0:99999:7:::
. . .
与“/etc/passwd”文件一样,每一行定义用户信息,每个字段由冒号 (:) 字符分隔。
Note:上述某些行第二个字段中的星号 (*) 值表示该帐户无法登录。这主要用于服务,是预期行为。
我们再看一下单行:
daemon:*:15455:0:99999:7:::
这些是“/etc/shadow”文件中定义的字段:
-
daemon: 帐户用户名。
<li><strong>*</strong>: Salt and hashed password. You can see what this looks like with the root entry above.
As noted above, the asterisk signifies that this account cannot be used to log in.</li>
<li><strong>15455</strong>: Last password change. This value is measured in days from the Unix "epoch", which is January 1, 1970.</li>
<li><strong>0</strong>: Days until password change permitted. 0 in this field means there are no restrictions.</li>
<li><strong>99999</strong>: Days until password change required. 99999 means that there is no limit to how long the current password is valid.</li>
<li><strong>7</strong>: Days of warning prior to expiration. If there is a password change requirement, this will warn the user to change their password this many days in advance.</li>
<li><strong>[blank]</strong>The last three fields are used to denote days before the account is made inactive, days since the Epoch when the account expires. The last field is unused.</li>
如何更改密码?
用户的密码可以通过发出“passwd”命令来修改。
默认情况下,此命令更改当前用户的密码,不需要特殊权限。
passwd
如果您想更改其他用户的密码,您将需要管理权限。可以使用以下语法:
sudo passwd username
系统将提示您输入“sudo”命令的密码,然后系统将提示您输入并确认您要使用的新密码。
如果您比较“/etc/shadow”文件中的哈希值,您会发现在发出 passwd 命令后它发生了变化。
如何创建新用户?
可以使用一些不同的命令创建用户。
最简单的方法可能是“adduser“命令,我们将在这里介绍该命令。在 Ubuntu 系统上,该命令链接到一个处理适当用户创建的 perl 脚本。
你可以这样称呼它:
adduser demo
Adding user `demo' ...
Adding new group `demo' (1000) ...
Adding new user `demo' (1000) with group `demo' ...
Creating home directory `/home/demo' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for demo
Enter the new value, or press ENTER for the default
Full Name []: test
Room Number []: room
Work Phone []: work phone
Home Phone []: home phone
Other []: other
Is the information correct? [Y/n]
您将被问到一系列问题,这些问题将有助于填写“/etc/passwd”文件和“/etc/shadow”文件中的信息。
我们可以通过输入以下内容来查看它添加到“/etc/passwd”文件中的条目:
tail -1 /etc/passwd
demo:x:1000:1000:test,room,work phoneme phone,other:/home/demo:/bin/bash
您可以看到,这极大地利用了评论字段。其他字段按预期填写。
我们可以运行类似的命令来查看对“/etc/shadow”文件所做的修改:
sudo tail -1 /etc/shadow
demo:$6$XvPCmWr4$HXWmaGSeU5SrKwK2ouAjc68SxbJgUQkQ.Fco9eTOex8232S7weBfr/CMHQkullQRLyJtCAD6rw5TVOXk39NAo/:15952:0:99999:7:::
结论
使用这些简单的工具,您可以更改系统上的登录信息。
进行任何更改后测试您的登录能力非常重要。保持身份验证文件的权限相同以维护功能和安全性也很重要。
贾斯汀·埃林伍德