Need to use sudo su - in Unix shell script

可紊 提交于 2019-12-13 03:56:44

问题


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.

For example:-

$ 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.
$

How can I make it work for 'adm' user too?


回答1:


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 can be configured to control:

  • Who can use it.
  • What commands they can run.
  • Who they can run them as.
  • Whether they need to supply their password when doing so.

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


来源:https://stackoverflow.com/questions/23731301/need-to-use-sudo-su-in-unix-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!