简介

在运维管理中,服务器的密码管理十分重要。服务器数量少的时候还好说,可以定时来改密码。一旦数量多了,再来改密码就不现实了。

前提

我们假定运维访问服务器是这样的:

  1. 创建一个普通用户用于登录服务器,给出基础权限,能进行简单的日常操作。但不能修改系统层面的东西。这个权限不能做太多的事。
  2. 切换到root用户时,不能使用root密码来进行认证切换。不然密码泄露,服务器最高权限就算是没了。这里可以使用动态口令的方式来进行认证切换。

原理

原理图如下:

这里通过FreeRadius+GoogleAuthenticator实现linux动态口令认证,其原理是:

  1. 通过开源的GoogleAuthenticator来实现动态口令
  2. 服务器通过FreeRadius进行认证
  3. FreeRadius使用PAM+GoogleAuthenticator来进行认证

这样说可能有点不清楚,我这里再整理下整个流程。流程是这样的:

  1. 用户使用普通用户登录业务服务器A 进行操作
  2. 用户需要提权,切换到root,需要输入动态口令来进行切换
  3. 输入动态口令
  4. 业务服务器A 就向动态口令认证服务器B 的FreeRadius进行认证
  5. 认证服务器B的FreeRadius收到认证请求后,开始认证
  6. FreeRadius会向GoogleAuthenticator来进行动态口令的校验,然后将结果返回给业务服务器。
  7. 业务服务器成功切换到root用户

安装部署

服务端

系统:CentOS 7.2

NTP安装

由于Google Authenticator依赖于时间,所以你的服务器时间必须总是正确的。这里通过ntp服务自动同步网络时间。

yum install ntp -y
systemctl enable ntpd
systemctl start ntpd

Google Authenticator Pam安装配置

安装

git clone https://github.com/google/google-authenticator-libpam.git
cd google-authenticator-libpam/
./bootstrap.sh
./configure
make
make install
ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so

如果出現:

configure: error: Unable to find the PAM library or the PAM header files

請安裝此套件後,重新執行./configure:

# yum install pam-devel

配置pam

/etc/pam.d/sshd

#添加下面一行
auth required pam_google_authenticator.so
#注释下面一行
#auth substack password-auth

配置ssh

/etc/ssh/sshd_config

# Change to no to disable s/key passwords
ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication no

重启sshd

systemctl restart sshd.service

为用户启用google-authenticator

输入命令:google-authenticator

1)屏幕提示Do you want authentication tokens to be time-based (y/n) ,回答y选用基于时间的token

2)屏幕提示二维码,拿出手机打开google authenticator软件(没有请自行下载),点击+后选择“条形码扫描"添加认证条目。 

---

注意:将屏幕显示的secret key, verification code 和 recovery codes 保存在安全的地方,供密码恢复使用。

---

3)Do you want me to update your "/home/sammy/.google_authenticator" file (y/n) y

4)Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n) y

4)By default, tokens are good for 30 seconds. In order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. If you experience problems with poor time synchronization, you can increase the window from its default size of +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens). Do you want to do so? (y/n) n

5)If the computer that you are logging into isn‘t hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting (y/n) y

新建ssh连接(不要关闭当前的防止无法访问)测试配置是否成功。

FreeRadius 安装配置

安装

yum install freeradius freeradius-utils -y

配置

/etc/raddb/radusd.conf

user = radiusd
group = radiusd
#改成
user = root
group = root

/etc/raddb/users

#取消下面2行注释
DEFAULT Group == "disabled", Auth-Type := Reject
Reply-Message = "Your account has been disabled." #添加一行
DEFAULT Auth-Type := PAM

/etc/raddb/sites-enabled/default

authenticate {
...
# Pluggable Authentication Modules
# pam
...
} #取消pam行前面的注释

freeradius启用pam模块

ln -s /etc/raddb/mods-available/pam /etc/raddb/mods-enabled/pam

/etc/raddb/clients.conf

#在最后添加一段认证类型
client macolee_sudo {
ipaddr = 0.0.0.0
netmask = 0
secret = macolee_sudo
require_message_authenticator = no
}

/etc/pam.d/radiusd

#%PAM-1.0
#注释掉下面一行
#auth include password-auth
#添加下面一行。secret是google Authenticator认证的配置文件,user是切换为root的时候需要认证
auth required pam_google_authenticator.so secret=/etc/raddb/gauth user=root
account required pam_nologin.so
account include password-auth
password include password-auth
session include password-auth

启动freeradius

systemctl enable radiusd
systemctl start radiusd

打开防火墙限制,开通FreeRadius的1812端口

/etc/sysconfig/iptables

-A INPUT -s 10.1.2.97 -p udp -m udp --dport 1812 -j ACCEPT

客户端

pam_radius安装配置

安装

#添加epel yum源
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
#安装
yum install pam_radius -y

配置

...

# server[:port]	shared_secret      timeout (s)
# 127.0.0.1 secret 1
# other-server other-secret 3 #添加认证服务器地址 认证类型
10.214.60.37:1812 macolee_sudo 10

修改sudo的pam配置

