LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2011, 05:37 AM   #1
John Small
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
changing user via su otheruser in a script


When our server starts up it needs to start some processes running in certain user accounts. Those user accounts require passwords. How do I write a script that switches the user and runs the command.

I know I can use su otheruser -c some_command. But how do I send the password into su otheruser ?

I've tried
echo somepassword | su otheruser

but that just complains that it'll only accept passwords from a tty device.

This is on a server running Red Hat

Thanks

John Small
 
Old 07-28-2011, 05:49 AM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,527

Rep: Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898
You don't need the password for
Code:
su -c
Code:
root@cannabis repo]# su repo -c whoami
repo
root@cannabis repo]#
Kind regards
 
Old 07-28-2011, 03:36 PM   #3
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,650

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
Correction

If I may: "You don't need the password for" is only true if you are running the su command AS ROOT.

It may be possible to use a combination of 'sudo' and 'su' to accomplish this for a less-privelaged user, but I would never allow that on any of MY systems without very strict controls.

John: is it possible to run this process as root?
 
Old 07-28-2011, 04:47 PM   #4
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
Let root execute the scripts, but the start/stop commands contained within the scripts would be executed under sudo.

So, let's say you've got process.sh, which you want to run under svcacct. Execute process.sh as root, and its start command would be:

Code:
/usr/bin/sudo -u svcacct /path/to/startCommand.sh
And you'll see that the process is running under the authority of svcacct. No password required.
 
1 members found this post helpful.
Old 07-28-2011, 07:04 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
As per repo, if the originating process is root owned (normal at startup), then 'su - ... is sufficient; no need to start messing with sudo.
In fact its the std way to achieve that effect you want.
 
1 members found this post helpful.
Old 07-29-2011, 11:35 AM   #6
John Small
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Code:
/usr/bin/sudo -u svcacct /path/to/startCommand.sh
The
Code:
 -u someaccount
Is the bit I was missing. I didn't know about the -u option. I can work everything else out.

Thanks everyone
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing user and running commands inside of a script? cory94bailly Linux - Newbie 2 02-22-2011 01:57 PM
where to get linux administrator script, ex: script to let's user change user info. hocheetiong Linux - Server 1 03-17-2008 01:06 AM
Changing user in bash script. r3gan Linux - Security 3 03-15-2006 12:27 PM
changing the user inside a shell script sanjith11 Programming 2 04-22-2004 08:44 AM
changing user in a shell script sanjith11 Programming 6 03-22-2004 02:18 PM


All times are GMT -5. The time now is 09:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration