linux下利用一次性口令实现安全管理

Linux服务器一直就是以稳定、高效、安全而著称。安全是比较重要的一个环节,这关系到商业机密,更关系到企业的存亡。本文介绍了如何使用optw生成一次性口令及只允许执行特定命令,以下为译文:

我想允许我的朋友登录我的服务器下载一些资料,但是只允许他登录10次,登陆后只允许执行scp命令,不许干别的事情,该怎么办呢?

归纳起来,完成以下2件事情:

生成一次性口令

只允许用户执行scp任务

实现目标1:生成一次性口令

安装otpw

sudo apt-get install otpw-bin libpam-otpw

配置common-auth

nano /etc/pam.d/common-auth

查找以下行:

auth [success=1 default=ignore] pam_unix.so nullok_secure

在上述行上加入:

auth sufficient pam_otpw.so
session optional pam_otpw.so

用户登录时,首先尝试使用一次性口令登录,失败后,使用正常登录方法。

配置sshd服务

增加一个otpw配置文件:

nano /etc/pam.d/otpw

内容如下:

auth           sufficient      pam_otpw.so
session        optional        pam_otpw.so

配置sshd配置文件包含otpw配置文件:

nano /etc/pam.d/sshd

查找:

@include common-auth

在上述行上增加一行:

@include otpw

修改sshd配置文件后,确保以下3个参数设置为yes:

UsePrivilegeSeparation yes
ChallengeResponseAuthentication yes
UsePAM yes

重新启动sshd服务

service ssh restart

这是基本的otpw配置. 确保用户home目录下存在文件配置文件 (~/.otpw) 的用户才会启用一次性口令认证. 所有其它用户不受影响。

下列命令产生4个一次性口令:

otpw-gen -h 5 -w 64

下列命令产生10个一次性口令:

otpw-gen -h 6 -w 79

命令输出如下:

Generating random seed ...

If your paper password list is stolen, the thief should not gain
access to your account with this information alone. Therefore, you
need to memorize and enter below a prefix password. You will have to
enter that each time directly before entering the one-time password
(on the same line).

When you log in, a 3-digit password number will be displayed.  It
identifies the one-time password on your list that you have to append
to the prefix password. If another login to your account is in progress
at the same time, several password numbers may be shown and all
corresponding passwords have to be appended after the prefix
password. Best generate a new password list when you have used up half
of the old one.

Overwrite existing password list '~/.otpw' (Y/n)?

Enter new prefix password:
Reenter prefix password:

Creating '~/.otpw'.
Generating new one-time passwords ...

OTPW list generated 2014-02-27 01:31 on kali

000 IT4U V3Bk  002 cfFE g=Gj  004 +2ML Ff92  006 kaag Ar:Y  008 VZY8 iGsp
001 9H7n aPhV  003 fcIJ zf/P  005 Qxqf OhgF  007 zPY/ QJOV  009 :N7K 3zEu

            !!! REMEMBER: Enter the PREFIX PASSWORD first !!!

SSH登录:

login as: test
Using keyboard-interactive authentication.
Password 003:
Linux debian 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1 i686
The programs included with the Debian GNU/Linux system are free software;the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Jul  9 20:03:23 2013 from 192.168.200.10
test@debian:~$

如果你的前缀口令是 “pass” 实际输入的003号密码是:

passfcIJ zf/P

(前缀密码后不需要输入空格)。

创建optw一次性口令的用户组并添加用户:

addgroup optw 
adduser test optw

修改文件权限:

chown root:optw /home/test/.otpw 
chmod 640 /home/test/.otpw

禁止其它用户重置口令:

chmod 750 /usr/bin/otpw-gen

目标2.限制用户只允许执行scp任务:

apt-get install rssh 
apt-get install scponly

2个定制的shell分别完成以下任务:

rssh限制用户的行为 
scponly时仅有scp命令的一个shell.

现在,可以修改用户的shell:

usermod -s /usr/sbin/scponly test 
usermod -s /usr/sbin/rssh test

And you can confiure rssh quite descent:

nano /etc/rssh.conf

Content:

# Leave these all commented out to make the default action for rssh to lock
# users out completely...
allowscp
#allowsftp
#allowcvs
#allowrdist
#allowrsync
#allowsvnserve
# if your chroot_path contains spaces, it must be quoted...
# In the following examples, the chroot_path is "/usr/local/my chroot"
user=test:011:000010:"/opt/scpspace/test chroot"  # scp with chroot

译者注:

1、optw是linux上的一次性口令的开源实现,类似于RSA公司Secure ID功能。

2、rssh是受限的shell,提供许多实用的功能。配置简单。

[译自vpsboard]

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/146897.html<

(0)
运维的头像运维
上一篇2025-03-11 07:31
下一篇 2025-03-11 07:33

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注