自定义Openssh Shell


8

环境是Ubuntu Server 12.04

我想在服务器上创建一个只能ssh到在日志文件上运行tail -f并在程序结束后关闭会话的用户(ctrl + c)。

有没有办法做到这一点?

Answers:


8

要学究一点,它不是ctrl + c,而是SIGHUP(更接近ctrl + d)杀死了该应用程序。

您基本上可以将所需的任何内容放入用户的shell中/etc/passwd。只需/bin/bash用另一个程序替换用户passwd行上的默认值(可能是)即可。该程序可以是/usr/bin/tail_log_file具有以下内容的脚本,例如,由root:root拥有,具有umode 0755:

#!/bin/rbash
tail -f /path/to/logfile

您可以使用rbash以外的其他解释器,但是在这种情况下,建议使用受限制的shell。

要对其进行极其繁琐的操作,您应该将脚本的路径添加到/etc/shells,但是我通常发现它仍然可以正常工作。

还请记住,用户可能会将脚本放在后台,或者使用某些选项(ssh username@host bash)仍然获得外壳。如果要以这种方式限制用户,则只有真正的文件系统权限才是真正的解决方案。


效果很好,非常感谢您的快速准确的回复。
Ablue,2012年

2
您要添加某些内容的唯一原因/etc/shells是允许拥有其他内容作为其shell的用户将shell设置为此。超级用户(root)始终可以将任何人的shell更改为他们想要的任何东西。
乔纳森·卡伦

1
添加@JonathanCallen所说的内容:将脚本添加到其中实际上是安全漏洞,/etc/shells因为这将允许用户更改其shell(因为这/usr/bin/tail_log_file将被视为“不受限制的shell”)!
约阿希姆·绍尔

好的,我将删除。
2012年

1
那太复杂了;强制命令(请参阅@tink的答案)是执行此操作的方法。
恢复莫妮卡-M.Schröder'12

10

如果您愿意使用基于密钥对的身份验证,则可以想到ssh强制命令。

man authorized_keys
/command=

2

您可以将ssh配置为在使用公共密钥身份验证登录时运行您选择的命令。为此,生成一对密钥:

djs@sardinia:~$ ssh-keygen -f restricted-key 
Generating public/private rsa key pair. 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in restricted-key. 
Your public key has been saved in restricted-key.pub. 
The key fingerprint is: b1:8f:26:47:c2:c5:f2:8d:ed:a0:c4:bd:9a:30:9d:08 djs@sardinia 
[...]

restricted-key.pub包含适合放入用户~/.ssh/authorized_keys文件的行:

ssh-rsa AAAA...UDz47Nl djs@sardinia

但是您可以向其中添加命令,并且使用密钥登录时,ssh将运行该命令:

command="tail -f /my/interesting/file" ssh-rsa AAAA...UDz47Nl djs@sardinia

然后,用户可以使用SSH到机器ssh -i restricted-key

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.