Solved

Linux, su and sudo

Posted on 2013-12-26
2
784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

739 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