需要在 Unix shell 脚本中使用 sudo su - [英] Need to use sudo su - in Unix shell script
问题描述
我处于初学者级别,我需要在脚本的一个命令行中为两个不同的用户使用 sudo su -
和 pwd
.(我以 pwd
为例;具体的命令并不重要.)我正在使用命令 sudo su - user -c pwd
.此命令在切换到一个用户时有效,但在切换到另一个时无效.
I am at beginner level and I need to use sudo su -
and pwd
in one command line in script for two different users. (I'm using pwd
as an example; the specific command is not important.)
And I am using command sudo su - user -c pwd
. This command works when switching to one user, but not when switching to another.
例如:-
$ sudo su - ora -c pwd
/oracle/
$ sudo su - adm -c pwd
Sorry, user myuser is not allowed to execute '/usr/bin/su - adm -c pwd' as root on server.
$
如何让它也适用于adm"用户?
How can I make it work for 'adm' user too?
推荐答案
sudo 用于像其他人一样运行命令.默认情况下,它以 root 身份运行命令.您还可以提供 -u 选项以作为另一个用户运行命令.您真的不需要将 sudo 和 su 一起使用,因为它们执行类似的工作.Sudo 以更加可控和可配置的方式完成这项工作.
sudo is used to run a command as somebody else. By default it runs a command as root. You can also supply the -u option to run a command as another user. You really shouldn't need to use sudo and su together as they do similar jobs. Sudo does this job in a much more controlled and configurable fashion.
可以配置Sudo来控制:
Sudo can be configured to control:
- 谁可以使用它.
- 他们可以运行哪些命令.
- 他们可以作为谁来运营.
- 他们在这样做时是否需要提供密码.
一次只能运行一个命令,所以如果你需要一起做几件事,你需要写一个脚本.或者,您可以将它们链接在一个衬里中.或者最后你可以以你需要的用户身份运行一个 shell.例如:
You can only run one command at a time, so if you need to do several things together you will need to write a script. Alternatively you can chain them together in a one liner. Or finally you can run a shell as the user you require. e.g.:
sudo bash
我认为在你的情况下你可能想要使用:
I think in your case you probably want to use:
sudo -u adm anycommand
这篇关于需要在 Unix shell 脚本中使用 sudo su -的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!