ssh environment variable

时间:2023-01-15 15:40:17

When you run a command as an argument to ssh, the command is run directly by sshd; the shell is not involved.

You can verify this using something like the following:

ssh b@B pstree

It is the shell (bash in this case) that pulls in your .bash_profile, and it only does so if the shell is invoked as a login shell, or if the shell was invoked with the --login option (see the bash man page; look for INVOCATION). The environment you get when running a command directly is set by things like sshd and PAM when you log in.

Thus, if you want your .bash_profile, you need to run it under the shell and you need to run the shell as an interactive or login shell, like this:

ssh b@B "bash -l -c 'env'"

That should get you output you desire. The only thing to watch out for is that quoting gets tricky pretty quickly. Note that on Linux, at least,

ssh b@B bash -l -c 'env'

will not get you a login shell (I'm not sure why).

http://serverfault.com/questions/586382/environment-variables-are-unavailable-when-running-scripts-over-ssh