在Linux中执行有些程序时,这些程序在执行前首先要对启动它的用户进行认证,符合一定的要求之后才允许执行,例如login, su等

在Linux中进行身份或是状态的验证程序是由PAM来进行的,PAM(Pluggable Authentication Modules)可动态加载验证模块,因为可以按需要动态的对验证的内容进行变更,所以可以大大提高验证的灵活性。

Linux各个发行版中,PAM使用的验证模块一般存放在/lib/security/目录下,可以使用ls命令进行查看本计算机支持哪些验证控制方式,一般的PAM模块名字例如pam_unix.so,模块可以随时在这个目录下添加和删除,这不会直接影响程序运行,具体的影响在PAM的配置目录下。

PAM的配置文件位于/etc/pam.d文件下面。ls命令可以看到所有的文件配置

root@zhf-maple:/etc/pam.d# ls -al

总用量 124

drwxr-xr-x   2 root root  4096 11月 27 21:31 .

drwxr-xr-x 133 root root 12288 11月 27 21:31 ..

-rw-r--r--   1 root root   384 11月 20  2014 chfn

-rw-r--r--   1 root root    92 11月 20  2014 chpasswd

-rw-r--r--   1 root root   581 11月 20  2014 chsh

-rw-r--r--   1 root root  1208 11月 26 20:23 common-account

-rw-r--r--   1 root root  1249 11月 26 20:23 common-auth

-rw-r--r--   1 root root  1480 11月 26 20:23 common-password

-rw-r--r--   1 root root  1470 11月 26 20:23 common-session

-rw-r--r--   1 root root  1435 11月 26 20:23 common-session-noninteractive

-rw-r--r--   1 root root   606 8月  21 07:54 cron

-rw-r--r--   1 root root    69 9月  14 00:53 cups

-rw-r--r--   1 root root   884 9月  30  2015 lightdm

-rw-r--r--   1 root root   551 9月  30  2015 lightdm-autologin

-rw-r--r--   1 root root   727 8月  24  2015 lightdm-greeter

-rw-r--r--   1 root root  4905 11月 27 21:31 login

-rw-r--r--   1 root root    92 11月 20  2014 newusers

-rw-r--r--   1 root root   520 4月  22  2017 other

-rw-r--r--   1 root root    92 11月 20  2014 passwd

-rw-r--r--   1 root root   270 5月  24  2017 polkit-1

-rw-r--r--   1 root root   168 1月  28  2016 ppp

-rw-r--r--   1 root root   143 7月  11 09:19 runuser

-rw-r--r--   1 root root   138 7月  11 09:19 runuser-l

-rw-r--r--   1 root root  2257 11月 20  2014 su

-rw-r--r--   1 root root   239 6月  13 03:51 sudo

-rw-r--r--   1 root root   317 10月  3 11:24 systemd-user

-rw-r--r--   1 root root    57 9月  13 09:14 ukui-screensaver

-rw-r--r--   1 root root   319 5月   8  2014 vsftpd

我们看下文件里面的格式是什么:

root@zhf-maple:/etc/pam.d# cat ./login

#

# The PAM configuration file for the Shadow `login' service

#

# Enforce a minimal delay in case of failure (in microseconds).

# (Replaces the `FAIL_DELAY' setting from login.defs)

# Note that other modules may require another minimal delay. (for example,

# to disable any delay, you should add the nodelay option to pam_unix)

auth       optional   pam_faildelay.so  delay=3000000

auth       required   pam_securetty.so

配置文件的格式主要有4列组成:

第一列代表模块类型,类型分为4种:

auth: 用来对用户的身份进行识别.如:提示用户输入密码,或判断用户是否为root

account:对帐号的各项属性进行检查.如:是否允许登录,是否达到最大用户数,或是root用户是否允许在这个终端登录等

session:这个模块用来定义用户登录前的,及用户退出后所要进行的操作.如:登录连接信息,用户数据的打开与关闭,挂载文件系统等.

passwd:使用用户信息来更新.如:修改用户密码.

第二列代表控制标记:也有4种类型:

required: 表示即使某个模块对用户的验证失败,也要等所有的模块都执行完毕后,PAM 才返回错误信息。这样做是为了不让用户知道被哪个模块拒绝。如果对用户验证成功,所有的模块都会返回成功信息

requisite: 与required相似,但是如果这个模块返回失败,则立刻向应用程序返回失败,表示此类型失败.不再进行同类型后面的操作.

sufficient:表示如果一个用户通过这个模块的验证,PAM结构就立刻返回验证成功信息(即使前面有模块fail了,也会把 fail结果忽略掉),把控制权交回应用程序。后面的层叠模块即使使用requisite或者required 控制标志,也不再执行。如果验证失败,sufficient 的作用和 optional 相同

optional:表示即使本行指定的模块验证失败,也允许用户接受应用程序提供的服务,一般返回PAM_IGNORE(忽略).

第三列代表模块路径:要调用模块的位置。一般是在/lib/security文件中。但是每个版本都不太一样,比如我是用的ubuntun17.10。模块位于/lib/x86_64-linux-gnu/security文件夹下面

zhf@zhf-maple:/lib/x86_64-linux-gnu/security$ ls -al

总用量 1064

drwxr-xr-x 2 root root   4096 11月 26 20:05 .

drwxr-xr-x 3 root root  12288 11月 26 20:08 ..

-rw-r--r-- 1 root root  18672 4月  22  2017 pam_access.so

-rw-r--r-- 1 root root  10080 10月 11 16:26 pam_cap.so

-rw-r--r-- 1 root root  10376 4月  22  2017 pam_debug.so

-rw-r--r-- 1 root root   6000 4月  22  2017 pam_deny.so

-rw-r--r-- 1 root root  10336 4月  22  2017 pam_echo.so

-rw-r--r-- 1 root root  14536 4月  22  2017 pam_env.so

-rw-r--r-- 1 root root  14728 4月  22  2017 pam_exec.so

-rw-r--r-- 1 root root  60368 4月  22  2017 pam_extrausers.so

-rw-r--r-- 1 root root  10368 4月  22  2017 pam_faildelay.so

-rw-r--r-- 1 root root  14576 4月  22  2017 pam_filter.so

-rw-r--r-- 1 root root  10304 4月  22  2017 pam_ftp.so

-rw-r--r-- 1 root root  42984 7月  31 21:21 pam_gnome_k

第四列是模块参数:参数可以用多个,之间用空格隔开。例如:password   required   pam_unix.so nullok obscure min=4 max=8 md5

我们再来看下配置文件里面的内容:比如下面两种。其中红色的字体注释已经写明了对应的模块需要做那些配置

# Uncomment and edit /etc/security/access.conf if you need to

# set access limits.

# (Replaces /etc/login.access file)

# account  required       pam_access.so

# Sets up user limits according to /etc/security/limits.conf

# (Replaces the use of /etc/limits in old login)

session    required   pam_limits.so

下面我们介绍集中常用的PAM模块,如下表:

PAM模块

结合管理类型

说明

pam_unix.so

auth

提示用户输入密码,并与/etc/shadow文件相比对.匹配返回0

account

检查用户的账号信息(包括是否过期等).帐号可用时,返回0.

password

修改用户的密码. 将用户输入的密码,作为用户的新密码更新shadow文件

pam_shells.so

auth

account

如果用户想登录系统,那么它的shell必须是在/etc/shells文件中之一的shell

pam_deny.so

account

auth

password

session

该模块可用于拒绝访问

pam_permit.so

auth

account

password

session

模块任何时候都返回成功.

pam_securetty.so

auth

如果用户要以root登录时,则登录的tty必须在/etc/securetty之中.

pam_listfile.so

auth

account

password session

访问应用程的控制开关

pam_cracklib.so

password

这个模块可以插入到一个程序的密码栈中,用于检查密码的强度.

pam_limits.so

session

定义使用系统资源的上限,root用户也会受此限制,可以通过/etc/security/limits.conf或/etc/security/limits.d/*.conf来设定

我们用pam_securetty.so这个模块来做个测试。这个模块的作用是如果用户要以root登录时,则登录的tty必须在/etc/securetty之中。我们知道我们在键盘上可以用ctrl+alt+f1-ctrl+alt+f6来登录各个终端。现在我们来限制root的登录终端

第一步:首先在/etc/pam.d/login文件中配置如下,表明当用户为root的时候需要用pam_securetty.so来进行验证

auth       required   pam_securetty.so

第二步:在/etc/securetty中将tty2给注释掉

# Virtual consoles

tty1

#tty2

第三步:在用root用户登录tty2的时候则会提示login incorrect.

一起来学linux:PAM模块的更多相关文章

  1. [转] linux中pam模块

    一.pam简介 Linux-PAM(linux可插入认证模块)是一套共享库,使本地系统管理员可以随意选择程序的认证方式. 换句话说,不用(重新编写)重新编译一个包含PAM功能的应用程序,就可以改变它使 ...

  2. Linux下PAM模块学习总结

    在Linux中执行有些程序时,这些程序在执行前首先要对启动它的用户进行认证,符合一定的要求之后才允许执行,例如login, su等.在Linux中进行身份或是状态的验证程序是由PAM来进行的,PAM( ...

  3. linux中pam模块

    https://www.cnblogs.com/ilinuxer/p/5087447.html linux中pam模块 一.pam简介 Linux-PAM(linux可插入认证模块)是一套共享库,使本 ...

  4. linux学习-用户的特殊 shell 与 PAM 模块

    特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...

  5. Linux PAM 之cracklib模块

       如何在Linux系统中限制密码长度的同时对密码的复杂程度也进行管理,最近发现有人的密码符合长度规则,但是却很简单很容易被猜出来,查了相关资料后发现了PAM中的pam_cracklib模块就是用来 ...

  6. Linux 账号管理及ACL权限设置,PAM模块简介

    有效群组与初始群组: groups:有效与支持群组的观察 newgrp:有效群组的切换,后面接群组名称 在passwd文件中记录的GID就是默认的GID,就是初始群组 /etc/passwd文件结构 ...

  7. 跟着鸟哥学Linux系列笔记3-第11章BASH学习

    跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 跟着鸟哥学Linux系列笔记2-第10章VIM学习 认识与学习bash 1. ...

  8. Linux PAM&&PAM后门

    Linux PAM&&PAM后门 我是壮丁 · 2014/03/24 11:08 0x00 PAM简介 PAM (Pluggable Authentication Modules )是 ...

  9. 十天学Linux内核之第十天---总结篇(kconfig和Makefile & 讲不出再见)

    原文:十天学Linux内核之第十天---总结篇(kconfig和Makefile & 讲不出再见) 非常开心能够和大家一起分享这些,让我受益匪浅,感激之情也溢于言表,,code monkey的 ...

随机推荐

  1. XSD(XML Schema Definition)学习笔记

    今天学习了XSD相关的知识,为了以后查找的方便,写一些笔记. 一.什么是XSD? 1.XSD全称:XML Schema Definition.XML Schema 的作用是定义 XML 文档的合法构建 ...

  2. js-控制浏览器和移动端的后退按钮 . popstate

    //控制浏览器和移动端的后退按钮 if (window.history && window.history.pushState) { $(window).on('popstate', ...

  3. formValidator阻止提交跳转

    formValidator这个前台校验插件非常好用,其中有几个很有特点的方法可以单独提出使用,效果非常棒这里要说的是其核心方法之一,阻止提交动作,先校验,校验成功再执行提交动作 $("#ph ...

  4. 洛谷—— P1847 轰炸II

    https://www.luogu.org/problemnew/show/1847 题目背景 本题为轰炸数据加强版 题目描述 一个城市遭到了M次轰炸,每次都炸了一个每条边都与边界平行的矩形 在轰炸后 ...

  5. 1005 Spell It Right

    1005 Spell It Right   Given a non-negative integer N, your task is to compute the sum of all the dig ...

  6. ORACLE MOS 翻译

    http://blog.csdn.net/msdnchina/article/details/53174196

  7. javascript --- 原型继承与属性拷贝的综合应用

    对于继承来说,主要目标就是将一些现有的功能据为己有.也就是说,我们在新建一个对象的时候,通常首先继承现有对象,然后再为其添加额外的属性和方法. 对此,我们可以通过一个函数调用来完成. 具体而言就是: ...

  8. C# 使用xenocode混淆加密【转】

    http://www.cnblogs.com/chendaoyin/archive/2013/05/03/3056692.html   之前就了解过混淆加密工具,但这还是第一次使用,选择了xenoco ...

  9. netd ResponseCode

    100 Requestion action was initiated; expect another reply before proceeding with a new command. 200 ...

  10. maven编译maven-surefire-plugin插件报错

    1.编译错误信息: [INFO] ------------------------------------------------------------------------ [ERROR] Fa ...