Programming Noob
Programming Noob

Reputation: 1845

Can the computer that I'm (p)ssh'ing into be showing different than the original $PATH under certain circumstances?

I'm using this tool called pssh to ssh into a number of hosts together

The syntax to use it is:

pssh [OPTIONS] command [...]

For example:

pssh -h hosts.txt uptime

Here hosts.txt is the file that contains the ip addresses of all of my hosts. The command mentioned above works just fine. Even if I replace uptime by ls it works fine and shows me the correct contents of all the files on the respective machines.

The problem is, if I replace that command with echo $PATH then it shows me my path

If I use this command:

pssh -h ips.txt which java

then for all the hosts it shows me this error:

which: no java in (/usr/local/bin:/bin:/usr/bin)

And if I ssh into just ONE of the hosts manually using the normal ssh command ()

and I execute the which command or see the $PATH variable, then is see this:

-bash-3.2$ echo $PATH
/home/xx/bin64:/home/xx/bin:/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin
-bash-3.2$ which java
/home/xx/bin/java
-bash-3.2$ 

So this shows that at least that one machine has java installed and the right path configured. So can someone tell me why can't I get this command working for all the hosts?

Upvotes: 0

Views: 105

Answers (1)

Celada
Celada

Reputation: 22261

The server(s) you are connecting to probably set a different $PATH depending on whether or the shell that is run remotely is a login or interactive shell or not. When you ssh interactively, an interactive, login shell is invoked on the server. When you ssh non-interactively, a non-interactive, non-login shell is invoked (with a -c option to directly run the specified command without entering interactive mode).

Many shells source different startup files depending on whether they are invoked as login shells and/or interactive shells. For example, bash reads ~/.profile (or variants) only when it is a login shell, and ~/.bashrc only when it is an intractive shell. The server probably sets up a $PATH that includes the directory for Java from one of those startup files.

The solution to this is to set up environment variables like $PATH from /etc/environment instead of from shell startup files.

Upvotes: 2

Related Questions