![](/img/trans.png)
[英]How can I execute a series of commands in a bash subshell as another user using sudo?
[英]How can I execute commands as another user with their .bash_profile instead of the current user's?
我想在启动脚本(rc.local)中以非root用户身份运行命令。 我要执行的命令实际上是非root用户的.bash_profile中的别名。 我尝试使用
su - myuser -c aliased_cmd
但这不起作用。 如果我只是键入
su - myuser
# wait for login...
aliased_cmd
它工作正常,但显然这在实际脚本中是不合适的。 当然,我可以将要使用的别名和函数复制到实际的rc.local文件中,但这似乎有点愚蠢……也许有我不知道的选项或使用sudo的方法这个?
在获取配置文件后,包括以下内容
shopt -s expand_aliases
man su
:
使用-时,必须将其指定为最后一个su选项。
如果不是最后一个选项,它没有指定会发生什么。 另外,在提要中,用户名在所有选项之后。
您是否尝试过su -c aliased_cmd - myuser
或su -l -c aliased_cmd myuser
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.