LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2011, 01:19 PM   #1
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Rep: Reputation: 0
changing user using su in shell script


Hi,
i have script where i want to switch user and run some command using that user how can i do this?
e.g

script ran from "user1"

...
..

su - user2
...
..
..
exit
#loged out from user 2 execution continued as user1
.....
....

i dont want script to promot user for password for user2 .Is there any i can do this.I want to hardcode password.

i am using Hp unix and dont have expect installed .

Last edited by route; 11-12-2011 at 01:25 PM. Reason: Addintional info
 
Old 11-12-2011, 01:30 PM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Maybe (just maybe) there is a way: allow the user 1 to run this one command. Put that user in a group (as only member) that is allowed to do this. Set permissions as needed, "stir gently and serve chilled" (it's saturday nite here)

Thor
 
Old 11-12-2011, 01:49 PM   #3
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
Does it have to be su? Sales pitch for sudo: sudo is built for one-off command execution and its security requirements can be tuned very nicely. I've got mine set up to allow anybody at all to execute any command as user 'nobody', with no password required; and I can install packages and reboot with no password, and so forth.

Don't let the man page scare you. Look at the examples section to get an idea what's going on, mine is

Code:
# anybody in group "sudo", on any machine, can do anything by giving their password
%sudo ALL=(ALL) ALL

# anybody that can get to a shell prompt can do anything as user 'nobody', no password
ALL ALL = (nobody) NOPASSWD: ALL

# I can install, upgrade, shutdown and reboot without a password. Because a-g install arguments take 
# suffix overrides, I can also remove packages with no password, which isn't exactly apparent here.
jthill ALL = (ALL) NOPASSWD: /usr/bin/apt-get install *
jthill ALL = (ALL) NOPASSWD: /usr/bin/apt-get update
jthill ALL = (ALL) NOPASSWD: /usr/bin/apt-get upgrade
jthill ALL = (ALL) NOPASSWD: /usr/bin/apt-get dist-upgrade
jthill ALL = (ALL) NOPASSWD: /sbin/telinit [60]
 
1 members found this post helpful.
Old 11-12-2011, 02:58 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How about using ssh keys to allow password-less login from user1@localhost to user2@localhost. Then your script only needs to ssh to localhost as user2 and run a command.
 
Old 11-13-2011, 12:20 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I'll reinforce the notion that sudo sounds like right tool for this job.
 
Old 11-14-2011, 12:21 AM   #6
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks all for quick help i am not allow to run sudo .
i will try with ssh keys but problem is that i have many machines which keeps on changing so ssh key logic i need to turn dynamic so that in runtime it will add keys.

just to add more info
my script do following (explanation for need of su).
run from user 1(need to run from this user because only user 1 can detect user for target file system (i.e. user2))

su user 2
copy some content to file system where only user 2 have permission to write
file system permission 755

is ther any other way of copying with other user other than using su?

Last edited by route; 11-14-2011 at 12:23 AM.
 
Old 11-14-2011, 07:39 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
You have many machines but the home of user2 stays the same, i.e. it’s shared across the machines?

Instead of su you could then use a plain scp too, but this involves again ssh-keys. Or as an alternative: hostbased authorization and in user2’s home you can create a file ~/.shosts where you can list the machine from which you connect, and so user1 can login to this account without any password:
Code:
$ cat ~user2/.shosts
mylocalmachine user1
 
  


Reply



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
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Solaris / OpenSolaris 6 03-18-2009 03:37 AM
Shell script for changing filename case Steve W Linux - Software 8 10-17-2008 03:15 AM
changing directories in a shell script stocky021 Programming 9 11-04-2007 11:36 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 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
Open Source Consulting | Domain Registration