百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Linux-PAM认证

nanshan 2025-01-01 21:57 12 浏览 0 评论

在新主机更改用户密码的时候,经常会出现"passwd: Have exhausted maximum number of retries for service"的报错

[root@10-112-41-157 ~]# echo 'co4lgTdDD3iK7WYEJAyL0KT5pLXS0o3r' | passwd --stdin testpam
Changing password for user testpam.
passwd: Have exhausted maximum number of retries for service

实际之上,可以使用chpasswd命令更改解决,但非明文更改密码无法实现

1.PAM机制

在centos 6中用户的密码权限更变模块主要涉及到PAM(Pluggable Authentication Modules)认证机制,该机制由Sun公司提供,在Linux中,PAM是可动态配置的,本地系统管理员可以自由选择应用程序如何对用户进行身份验证。PAM应用在许多程序与服务上,比如登录程序(login、su)的PAM身份验证(口令认证、限制登录),passwd强制密码,用户进程实时管理,向用户分配系统资源等

点击我:>>centos官方文档说明

2.centos pam配置文件的构成

pam主要由动态库与配置文件构成。/etc/pam.d/目录中定义了各种程序和服务的PAM配置文件,如其中system-auth文件是PAM模块的重要配置文件,它主要负责用户登录系统的身份认证工作,不仅如此,其他的应用程序或服务可以通过include接口来调用它(该文件是system-auth-ac的软链接)。此外password-auth配置文件也是与身份验证相关的重要配置文件,比如用户的远程登录验证(SSH登录)就通过它调用。而模块文件则主要存放于/lib64/security/中

1.配置文件

[root@10-110-122-196 ~]# ll /etc/pam.d/
total 148
-rw-r--r--. 1 root root 272 Oct 18  2014 atd
-rw-r--r--. 1 root root 192 Oct 15  2014 chfn
-rw-r--r--. 1 root root 192 Oct 15  2014 chsh
-rw-r--r--  1 root root 232 Aug 18  2015 config-util
-rw-r--r--. 1 root root 293 Nov 23  2013 crond
-rw-r--r--. 1 root root  71 Nov 22  2013 cvs
-rw-r--r--. 1 root root 115 Nov 11  2010 eject
lrwxrwxrwx. 1 root root  19 Jan 14  2016 fingerprint-auth -> fingerprint-auth-ac
-rw-r--r--. 1 root root 659 Jan 14  2016 fingerprint-auth-ac
略

2.依赖的模块

[root@10-110-122-196 ~]# ll /lib64/security/
total 808
-rwxr-xr-x  1 root root 18552 Aug 18  2015 pam_access.so
-rwxr-xr-x. 1 root root  7504 Dec  8  2011 pam_cap.so
-rwxr-xr-x  1 root root 10272 Aug 18  2015 pam_chroot.so
-rwxr-xr-x. 1 root root  9216 Nov 11  2010 pam_ck_connector.so
-rwxr-xr-x  1 root root 27080 Aug 18  2015 pam_console.so
-rwxr-xr-x  1 root root 14432 Aug 18  2015 pam_cracklib.so
-rwxr-xr-x  1 root root 10168 Aug 18  2015 pam_debug.so
以下略

3.判断是否使用pam认证

判断一个程序是否使用了pam认证,可以查看其是否有使用pam模块即可

[root@10-110-122-196 pam.d]# ldd /usr/sbin/sshd | grep pam
	libpam.so.0 => /lib64/libpam.so.0 (0x00007f3460605000)
[root@10-110-122-196 pam.d]# ldd /usr/bin/passwd | grep pam
	libpam_misc.so.0 => /lib64/libpam_misc.so.0 (0x00007fe943edb000)
	libpam.so.0 => /lib64/libpam.so.0 (0x00007fe9434f6000)

以上说明sshd和passwd都有使用pam认证

3.pam配置文件的格式语法

<module interface>  <control flag>   <module name>   <module arguments>

