Discussion:
Switching users in a bash script?
(too old to reply)
Angel Tsankov
2006-08-26 09:18:24 UTC
Permalink
Hello!

A lot of small pieces of a long bash script need to be executed as different users. How can I most easily accomplish this task?
Apostolos P. Tsompanopoulos
2006-08-26 15:06:20 UTC
Permalink
Post by Angel Tsankov
Hello!
A lot of small pieces of a long bash script need to be executed as different
users. How can I most easily accomplish this task?
First read the man pages of sudo(8) and sudoers(5). Check especially
the "-u" and "NOPASSWD" sections (respectively). Then 'fix' the
/etc/sudoers file using "visudo". Finally 'fix' your script!

Apostolos
--
Replace earth.space with gmail.com for a valid e-mail
Unruh
2006-08-26 15:54:39 UTC
Permalink
Post by Angel Tsankov
Hello!
A lot of small pieces of a long bash script need to be executed as different users. How can I most easily accomplish this task?
Have root run the script and use
su username -c commandname
man su
That is the ONLY way.

Note that you cannot use suid on the script.
Michael Heiming
2006-08-26 16:29:37 UTC
Permalink
Post by Unruh
Post by Angel Tsankov
A lot of small pieces of a long bash script need to be
executed as different users. How can I most easily accomplish
this task?
Have root run the script and use
su username -c commandname
man su
That is the ONLY way.
Not really, at least three other possibilities come to my mind.

- Using 'sudo' ; NOPASSWD for the script.

- Using ssh: 'ssh ***@localhost "script"; with keys setup.

- Writing a short C wrapper that calls the script and is set
SUID.

The last should be avoided. The first needs root permissions to
setup sudo. Using ssh doesn't need root permissions at all,
presuming access to "otheruser" account is available.
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo ***@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 412: Radial Telemetry Infiltration
Moe Trin
2006-08-26 19:14:45 UTC
Permalink
On Sat, 26 Aug 2006, in the Usenet newsgroup comp.os.linux.misc, in article
<44f011df$0$75034$***@news.sunsite.dk>, Angel Tsankov wrote:

[Note: comp.os.linux.help and comp.os.linux.questions are bogus groups,
only carried on mis-configured news servers. 'comp.os.linux.help' was
renamed 'comp.os.linux.misc' in 1994.]
Post by Angel Tsankov
A lot of small pieces of a long bash script need to be executed as
different users. How can I most easily accomplish this task?
I assume you have looked at _group_ permissions and decided that running
the script as a group member isn't going to do the job.

[compton ~]$ whatis su
su (1) - run a shell with substitute user and group IDs
[compton ~]$

specifically, the 'su -c' option. To "run as" other users, _without_
supplying a password, this script would have to be run by root. Otherwise
the individual passwords need to be included on the 'su' invocations,
which is a large security problem.

Old guy
Angel Tsankov
2006-08-26 19:29:13 UTC
Permalink
Post by Angel Tsankov
Hello!
A lot of small pieces of a long bash script need to be executed as different users. How can I most easily accomplish this task?
OK, my question was whether I can 'su' in a bash script so that the commands that follow the 'su' in che script will be executed as
the su'ed user.
Floyd L. Davidson
2006-08-26 19:49:06 UTC
Permalink
Post by Angel Tsankov
OK, my question was whether I can 'su' in a bash script so that
the commands that follow the 'su' in che script will be executed
as the su'ed user.
My question is why you don't read the man page and then try it.
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) ***@apaflo.com
Bill Marcum
2006-08-27 04:45:06 UTC
Permalink
["Followup-To:" header set to comp.os.linux.misc.]
On Sat, 26 Aug 2006 22:29:13 +0300, Angel Tsankov
Post by Angel Tsankov
Post by Angel Tsankov
Hello!
A lot of small pieces of a long bash script need to be executed as
different users. How can I most easily accomplish this task?
OK, my question was whether I can 'su' in a bash script so that the
commands that follow the 'su' in che script will be executed as the
su'ed user.
Use a here document, like
su - username <<END
cd /foo/bar
ls -l
END

You will have to type each user's password unless the main script runs
as root, or unless you use sudo (using "nopasswd" in the sudoers file so
user a can execute command b as user c).
--
A person forgives only when they are in the wrong.
Continue reading on narkive:
Loading...