How to set PATH when running a ssh command?
You have few possibilities:
- Set the
PATH
on the server in~/.ssh/environment
(needs to be enabled byPermitUserEnvironment yes
insshd_config
). - Use full path to the binary
- As you mentioned, manually source
.bashrc
: prefix the command with. ~/.bashrc
(orsource
)
It pretty much depends on the use case, which way you will go.
You are equating local settings to remote settings.
Locally, a bash instance, the present running shell in which you write:
ssh user@host command
Will execute the command ssh (nothing more) as a client ssh.
To do so the local shell needs not to start a sub-shell or a new shell or login.
The command is executed as a ls
command is: locally.
It is the client ssh command that opens a network connection to a remote system, where, if correctly authenticated, a new shell will be started to execute the command written as an argument to ssh, or, if no argument is given, expect further commands on that connection.
That new Remote shell necessarily will be a login shell as the remote user (to that system) needs to be authenticated to login. Or, if some specific command is given, just run such command with the authenticated user privileges.
You could see which files are sourced by adding a $file sourced
to the start of each file (in the remote system)(root is needed to change /etc/
files):
$ a=(~/.bashrc ~/.profile /etc/bash.bashrc /etc/profile)
$ for f in "${a[@]}"; do sed -i '1 i\echo "'"$f"' was read"\n' "$f"; done
And then just start a ssh console:
$ ssh sorontar@localhost
/etc/profile was read
/etc/bash.bashrc was read
/home/sorontar/.profile was read
/home/sorontar/.bashrc was read
In this case, both bashrc
files were read because each profile
file had commands to include them, not because the login shell directly sourced them.
$ ssh sorontar@localhost :
/etc/bash.bashrc was read
/home/sorontar/.bashrc was read
In this system, where bashrc
is read in both cases.
No need to add a source ~/.bashrc
to the command to execute.
Change PATH
All you need to do is include the correct settings to change the "$PATH", either in /etc/bash.bashrc
for all users that start a shell in this system. Or in ~/.bashrc
for each user that needs it. You could add (or edit) an skeleton of an user .bashrc
to /etc/skel/
to have any new user created have the correct file available.
The above is valid only for bash. If you need the setting to work for all shells, probably setting the environment variable PATH using the ssh file ~/.ssh/environment
for each user that need it. Or use /etc/ssh/sshrc
for a global setting in the system where the ssh server is running (please read the Files section in man sshd
for some additional detail).