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

Linux-PAM认证

nanshan 2025-01-01 21:57 13 浏览 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版本

相关推荐

雷军1994年写的老代码曝光,被称像诗一样优雅

大数据文摘授权转载自程序员的那些事雷军的代码像诗一样优雅↓↓↓有些网友在评论中质疑,说雷军代码不会是“屎”一样优雅吧。说这话的网友,也许是开玩笑的,也许是真没看过雷军写过的代码。在2011年的时候,我...

原创经验分享:低级bug耗费12小时Fix

调试某程序非常简单的程序,简单到认为不可能存在缺陷,但该BUG处理时间超过12小时:程序属于后台进程,监控系统每隔15秒检查外设IO状态,IO异常后发出报警或复位外设,外设都在linux下有/sys/...

SpringBoot实现的简单停车位管理系统附带导入和演示教程视频

这一次为大家带来的是简单的停车位管理系统,基于SpringBoot+Thymeleaf+Mybatis框架,这个系统相对来说比较简单,很容易学习并快速上手,因为逻辑很清晰,没有太复杂的代码逻辑,所以学...

一个开箱即用的代码生成器(代码自动生成工具开源)

今天给大家推荐一个好用的代码生成器,名为renren-generator,该项目附带前端页面,可以很方便的选择我们所需要生成代码的表。首先我们通过git工具克隆下来代码(地址见文末),导入idea。...

【免费开源】JeecgBoot单点登录源码全部开源了

JeecgBoot单点登录源码全部开源了,有需要的朋友可以来薅羊毛了。一、JeecgBoot介绍JeecgBoot是一款企业级的低代码平台!前后端分离架构SpringBoot2.x,SpringCl...

SpringBoot+JWT+Shiro+Mybatis实现Restful快速开发后端脚手架

作者:lywJee来源:cnblogs.com/lywJ/p/11252064.html一、背景前后端分离已经成为互联网项目开发标准,它会为以后的大型分布式架构打下基础。SpringBoot使编码配置...

为什么越来越多的人选择使用idea软件

IDEA软件是什么?IDEA软件是干什么的?为什么越来越多的人选择使用IDEA软件?IDEA软件,全称IntelliJIDEA,它是由JetBrains公司开发开发的一款功能强大的集成开发环境(ID...

开题报告大学生互助系统(附源码)java毕设

本系统(程序+源码)带文档lw万字以上文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容选题背景随着互联网技术的飞速发展,大学生群体对信息共享与互助的需求日益增长。关于大...

SpringBoot项目快速开发框架JeecgBoot——项目简介及系统架构!

项目简介及系统架构JeecgBoot是一款基于SpringBoot的开发平台,它采用前后端分离架构,集成的框架有SpringBoot2.x、SpringCloud、AntDesignof...

新手配电脑13代CPU怎么选择(新手配电脑13代cpu怎么选择好)

Intel第13代酷睿i3、i5、i7、i9系列处理器的核心参数、性能差异及适用群体的详细说明(以桌面端为例):一、13代酷睿全系参数对比(桌面端主流型号)参数i3-13100i5-13600Ki7-...

加速 SpringBoot 应用开发,官方热部署神器真带劲

平时使用SpringBoot开发应用时,修改代码后需要重新启动才能生效。如果你的应用足够大的话,启动可能需要好几分钟。有没有什么办法可以加速启动过程,让我们开发应用代码更高效呢?今天给大家推荐一款Sp...

基于微信小程序的移动端物流系统-计算机毕业设计源码+LW文档

摘要随着Internet的发展,人们的日常生活已经离不开网络。未来人们的生活与工作将变得越来越数字化,网络化和电子化。网上管理,它将是直接管理移动端物流系统app的最新形式。本论文是以构建移动端物流系...

springboot教务管理系统+微信小程序云开发附带源码

今天给大家分享的程序是基于springboot的管理,前端是小程序,系统非常的nice,不管是学习还是毕设都非常的靠谱。本系统主要分为pc端后台管理和微信小程序端,pc端有三个角色:管理员、学生、教师...

SpringBoot全家桶:23篇博客加23个可运行项目让你对它了如指掌

SpringBoot现在已经成为Java开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成。本项目对目前Web开发中常用的各个技术,通过和SpringBoot的集成,并且对各种技术通...

Maven+JSP+Servlet+C3P0+Mysql实现的音乐库管理系统

本系统基于Maven+JSP+Servlet+C3P0+Mysql实现的音乐库管理系统。简单实现了充值、购买歌曲、poi数据导入导出、歌曲上传下载、歌曲播放、用户注册登录注销等功能。难度等级:简单技术...

取消回复欢迎 发表评论: