Solved

Linux, su and sudo

Posted on 2013-12-26
2
764 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

777 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