Solved

Linux, su and sudo

Posted on 2013-12-26
2
770 Views
Last Modified: 2013-12-27
I am running RHEL 6.5

I want to use a single command that will switch user, and then start a program that is only available within that user's environment.

Normally I would use two commands

su - mqm

runmqsc AAA < test.mq

Open in new window


I want to use a single su or sudo command to both switch to mqm, and then start up a program.

How would I write such a command ?

Thanks
0
Comment
Question by:Los Angeles1
2 Comments
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39740768
Try -
user /bin/su - mqm

Open in new window

You can substitute user with %groupname if you want to include a group.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39740781
If you don't want to get asked for a password you must be root to use "su -" like this:

su - mqm -c "/path/to/runmqsc AAA < /path/to/test.mq"

The "-" flag preserves the environment.


As an authorized sudo user you can do:

sudo -u mqm  "/path/to/runmqsc AAA < /path/to/test.mq"

This requires an entry in sudoers for the user "userA" issuing the command like

userA   ALL=(mqm) NOPASSWD:/path/to/runmsgc

There are three possible methods to preserve the target user's environment:

First method: The option "Defaults env_reset" must not be specified or (with the newer sudo implementations where it's the default) must be negated in sudoers:
Defaults !env_reset

Alternatively, some sudo implementations support the "-i" (=initialize environment)  flag, so you can try:

sudo -u mqm  -i "/path/to/runmqsc AAA < /path/to/test.mq"

A third alternative (if supported): Use the "SETENV" tag and "-E"

userA   ALL=(mqm) NOPASSWD:SETENV:/path/to/runmsgc

sudo -u mqm -E "/path/to/runmqsc AAA < /path/to/test.mq"
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question