#%PAM-1.0
#添加下面一行
auth required pam_radius_auth.so
#注释下面一行
#auth include system-auth
account include system-auth
password include system-auth
session optional pam_keyinit.so revoke
session required pam_limits.so

验证

前提

客户端服务器上的用户名,在radius服务端也必须存在。比如客户端要切换为root的普通用户是test1,那么radius服务端也必须有test1这个普通用户

客户端和服务端添加用户

#添加用户
useradd test1
#给用户sudo权限
命令:visudo
#添加
macolee ALL=(ALL) /bin/su - root

测试登录

> 1. 以test1用户登录客户端服务器
> 2. 输入名sudo su - root
> 3. 输入手机app上google验证器上的验证码
> 4. 能成功切换,表示配置成功

FreeRadius+GoogleAuthenticator实现linux动态口令认证的更多相关文章

  1. php集成动态口令认证

    这篇文章主要为大家详细介绍了php集成动态口令认证,动态口令采用一次一密.用过密码作废的方式来提高安全性能,感兴趣的小伙伴们可以参考一下 大多数系统目前均使用的静态密码进行身份认证登录,但由于静态密码 ...

  2. 使用OTP动态口令(每分钟变一次)进行登录认证

    GIT地址:https://github.com/suyin58/otp-demo 在对外网开放的后台管理系统中,使用静态口令进行身份验证可能会存在如下问题: (1) 为了便于记忆,用户多选择有特征作 ...

  3. YS动态口令系统接入流程

    动态口令是保护用户账户的一种常见有效手段,即用户进行敏感操作(比如登录)时,需要用户提供此动态生成的口令做二次身份验证,假设用户的口令被盗,如果没有动态口令,也无法进行登录或进行敏感操作,保护了用户的 ...

  4. 使用google身份验证器实现动态口令验证

    最近有用户反应我们现有的短信+邮件验证,不安全及短信条数限制和邮件收验证码比较慢的问题,希望我们 也能做一个类似银行动态口令的验证方式.经过对可行性的分析及慎重考虑,可以实现一个这样的功能. 怎么实现 ...

  5. poptest老李谈动态口令原理

    poptest老李谈动态口令原理     poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908 ...

  6. centos7 ssh免口令认证登录

    摘要:centos7, xshell, 公钥,  ssh ssh登录方式有口令认证登录和密钥认证登录 接下来本次介绍是ssh密钥登录方式 (1)产生公钥 (2)将公钥放置到centos7的(/root ...

  7. Google authenticator 谷歌身份验证,实现动态口令

    Google authenticator 谷歌身份验证,实现动态口令 google authenticator php 服务端 使用PHP类 require_once '../PHPGangsta/G ...

  8. 静态数据认证(SDA)与动态数据认证(DDA)的区别

    PBOC/EMV里有两个非常重要的概念,SDA(staticdataauthentication)和DDA(dynamicdataauthentication),分别叫做静态数据认证和动态数据认证.这 ...

  9. linux动态库默认搜索路径设置的三种方法

    众所周知, Linux 动态库的默认搜索路径是 /lib 和 /usr/lib .动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库, 并且该动态库还未加载到内存中,则系统会自动到这两 ...

随机推荐

  1. 在Linux服务器上使用Vbox安装虚拟机

    先去官网(www.virtualbox.org)上下载对应Linux系统的Vbox版本. 我这边用的是Oracle Linux 7系统(KDE安装) 使用rpm安装virtualbox 发现报错,按照 ...

  2. org.activiti.engine.activitiexception:version of activiti database(5.22) is more recent than the engine(5.12)

    公司项目启动出现报错,百度查询结果如下:链接地址 org.activiti.engine.ActivitiException: Version of activiti database (5.15.1 ...

  3. log4j 2.+框架

    今天听网友介绍说Log4j2说效率比lOG4J高而且敲级好用.晚上有空就花了几个时间研究了一下.发现嗯,的确好用.我还清楚的记得Log4j1.2的时候我们需要设置log4j需要通过一个properti ...

  4. Java如何按空格读取内容

    String s = input.nextLine(); String[] data = s.split(" ");

  5. openSUSE XFCE桌面 多媒体解码器安装

    openSUSE15 leap 在终端命令行安装编解码器: 添加必要的软件源 zypper addrepo -f http://packman.inode.at/suse/openSUSE_Leap_ ...

  6. php 微信调用扫一扫

    类库代码: <?phpclass JSSDK { private $appId; private $appSecret; public function __construct($appId, ...

  7. idea 从git上checkout项目下来,project没有文件目录结构

    1.去到 查看sdk有没有配置 查看该部分是否是空的,如果没有显示项目,添加导入项目

  8. Python 列表(List)

    Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型 ...

  9. 新增和编辑clob字段

    #region 新的数据新增和修改方法 /// <summary> /// 添加信息 /// </summary> /// <returns></return ...

  10. C#中的抽象类、抽象方法和虚方法

    [抽象类]abstract 修饰符可与类和方法一起使用定义抽象类的目的是提供可由其子类共享的一般形式.子类可以根据自身需要扩展抽象类.抽象类不能实例化.抽象方法没有函数体.抽象方法必须在子类中给出具体 ...