简体   繁体   English

Shell:以不同用户身份执行脚本命令

[英]Shell: Execute script commands as different users

I have the following script: 我有以下脚本:

Command1 
Command2
Command3

I would like to execute the command1 and command2 as user user1 , and execute the command3 as user2 . 我想以用户user1身份执行command1command2 ,并以user2身份执行command3

That's why this command uses a file that is on the home directory, and each user has it's own. 这就是为什么此命令使用主目录中的文件,并且每个用户都有自己的文件的原因。 Command 3 requires the User2 file, that's why I need to execute it as user2. 命令3需要User2文件,这就是为什么我需要以user2身份执行它。

How can I do that? 我怎样才能做到这一点?

Well sudo is what you need. 那么sudo是您所需要的。 If you can install it and have the right to set it up, you'll be able to execute your command with the users you configured. 如果可以安装它并有权设置它,则可以与配置的用户一起执行命令。

Your /etc/sudoers.conf file will look like somethig like this: 您的/etc/sudoers.conf文件如下所示:

user1 ALL = NOPASSWD: /full/path/to/Command1
user1 ALL = NOPASSWD: /full/path/to/Command2
user2 ALL = NOPASSWD: /full/path/to/Command3

your script could then be written as: 您的脚本可以这样写:

sudo -u user1 Command1
sudo -u user1 Command2
sudo -u user2 Command3

Here is a short introduction to sudo 这是sudo的简短介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM