环境是Ubuntu Server 12.04
我想在服务器上创build一个用户,只能在日志文件中运行尾部-f的shell,并在程序结束后closures会话(ctrl + c)。
有没有办法做到这一点?
要迂腐,它不会是CTRL + C,但SIGHUP (更接近CTRL + D),杀死应用程序。
你可以在/etc/passwd把你想要的任何东西放到用户的shell中。 只需将用户的密码行(可能是/bin/bash )的默认值replace为另一个程序。 该程序可以是脚本,例如/usr/bin/tail_log_file ,这些内容由root:root拥有,其中umode 0755:
#!/bin/rbash tail -f /path/to/logfile
你可以使用除rbash之外的其他解释器,但是在这种情况下最好使用受限制的shell。
对此非常迂腐,你应该将脚本的path添加到/etc/shells ,但是我通常会发现它仍然有效。
还要记住,用户可能会把脚本放在后台,或者使用一些选项( ssh username@host bash )并且仍然获得一个shell。 如果你想以这种方式来限制用户,好的文件系统权限是唯一真正的解决scheme。
如果您很高兴使用基于密钥对的身份validation,那么ssh强制命令就会浮出水面。
man authorized_keys /command=
您可以configurationssh在您使用公钥authenticationlogin时运行您select的命令。 为此,请生成一对密钥:
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
但是你可以给它添加一个命令,当用这个键login时,ssh会运行这个命令:
command="tail -f /my/interesting/file" ssh-rsa AAAA...UDz47Nl djs@sardinia
然后用户可以使用ssh -i restricted-key ssh到机器。