详细查看一台主机中的system-auth-ac配置文件

[root@10-110-122-196 pam.d]# cat system-auth-ac 
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_fprintd.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        required      pam_deny.so

account     required      pam_unix.so
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     required      pam_permit.so

password    requisite     pam_cracklib.so  retry=3 difok=3 minlen=10 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 enforce_for_root
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so

1.模块接口:

2.控制标识:

3.模块参数

这里面主要讲pam_cracklib.so的相关参数,因为用户密码修改主要与此模块相关

注意,以上参数仅对非root用户生效,如果想对root用户生效,需要加入参数enforce_for_root,所以回到文章最初提出的那个异常,root用户因为也被强制生效了,所以无法更改用户密码,去掉该选项即可

4.passwd与chpasswd的区别

因为passwd不能更改密码的时候,可以直接使用chpasswd进行更改

[root@10-110-122-196 pam.d]# ldd /usr/sbin/chpasswd | grep pam
[root@10-110-122-196 pam.d]# ldd /usr/bin/passwd | grep pam
	libpam_misc.so.0 => /lib64/libpam_misc.so.0 (0x00007f64dd087000)
	libpam.so.0 => /lib64/libpam.so.0 (0x00007f64dc6a2000)

从以上可以看得,chpasswd并未使用pam认证机制,所以在passwd不能修改密码的时候,可以进行密码的修改。

从man中查看两者的区别

NAME
       passwd - update user’s authentication tokens

SYNOPSIS
       passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]

DESCRIPTION
       The passwd utility is used to update user’s authentication token(s).
       This task is achieved through calls to the Linux-PAM and Libuser API.  Essentially, it initializes itself as a "passwd" service with Linux-PAM and utilizes configured password modules to authenticate and then update a user’s password.
NAME
       chpasswd - update passwords in batch mode

SYNOPSIS
       chpasswd [options]

DESCRIPTION
       The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing users. Each line is of the format:
       user_name:password
       By default the supplied password must be in clear-text, and is encrypted by chpasswd. Also the password age will be updated, if present.
       The default encryption algorithm can be defined for the system with the ENCRYPT_METHOD variable of /etc/login.defs, and can be overwiten with the -e, -m, or -c options.
       chpasswd first update the password in memory, and then commit all the changes to disk if no errors occured for any users.
       This command is intended to be used in a large system environment where many accounts are created at a single time.

很显然可以查看得到passwd需要调用linux-PAM,然后调用密码模块,最后更新用户密码。而chpasswd是直接更新用户密码

而chpasswd使用的加密算法可以通过选项进行控制,默认是使用SHA-512算法

从/etc/shadow文件中可以看到用户加密码后的密码

[root@10-110-122-196 pam.d]# cat /etc/shadow
root:$6$hFXXRDb0$jQsZU9Lo8.HeYt4.kGr4QD8eUypaBr6EV403dN1LWBTXChNNXad0sHWl/55T7PiKUwdYoiAyeDt1.xqqf2Pt61:17477:0:99999:7:::

其中第二行中的6,代替加密算法为SHA-512,从系统库文件为GLIBC_2.7起,默认加密算法均为 SHA-512,查看系统库glibc版本方法:

[root@10-110-122-196 pam.d]# strings /lib64/libc.so.6 | grep GLIBC
GLIBC_2.2.5
GLIBC_2.2.6
GLIBC_2.3
GLIBC_2.3.2
GLIBC_2.3.3
GLIBC_2.3.4
GLIBC_2.4
GLIBC_2.5
GLIBC_2.6
GLIBC_2.7
GLIBC_2.8
GLIBC_2.9
GLIBC_2.10
GLIBC_2.11
GLIBC_2.12
GLIBC_PRIVATE

centos6系统以上即为2.12版本

相关推荐

Let’s Encrypt免费搭建HTTPS网站

