Re: shell script how to switch users?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 1/4/07, Rajiv Jaisankar <rajiv.jaisankar@xxxxxxxxx> wrote:
Hi All,
i am not sure if this is the right mailing list for asking this question. I
would appreciate any help on this.

I want to switch users using a shell script, i.e.
Say i am logged in as user1. I would like to say su - user2 in my script.
 I would be performing some operations as user "user2" as part of the script
after logging in as this user.
Finally i will be exiting back to user1 shell.

How will i login as user2 through shell script?
How will i  execute scripts as user2 after logging in from shell script?
Even if i am able to login as user2
any commands after "su - user2" are not executed as user2. They are executed
only as user1 when i exit the
user2 shell.

--
Regards,
Rajiv
--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe:
https://www.redhat.com/mailman/listinfo/fedora-list


Use the -c option with su, and call a new script to run as that user.

I tried the following:

test.sh script:
#!/bin/bash
echo "before su"
su root -c ./rootscript.sh
echo "after su"
whoami

rootscript.sh (located in the same directory hence why called by ./rootscript)
#!/bin/bash
echo "now in sub shell"
whoami
echo "exiting sub shell"

It behaved exactly as you'd like.  Within the second script, I was
root running the commands.

Jacques B.

[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux