Changing unix user in a shell script
Asked Answered
A

2

0

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

And inside the script, something like :

sudo $1 $2 etc...

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

Thanks for any help

Astraphobia answered 15/7, 2012 at 16:9 Comment(0)
F
2

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

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

Note on Topicality

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.

Farming answered 15/7, 2012 at 16:26 Comment(0)
A
1

I have a limited amount of experience with the sudo command, but I know that you can create your code with a texteditor, and use the chmod command to change permissions and different ways people can access your code.

More specifically, once you've got your .command file, you can go into your emulator and type in chmod. Add a space after that, and then you can customize the users who can actually interact with the code. For example, chmod a+rwx means that everyone (a) can have the privileges of Read, Write, and Execute (rwx). There are many ways chmod can be used, and this webpage, is a good resource for finding out those ways: http://www.zzee.com/solutions/how-to-use-chmod.shtml

After the chmod and the user/permissions parameters, you can add the filepath of your code at the end to tell UNIX which file to chmod. After that, you can manually change the members of the administrative groups that may run the code to allow for better flexibility, as CodeGnome suggested.

Aeri answered 15/12, 2012 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.