HTTPS(全称:HyperTextTransferProtocoloverSecureSocketLayer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入...

使用Nginx配置TCP负载均衡(nginx tcp负载)

假设Kubernetes集群已经配置好,我们将基于CentOS为Nginx创建一个虚拟机。以下是实验种设置的详细信息:Nginx(CenOS8Minimal)-192.168.1.50Kube...

Nginx负载均衡及支持HTTPS与申请免费SSL证书

背景有两台minio文件服务器已做好集群配置,一台是192.168.56.41:9000;另一台是192.168.56.42:9000。应用程序通过Nginx负载均衡调用这两台minio服务,减轻单点...

HTTPS配置实战(https配置文件)

原因现在网站使用HTTPS是规范操作之一,前些日子买了腾讯云服务,同时申请了域名http://www.asap2me.top/,目前该域名只支持HTTP,想升级为HTTPS。关于HTTPS的链接过程大...

只有IP地址没有域名实现HTTPS访问方法

一般来说,要实现HTTPS,得有个注册好的域名才行。但有时候呢,咱只有服务器的IP地址,没注册域名,这种特殊情况下,也能照样实现HTTPS安全访问,按下面这些步骤来就行:第一步,先确认公网...

超详解:HTTPS及配置Django+HTTPS开发环境

众所周知HTTP协议是以TCP协议为基石诞生的一个用于传输Web内容的一个网络协议,在“网络分层模型”中属于“应用层协议”的一种。在这里我们并不研究该协议标准本身,而是从安全角度去探究使用该协议传输数...

Godaddy购买SSL之后Nginx配置流程以及各种错误的解决

完整流程:参考地址:https://sg.godaddy.com/zh/help/nginx-generate-csrs-certificate-signing-requests-3601生成NGI...

Nginx从安装到高可用,一篇搞定(nginx安装与配置详解)

一、Nginx安装1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本2、上传nginx到linux系统3、安装依赖环境(1)安装gcc环境yuminstallgc...

阿里云免费证书申请,配置安装,使用tomcat,支持http/https访问

参数说明商品类型默认已选择云盾证书服务(无需修改)。云盾证书服务类型SSL证书服务的类型。默认已选择云盾SSL证书(无需修改),表示付费版SSL证书。如果您需要免费领取或付费扩容DV单域名证书【免费试...

你试过两步实现Nginx的规范配置吗?极速生成Nginx配置小工具

NGINX是一款轻量级的Web服务器,最强大的功能之一是能够有效地提供HTML和媒体文件等静态内容。NGINX使用异步事件驱动模型,在负载下提供可预测的性能。是当下最受欢迎的高性能的Web...

从零开始搭建HTTPS服务(搭建https网站)

搭建HTTPS服务的最初目的是为了开发微信小程序,因为wx.request只允许发起HTTPS请求,并且还必须和指定的域名进行网络通信。要从零开始搭建一个HTTPS的服务需要下面4...

群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问

安全第一步,群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问【新手导向】NAS本质还是一个可以随时随地访问的个人数据存储中心,我们在外网访问的时候,特别是在公网IP下,其实会面临着很多安...

让网站快速升级HTTPS协议提高安全性

为什么用HTTPS网络安全越来越受到重视,很多互联网服务网站,都已经升级改造为https协议。https协议下数据包是ssl/tcl加密的,而http包是明文传输。如果请求一旦被拦截,数据就会泄露产生...

用Https方式访问Harbor-1.9版本(https访问流程)

我上周在头条号写过一篇原创文章《Docker-Harbor&Docker-kitematic史上最详细双系统配置手册》,这篇算是它的姊妹篇吧。这篇文章也将用到我在头条写的另一篇原创文章的...

如何启用 HTTPS 并配置免费的 SSL 证书

在Linux服务器上启用HTTPS并配置免费的SSL证书(以Let'sEncrypt为例)可以通过以下步骤完成:---###**一、准备工作**1.**确保域名已解析**...

取消回复欢迎 发表评论: