Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-28-2011, 04: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


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

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

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849

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, 03:47 PM   #4
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, which you want to run under svcacct. Execute as root, and its start command would be:

/usr/bin/sudo -u svcacct /path/to/
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, 06:04 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
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, 10:35 AM   #6
John Small
LQ Newbie
Registered: Jul 2011
Posts: 2

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

Thanks everyone


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 12:57 PM
where to get linux administrator script, ex: script to let's user change user info. hocheetiong Linux - Server 1 03-17-2008 12:06 AM
Changing user in bash script. r3gan Linux - Security 3 03-15-2006 11:27 AM
changing the user inside a shell script sanjith11 Programming 2 04-22-2004 07:44 AM
changing user in a shell script sanjith11 Programming 6 03-22-2004 01:18 PM

All times are GMT -5. The time now is 05:42 PM.

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