在 shell 脚本中更改 unix 用户 [英] Changing unix user in a shell script

查看:64
本文介绍了在 shell 脚本中更改 unix 用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 shell 脚本中更改当前用户,以便通过将登录名和密码作为参数传递给用户以管理员身份执行命令.

I want to change the current user in a shell script in order to give the user the ability to execute the commands as an administrator, by passing the login and password as arguments.

我想有能力做这样的事情:

I want to have the ability to do something like :

sh ./script.sh login password

在脚本内部,类似:

sudo $1 $2 etc...

但是我不知道如何清楚地编码.

However I can't figure out how to clearly code this.

感谢您的帮助

推荐答案

Just Use Sudo

sudoers(5) 文件允许非常复杂的权限和命令规范.作为一个简单的例子,您可以让组中的每个人以 root 身份运行单个脚本,而无需密码.这更具脚本性,同时仍允许您限制访问.例如:

Just Use Sudo

The sudoers(5) file allows for very complex permissions and command specifications. As a simplistic example, you can just let everyone in a group run a single script as root without the need for a password. This is much more scriptable, while still allowing you to limit access. For example:

# /etc/sudoers
%somegroup ALL=(ALL:ALL) NOPASSWD:/path/to/script.sh

由于脚本默认以 root 身份运行,因此不需要在脚本中跳过更多的环节.

Since the script will run as root by default, no further hoops need to be jumped through in your script.

这是 Stack Overflow 的一个主题,因为它与脚本相关.但是,如果您有关于配置 sudoers 文件的后续问题,您可能应该在 SuperUser 上提出这些问题.

This is somewhat on-topic for Stack Overflow, since it's scripting-related. However, if you have follow-on questions about configuring the sudoers file, you should probably ask those questions on SuperUser instead.

这篇关于在 shell 脚本中更改 unix 用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