PAM使用
前言:
由于前面一篇已经介绍过pam的作用与机制,此处不再阐述,只是示例常用pam模块及其使用
如需了解pam,请看pam机制
PAM模块帮助:
官方在线文档:http://www.linux-pam.org/Linux-PAM-html
官方离线文档:http://www.linux-pam.org/documentation/
常用PAM模块:
pam_securetty.so模块:
功能:只允许root用户在/etc/securetty列出的安全终端上登陆
注意:CentOS8没有调用此模块,所以默认允许root远程登录telnet
例: 允许telnet用root登录
vim /etc/pam.d/remote
#注释此行
#auth required pam_securetty.so
或者:
vim /etc/securetty
pts/0.pts/1
pam_nologin.so 模块:
功能:如果/var/run/nologin或/etc/nologin文件存在,将导致非root用户不能登陆,当该用户登陆时,会显示/etc/nologin文件内容,并拒绝登陆
例: 禁止普通用户登录,包括su
#此时ssh普通用户登录会失败
vim /etc/nologin
pam_nologin deny login
vim /etc/pam.d/su
account required pam_nologin.so
pam_limits.so模块:
功能: 在用户级别实现对其可使用的资源的限制
如: 可打开的文件数量,可运行的进程数量,可用内存空间
使用ulimiti命令临时修改:
ulimt -h
-n 每个进程最多的打开的文件描述符个数
-u 最大用户进程数
-S 使用 soft(软)资源限制
-H 使用 hard(硬)资源限制
ulimit -n 1048576 #文件描述符最大值就是此
改配置文件永久生效:
/etc/security/limits.conf
/etc/security/limits.d/*.conf
配置文件格式: <domain> <type> <item> <value>
格式说明:
domain:
Username 单个用户
@group 组内所有用户
* 所有用户
% 仅用于限制 maxlogins limit , 可以使用 %group 语法. 只用 % 相当于 * 对所有用户
maxsyslogins limit限制. %group 表示限制此组中的所有用户总的最大登录数
type:
Soft 软限制,普通用户自己可以修改
Hard 硬限制,由root用户设定,且通过kernel强制生效
- 二者同时限定
item:
nofile 所能够同时打开的最大文件数量,默认为1024
nproc 所能够同时运行的进程的最大数量,默认为1024
例:生产案例配置
cat >/etc/security/limits.conf <<-EOF
* - core ulimited
* - nproc 1000000
* - nofile 1000000
* - memlock 32000
* - msgqueue 8192000
EOF
pam_succeed_if 模块:
功能:根据参数中的所有条件都满足才返回成
例: ubuntu允许root登录桌面
vim /etc/pam.d/gdm-passwd
#将下面行注释,即可root登录
#auth requried pam_succeed_if.so user !=root quiet_success
pam_google_authenticator模块:
功能:实现SSH登录的两次身份验证,先验证APP的数字码,再验证root用户的密码,都通过才可以登录。目前只支持口令验证,不支持基于key验证
官方网站:https://github.com/google/google-authenticator-android
使用方法:
- 在手机应用市场搜索:身份验证器或authenticator,并安装APP
- 运行脚本(需要联网EPEL源),本质是修改了/etc/pam.d/sshd文件,将google的PAM模块加入进去实现
脚本:
vim google-authenticator.sh
#!/bin/bash
set -eu
#安装epel
yum install -y epel-release.noarch
yum makecache
#安装google authenticator
yum install -y google-authenticator.x86_64
echo -e "\033[31mDo you want me to update your "/root/.google_authenticator" file? (y/n) y"
echo -e "\033[31m你希望我更新你的“/root/.google_authenticator”文件吗(y/n)?\033[0m"
echo -e "\033[31mDo you want to disallow multiple uses of the same authentication"
echo -e "\033[31mtoken? This restricts you to one login about every 30s, but it increases"
echo -e "\033[31myour chances to notice or even prevent man-in-the-middle attacks (y/n) y"
echo -e "\033[31m你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒, 但是这加大了发现或甚至防止中间人攻击的可能性(y/n)?\033[0m"
echo -e "\033[31mBy default, a new token is generated every 30 seconds by the mobile app."
echo -e "\033[31mIn order to compensate for possible time-skew between the client and the server,"
echo -e "\033[31mwe allow an extra token before and after the current time. This allows for a"
echo -e "\033[31mtime skew of up to 30 seconds between authentication server and client. If you"
echo -e "\033[31mexperience problems with poor time synchronization, you can increase the window"
echo -e "\033[31mfrom its default size of 3 permitted codes (one previous code, the current"
echo -e "\033[31mcode, the next code) to 17 permitted codes (the 8 previous codes, the current"
echo -e "\033[31mcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes"
echo -e "\033[31mbetween client and server."
echo -e "\033[31mDo you want to do so? (y/n) y"
echo -e "\033[31m默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,\033[0m"
echo -e "\033[31m我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题, 可以增加窗口从默认的3个可通过验证码增加到17个可通过验证码,\033[0m"
echo -e "\033[31m这将允许客户机与服务器之间的时差增加到4分钟。你希望这么做吗(y/n)?\033[0m"
echo -e "\033[31mIf the computer that you are logging into isn't hardened against brute-force"
echo -e "\033[31mlogin attempts, you can enable rate-limiting for the authentication module."
echo -e "\033[31mBy default, this limits attackers to no more than 3 login attempts every 30s."
echo -e "\033[31mDo you want to enable rate-limiting? (y/n) y"
echo -e "\033[31m如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块\033[0m"
echo -e "\033[31m启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。 你希望启用尝试次数限制吗(y/n)?\033[0m"
echo -e "\033[32m 在App Store 搜索Google Authenticator 进行App安装 \033[0m"
google-authenticator
#/etc/pam.d/sshd文件,修改或添加下行保存
#auth required pam_google_authenticator.so
sed -i '1a\auth required pam_google_authenticator.so' /etc/pam.d/sshd
#编辑/etc/ssh/sshd_config找到下行
#ChallengeResponseAuthentication no
#更改为
#ChallengeResponseAuthentication yes
sed -i 's/.*ChallengeResponseAuthentication.*/ChallengeResponseAuthentication
yes/' /etc/ssh/sshd_config
#重启SSH服务
service sshd restart
在手机商店,下载谷歌的身份验证器app: Authenticator

浙公网安备 33010602011771号