我需要做的就是以特定用户身份运行特定脚本,该用户nologin/false在/etc/passwd.
我会以 root 身份运行脚本,这应该以另一个用户身份运行。跑步:
~# su -c "/bin/touch /tmp/test" testuser
Run Code Online (Sandbox Code Playgroud)
会工作,但我需要一个有效的测试用户外壳。我知道我可以禁用密码passwd -d testuser并将外壳保留为/bin/bash这种方式会安全一点,但我需要有nologin/false外壳。
基本上我需要的是crontab当我们将作业设置为以特定用户身份运行时会做什么,而不管这个用户是否有nologin/falseshell。
ps 我发现这个线程Executing a command as a nologin user,但我不知道如何concatenate将命令su -s /bin/sh $user发送到我需要运行的脚本。
使用 CentOs,我想以用户“培训”的身份运行脚本作为系统服务。我使用 daemontools 来监控进程,它需要一个以 root 身份运行并且没有 tty 标准的启动程序脚本。
下面我给出了我的五种不同尝试,但都失败了。
:
#!/bin/bash
exec >> /var/log/training_service.log 2>&1
setuidgid training training_command
Run Code Online (Sandbox Code Playgroud)
最后一行不够好,因为对于training_command,我们需要为trqaining 用户设置环境。
:
su - training -c 'training_command'
Run Code Online (Sandbox Code Playgroud)
这看起来像(以不同的用户身份运行 shell 脚本)但提供 ' standard in must be tty' as su 确保 tty 存在以可能接受密码。我知道我可以通过修改 /etc/sudoers(a la https://superuser.com/questions/119376/bash-su-script-giving-an-error-standard-in-must-be-a- tty)但我不情愿并且不确定后果。
:
sudo -u training -i bash -c 'source $HOME/.bashrc; training_command'
Run Code Online (Sandbox Code Playgroud)
同一主题的变体:' sudo: sorry, you must have a tty to run sudo'
:
runuser - training -c 'training_command'
Run Code Online (Sandbox Code Playgroud)
这个给runuser: cannot set …