Solved

Linux, su and sudo

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now