centos 修改密码_CentOS7 - 加强密码限制

实施密码限制

弱密码可能是任何系统中最薄弱的安全点之一。简单密码容易受到暴力攻击, 密码如果受到威胁, 将为恶意活动提供广阔的机会窗口。因此, 确保用户选择足够复杂的密码并定期更改密码非常重要。此配方向您展示了如何通过对用户密码实施各种限制来增强系统的安全性。您将了解如何指定密码的最低复杂性要求、必须更改密码的时间以及如何在多次登录尝试失败后锁定帐户。

准备

需要一个 CentOS 系统和管理权限, 可以通过使用根帐户登录来提供, 也可以通过使用 sudo 来提供。

它是如何做到的

请按照以下步骤强制实施密码限制, 以提高 CentOS 系统的安全性:

  • 控制密码老化的参数见/etc/login.defs;使用所选的文本编辑器打开文件:
vi /etc/login.defs
  • 找到密码过期控制的地方,更新如下内容PASS_MAX_DAYS, PASS_MIN_DAYS, PASS_MIN_LEN, and PASS_WARN_AGE:
PASS_MAX_DAYS 90PASS_MIN_DAYS 0PASS_MIN_LEN 8PASS_WARN_AGE 15
  • 保存并退出。
  • 修改后的login.defs内容会应用到新建用户的相关策略中。已经建立的用户需要使用如下的命令进行修改:
chage --maxdays 90 --mindays 0 --warndays 15 tboronczyk
  • 控制密码可接受的复杂性的参数在/etc/security/pwquality.conf中,打开文件编辑:
vi /etc/security/pwquality.conf
  • 取消注释最小值, 以指定所需的最小密码复杂度加上1。例如, 由所有小写字符组成的8个字符的密码需要9的小密码:
minlen = 9
  • 您可以取消对其他值的注释, 并根据需要设置它们。每个值前面都有一个简短的描述性注释, 说明它的作用。若要要求来自特定类 (大写、小写、数字和其他特殊) 的最小字符数, 请将该值指定为负数。例如, 如果密码需要至少一个数字和一个大写字符, 则 dcredit 和 ucredit 都将设置为-1:
279546281473fde564768ee6c025314b.png

配置系统密码复杂性要求的选项可在 pwqualiity. conf 中。

  • 保存并退出。
  • 接下来, 我们将更新 PAM 的密码验证和系统-auth 模块配置, 以便在多次登录尝试失败后锁定帐户。打开文件/etccp. pam. d/密码-auth:
vi /etc/pam.d/password-auth
  • 更新文件开头的 "验证行" 组, 如下所示。第二行和第四行已添加, 并将 pam_faillock包括到身份验证堆栈:
auth required pam_env.soauth required pam_faillock.so preauth silent audit deny=3  unlock_time=600auth sufficient pam_unix.so nullok try_first_passauth [default=die] pam_faillock.so authfail audit deny=3  unlock_time=600auth requisite pam_succeed_if.so uid >= 1000 quiet_successauth required pam_deny.so
  • 更新账号的组的地方。添加了第二行的pam_faillock到账户栈中:
account required pam_unix.soaccount required pam_faillock.soaccount sufficient pam_localuser.comaccount sufficient pam_succeed_if.so uid < 1000 quietaccount required pam_permit.so

注意

  • 修改password-auth或者system-auth的时候一定要小心。模块在堆栈中的列出顺序非常重要。

它是如何工作的

正确配置本地帐户的身份验证要求有点破裂的体验. 首先, 有传统的 Unix 密码文件 (/etc/passwd and /etc/groups) and the shadow-utils package, which adds shadowing support (/etc/shadow). 这些共同构成了本地帐户凭据的核心数据库。此外, 与大多数其他现代 Linux 系统类似, CentOS 使用 PAM, 这是一个可插入身份验证模块的集合。 默认情况下, PAM 堆栈配置为查找阴影文件中的帐户信息, 但它还提供了支持 pam 的程序可以利用的其他功能, 如密码强度检查。 作为管理员, 您负责配置这些服务, 以便它们能够正常运行, 并在组织设置的可接受的安全准则范围内运行。

In this recipe, we first updated the password aging related controls found in /etc/logins.def:

在本文中,我们首先通过更新/etc/login.defs文件中的密码过期策略:

PASS_MAX_DAYS 90PASS_MIN_DAYS 0PASS_MIN_LEN 8PASS_WARN_AGE 15

PASS_MAX_DAYS定义了密码多长时间需要修改.设置为90,用户必须在90天以内进行修改. PASS_MIN_DAYS定义了用户在多少天以内不得修改密码。因为设置成了0,用户想啥时候重新设置就啥时候重新设置。PASS_WARN_AGE 定义了用户剩余多少天过期密码的时候通知用户。

注意

PASS_MIN_LEN 应该设置最小密码长度, 但你会发现 PAM 的密码复杂性要求取代了这一点, 使设置几乎毫无价值。

在密码和阴影文件中创建条目时, 用户添加等实用程序使用这些设置作为默认值。它们不会追溯应用于现有用户, 因此我们需要使用 chage 来更新他们的帐户:

chage --maxdays 90 --mindays 0 --warndays 15 tboronczyk

chage 可以设置用户密码的最小和最长期限以及挂起过期的通知窗口, 但请注意没有最低长度要求。

我们还可以使用 chage 使用户的密码立即过期, 以便他们在下次登录时必须指定新密码。为此, 我们提供--最近的参数, 其值为 0:

chage --lastdays 0 tboronczyk

小提示

如果您有几个以上的帐户, 您可能希望使用一些基本的 shell 脚本自动使用 chage。下面是一系列命令, 这些命令以自动方式更新所有现有用户帐户:

getent shadow | awk -F : 'substr($2, 0, 1) == "$" { print $1 }' | xargs -n 1 chage --maxdays 90 --mindays 0 --warndays 15

这是通过检索阴影文件的内容和使用 awk 使用: 作为字段分隔符拆分每个记录来实现的。awk 查看第二个字段中的值 (加密密码), 以查看它是否以 $ 开头, 指示该帐户具有密码, 以便在没有密码的情况下筛选出禁用的帐户和系统帐户。然后将每个匹配记录中的用户名通过管道传送到 xargs, 然后将名称一次提供一个用于 chage。

当 PAM 模块 pam_pwquality检查密码的复杂性时, 我们在模块的配置文件 "/etc/security/pam_pwquality" 中指定了密码复杂性要求。它使用信用系统来衡量密码的质量, 每个字符都将一分计入密码的总分。这个分数必须达到或超过我们给minlen的价值。

http://wpollock.com/AUnix2/PAM-Help.htm有一个更好的关于pam_pwquality计算密码复杂度的解释。算法如下:

  • Add one for each character in the password regardless of the type of the character
  • Add one to that for each lowercase letter used, up to a maximum of lcredit
  • Add one to that for each uppercase letter used, up to a maximum of ucredit
  • Add one to that for each digit used, up to a maximum of dcredit
  • Add one to that for each symbol used, up to a maximum of ocredit

该页面还提供了一些不同密码的复杂性计算, 值得一读。

然后, 我们更新了密码-身份验证和系统-auth 文件, 以锁定用户的帐户后三次不成功的登录尝试。需要配置不同的身份验证堆栈, 因为不同的登录方法将调用不同的身份验证堆栈 (即通过 SSH 登录而不是本地登录):

auth required pam_env.soauth required pam_faillock.so preauth silent audit deny=3  unlock_time=600auth sufficient pam_unix.so nullok try_first_passauth [default=die] pam_faillock.so authfail audit deny=3 unlock_time=600auth requisite pam_succeed_if.so uid >= 1000 quiet_successauth required pam_deny.soaccount required pam_unix.soaccount required pam_faillock.soaccount sufficient pam_localuser.comaccount sufficient pam_succeed_if.so uid < 1000 quietaccount required pam_permit.so

The pam_faillock 模块被添加到身份验证堆栈中的多个位置. The first appearance in the auth block 执行预检查 (预检查), 以查看帐户是否已被锁定。 The second appearance tallies the failed attempt (authfail). The argument specified by deny is the number of failed attempts permitted before locking the account. unlock_time specifies how much time the module should wait (in seconds) before unlocking the account so that another login attempt can be made. As the example specifies 600 seconds, a user will have to wait 10 minutes for the lockout to expire. The module's appearance in the account block denies authentication to the locked account.

The faillock command is used to view the number of failed login attempts and to unlock an account. To see the failed attempts, invoke the command using the --user argument to specify the account's username:

faillock --user tboronczyk

To manually unlock the account before unlock_time has elapsed, invoke the command with the --reset argument:

faillock --user tboronczyk --reset

See also

Refer to the following resources for more information on how user accounts are authenticated and how to enforce password restrictions:

  • The chage man page (man 1 chage)
  • The shadow file man page (man 5 shadow)
  • The pam_faillock man page (man 8 pam_faillock)
  • Linux Documentation Project: Putting the Shadow suite to use (http://tldp.org/HOWTO/Shadow-Password-HOWTO-7.html)
  • The Linux-PAM System Administrator's Guide (http://www.linux-pam.org/Linux-PAM-html/Linux-PAM_SAG.html)
  • RHEL Security Guide: Password Security (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html-single/Security_Guide/index.html#sec-Password_Security)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值