在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. Python日志(logging)模块使用方法简介

    A logger is configured to have a log level. This log level describes the severity of the messages th ...

  2. Codeforces Round #442 (Div. 2) Danil and a Part-time Job

    http://codeforces.com/contest/877/problem/E 真的菜的不行,自己敲一个模板,到处都是问题.哎 #include <bits/stdc++.h> u ...

  3. [原创][FPGA]Quartus中调用Modelsim波形仿真步骤说明

    0. 简介 在使用QuartusII软件的过程中,经常地需要跑仿真,那么说到仿真就不得不说Modelsim这个仿真软件了,我们这里介绍下该软件在QuartusII中的使用方法. 1. 建立Quartu ...

  4. BZOJ——2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit:  ...

  5. codeforces A. Wrong Subtraction

    A. Wrong Subtraction time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. javascript --- 移除DOM节点

    在IE中移除容器类节点,会引起内存泄露,最好是创建一个新的节点,比如div,然后将要删除的节点放入这个div中,再将div的innerHTML清空.其它的直接removeChild就可以了. var ...

  7. Android在其他线程中更新UI

    public class TransferTools { private static final int MSG_START = 1001; private static final int MSG ...

  8. linux 和网络安装 linux

    接触linux 一年了,一直没有总结过什么东西.在开学前的这么几天把之前做的一些东西重新整理一下吧. 或许还会有别的收获呢. linux安装: 去年10月份接触linux以来安装不下数十次,不管是光盘 ...

  9. Android HandlerThread 消息循环机制之源代码解析

    关于 HandlerThread 这个类.可能有些人眼睛一瞟,手指放在键盘上,然后就是一阵狂敲.立即就能敲出一段段华丽的代码: HandlerThread handlerThread = new Ha ...

  10. weblogic的几点配置

    2.在tomcat下写过滤器以后还有的地方需要手工转码<-->weglobic下也不用 eg:SubjectAction.java3.weblogic下anltr.jar有冲突,需要从外界 ...