如何在Linux中使用PAM添加密码 (linux pam添加密码)

How to add password authentication with PAM on Linux

As the backbone of modern computing systems, security has become one of the most crucial aspects of mntning and managing Linux servers. Ensuring that users authenticate themselves before accessing sensitive data or systems is an essential part of this security process. One way to achieve this is by using Pluggable Authentication Modules (PAM), a powerful authentication framework that allows you to add authentication to various applications and services running on your Linux system. In this article, we will explore how to use PAM to add password authentication to a Linux server.

Understanding PAM

Before we dive into the process of adding password authentication with PAM, it is crucial to understand what PAM is and how it works. PAM is a modular authentication framework that separates the authentication process for different applications or services running on your system. PAM works by loading the appropriate authentication module(s) that correspond with a specific service request. These modules handle the authentication process and report back to the PAM framework on whether the authentication was successful or not. This approach makes it easy to configure different authentication methods depending on the service request or application.

Step 1: Installing the necessary PAM module

The first step in adding password authentication with PAM is to install the necessary PAM module. Depending on your Linux distribution, the PAM module required may differ. However, most Linux distributions include a PAM module for password authentication already installed by default. To install the module manually, you can use your distribution’s package manager. For example, if you are using Ubuntu or Debian, you can install the necessary module by running the following command:

sudo apt-get install libpam-modules

Step 2: Configuring PAM

Now that we have installed the necessary PAM module, we need to configure it to add password authentication. PAM configuration files are typically located in the /etc/pam.d/ directory. Each service or application has its own configuration file, and each file contns the PAM rules that specify the authentication mechani for that application or service.

In this example, we will add password authentication for SSH. The SSH PAM configuration file is located in /etc/pam.d/sshd. To add password authentication, we need to modify this file.

First, create a backup of the file using the following command:

sudo cp /etc/pam.d/sshd /etc/pam.d/sshd.bak

Next, open the SSH PAM configuration file using a text editor such as nano:

sudo nano /etc/pam.d/sshd

In the SSH PAM configuration file, you will see a list of PAM rules. To add password authentication, we need to add a new rule that specifies the PAM module for password authentication. This can be done by adding the following line at the beginning of the file:

auth required pam_unix.so

This PAM rule specifies that the authentication method used will be the traditional Unix password authentication method. Once you have added this line, save and close the file.

Step 3: Testing password authentication

Now that we have configured PAM for password authentication, we need to test it to ensure it is working as intended. To do this, you can try to SSH into your Linux server using a user account that has a password configured.

Once you try to SSH into the server, you should be prompted for a password. Once you enter the correct password, you should be granted access to the Linux server. If the password authentication does not work, review the SSH PAM configuration file and ensure you have followed the steps correctly.

Conclusion

By leveraging PAM, adding password authentication to your Linux server becomes an easy and effective process. PAM not only allows you to add password authentication, but it also enables you to configure various authentication methods to meet the specific needs of your applications and services. When coupled with other security measures, such as firewall configuration and a comprehensive backup strategy, PAM can help you build a robust and secure Linux environment for your organization.

相关问题拓展阅读:

如何在linux系统中设置严密的密码策略

用户帐号管理是系统管理员最重要的工作之一。而密码安全是系统安全中最受关注的一块。在本教程中,我将为大家介绍如何在 Linux 上设置密码策略。 假设你已经在你的 Linux 系统上使用了 PAM (Pluggable Authentication Modules,插入式验证模块)…

为什么linux系统下修改密码总说密码不够好

这是linux密码策略决定的,建议用大写+数字+特殊符号,这种策略是可以更改的,

1、使用配置文件/etc/pam.conf

该文件是由如下的行所组成的:

service-name module-type control-flag module-path arguments

service-name 服务的名字,比如telnet、login、ftp等,服务名字“OTHER”代表所有没有在该文件中明确配置的其它服务。

module-type 模块类型有四种:auth、account、session、password,即对应PAM所支持的四种管理方式。同一个服务可以调用多个 PAM模块进行认证,这些模块构成一个stack。

control-flag 用来告诉PAM库该如何处理与该服务相关的PAM模块的成功或失败情况。它有四种可能的 值:required,requisite,sufficient,optional。

