14

I'm trying to execute a script from a different server using ssh

Here's the command I'm using from server 1 to launch a query on server 2:

ssh -t user@xxx.xxx.xxx.xx "cd /var/www/vhosts/xxxxx/subdomains/preprod/; sudo ./replace.sh";

but the problem is that when I do sudo the $home = /root while the script is under: /var/www/vhosts/xxxxx/subdomains/preprod/

How can i tell sudo to preserve the environment?

I tried sudo -P - , sudo -H, without any luck.

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Tarek
  • 3,810
  • 3
  • 36
  • 62
  • 1
    Does the replace.sh use the path from which it is executed? Otherwise you could just do sudo /var/www/vhosts/xxxxx/subdomains/preprod/replace.sh. – Tommy May 05 '11 at 19:47
  • Thank you for taking the time to answer this , it work that way . But I'm more interested in knowing if there is an argument that we can pass to sudo to keep the current Path instead of setting it back to root ... – Tarek May 05 '11 at 20:05

1 Answers1

21

That's what I got from the man page.

sudo -E

-E The -E (preserve environment) option will override the env_reset option in sudoers(5)). It is only available when either the matching command has the SETENV tag or the setenv option is set in sudoers(5).

Tommy
  • 1,277
  • 1
  • 11
  • 22
  • 1
    Also tried that option yesterday and it failed :) – Tarek May 06 '11 at 13:21
  • 1
    apparently modification of the PATH environment variable still can occur despite -E (esp. in Ubuntu, where this occurs by default) as a security concern. See http://stackoverflow.com/questions/257616/sudo-changes-path-why – rogerdpack Nov 13 '13 at 16:49
  • 1
    @Tarek The use of -E maybe restricted by your admin because environemtn variables could potentially cause harm. Though in that case sudo should come back with a clear statement saying: You are not allowed to use -E. If it just silently fails, you will have a different problem. – TorstenS Aug 16 '17 at 10:18