required 表示本模块必须返回成功才能通过认证,但是如果该模块返回失败的话,失败结果也不会立即通知用户,而是要等到同一stack 中的所有模块全部执行完毕再将失败结果返回给应用程序。可以认为是一个必要条件。

requisite 与required类似,该模块必须返回成功才能通过认证,但是一旦该模块返回失败,将不再执行同一stack内的任何模块,而是直 接将控制权返回给应用程序。是一个必要条件。注:这种只有RedHat支持,Solaris不支持。

sufficient 表明本模块返回成功已经足以通过身份认证的要求,不必再执行同一stack内的其它模块,但是如果本模块返回失败的话可以 忽略。可以认为是一个充分条件。

optional表明本模块是可选的,它的成功与否一般不会对身份认证起关键作用,其返回值一般被忽略。

对于control-flag,从Linux-PAM-0.63版本起,支持一种新的语法,具体可参看LinuxPAM文档。

module-path 用来指明本模块对应的程序文件的路径名,一般采用绝对路径,如果没有给出绝对路径,默认该文件在目录/usr/lib/security下面。

arguments 是用来传递给该模块的参数。一般来说每个模块的参数都不相同,可以由该模块的开发者自己定义,但是也有以下几个共同 的参数:

debug 该模块应当用syslog( )将调试信息写入到系统日志文件中。

no_warn 表明该模块不应把警告信息发送给应用程序。

use_first_pass 表明该模块不能提示用户输入密码,而应使用前一个模块从用户那里得到的密码。

try_first_pass 表明该模块首先应当使用前一个模块从用户那里得到的密码,如果该密码验证不通过,再提示用户输入新的密码。

use_mapped_pass 该模块不能提示用户输入密码,而是使用映射过的密码。

expose_account 允许该模块显示用户的帐号名等信息,一般只能在安全的环境下使用,因为泄漏用户名会对安全造成一定程度的威胁。

2、使用配置目录/etc/pam.d/(只适用于RedHat Linux)

该目录下的每个文件的名字对应服务名,例如ftp服务对应文件/etc/pam.d/ftp。如果名为x的服务所对应的配置文件/etc/pam.d/x不存 在,则该服务将使用默认的配置文件/etc/pam.d/other。每个文件由如下格式的文本行所构成:

module-type control-flag module-path arguments

每个字段的含义和/etc/pam.conf中的相同。

由于公司使用的是RedHat的Linux故此我将使用pam.d这个配置目录。密码复杂度通过/etc/pam.d/system-auth这个文件来实现的故此我们先看一下默认有什么内容然后将这个文件备份一个:

在这个文件中我们会用到pam_cracklib.so这个模块。pam_cracklib.so是一个常用并且非常重要的PAM模块。该模块主要的作用是对用户密码的强健性进行检测。即检查和限制用户自定义密码的长度、复杂度和历史等。如不满足上述强度的密码将拒绝使用。

pam_cracklib.so比较重要和难于理解的是它的一些参数和计数方法,其常用参数包括:   

debug:将调试信息写入日志;

type=:当添加/修改密码时,系统给出的缺省提示符是“New UNIX password:”以及“Retype UNIX

password:”,而使用该参数可以自定义输入密码的提示符,比如指定type=your own word;

retry=N:定义登录/修改密码失败时,可以重试的次数;

Difok=N:定义新密码中必须有几个字符要与旧密码不同。但是如果新密码中有1/2以上的字符与旧密码不同时,该新密码将被接受;

minlen=N:定义用户密码的最小长度;

dcredit=N:定义用户密码中必须包含多少个数字;

ucredit=N:定义用户密码中必须包含多少个大写字母;

lcredit=N:定义用户密码中必须包含多少个小些字母;

ocredit=N:定义用户密码中必须包含多少个特殊字符(除数字、字母之外)

linux pam添加密码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux pam添加密码,如何在Linux中使用PAM添加密码,如何在linux系统中设置严密的密码策略,为什么linux系统下修改密码总说密码不够好的信息别忘了在本站进行查找喔。


数据运维技术 » 如何在Linux中使用PAM添加密码 (linux pam添加密码)