How to set PATH when running a ssh command?












13















Assuming user has /bin/bash as the shell in /etc/passwd. Then ssh user@host command runs the command using Bash. However, that shell is neither login nor interactive, which means neither ~/.bash_profile nor ~/.bashrc is sourced. In that case how to set the PATH environment variable so that executables can be found and executed? Is it recommended to prefix the actual command with source ~/.bashrc?










share|improve this question




















  • 1





    .bashrc is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use ~/.pam_environment.

    – muru
    Dec 24 '16 at 12:41
















13















Assuming user has /bin/bash as the shell in /etc/passwd. Then ssh user@host command runs the command using Bash. However, that shell is neither login nor interactive, which means neither ~/.bash_profile nor ~/.bashrc is sourced. In that case how to set the PATH environment variable so that executables can be found and executed? Is it recommended to prefix the actual command with source ~/.bashrc?










share|improve this question




















  • 1





    .bashrc is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use ~/.pam_environment.

    – muru
    Dec 24 '16 at 12:41














13












13








13


3






Assuming user has /bin/bash as the shell in /etc/passwd. Then ssh user@host command runs the command using Bash. However, that shell is neither login nor interactive, which means neither ~/.bash_profile nor ~/.bashrc is sourced. In that case how to set the PATH environment variable so that executables can be found and executed? Is it recommended to prefix the actual command with source ~/.bashrc?










share|improve this question
















Assuming user has /bin/bash as the shell in /etc/passwd. Then ssh user@host command runs the command using Bash. However, that shell is neither login nor interactive, which means neither ~/.bash_profile nor ~/.bashrc is sourced. In that case how to set the PATH environment variable so that executables can be found and executed? Is it recommended to prefix the actual command with source ~/.bashrc?







bash ssh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '16 at 11:07









Jakuje

16.5k53256




16.5k53256










asked Dec 24 '16 at 9:20









CykerCyker

1,51521532




1,51521532








  • 1





    .bashrc is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use ~/.pam_environment.

    – muru
    Dec 24 '16 at 12:41














  • 1





    .bashrc is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use ~/.pam_environment.

    – muru
    Dec 24 '16 at 12:41








1




1





.bashrc is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use ~/.pam_environment.

– muru
Dec 24 '16 at 12:41





.bashrc is sourced, but it probably has a test for interactivity at the top. Things you put before that check should apply, and that's what I do to force PATH when the server doesn't allow user environment or use ~/.pam_environment.

– muru
Dec 24 '16 at 12:41










2 Answers
2






active

oldest

votes


















9














You have got few possibilities:




  • Set the PATH in the server ~/.ssh/environment (needs to be enabled by PermitUserEnvironment yes in sshd_config).

  • Use full path to the binary

  • As you mentioned, manually source .bashrc: prefix the command with . ~/.bashrc (or source)


It pretty much depends on the use case, which way you will go.






share|improve this answer



















  • 1





    Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

    – sorontar
    Dec 25 '16 at 3:53



















5














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 iecho "'"$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).






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f332532%2fhow-to-set-path-when-running-a-ssh-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    You have got few possibilities:




    • Set the PATH in the server ~/.ssh/environment (needs to be enabled by PermitUserEnvironment yes in sshd_config).

    • Use full path to the binary

    • As you mentioned, manually source .bashrc: prefix the command with . ~/.bashrc (or source)


    It pretty much depends on the use case, which way you will go.






    share|improve this answer



















    • 1





      Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

      – sorontar
      Dec 25 '16 at 3:53
















    9














    You have got few possibilities:




    • Set the PATH in the server ~/.ssh/environment (needs to be enabled by PermitUserEnvironment yes in sshd_config).

    • Use full path to the binary

    • As you mentioned, manually source .bashrc: prefix the command with . ~/.bashrc (or source)


    It pretty much depends on the use case, which way you will go.






    share|improve this answer



















    • 1





      Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

      – sorontar
      Dec 25 '16 at 3:53














    9












    9








    9







    You have got few possibilities:




    • Set the PATH in the server ~/.ssh/environment (needs to be enabled by PermitUserEnvironment yes in sshd_config).

    • Use full path to the binary

    • As you mentioned, manually source .bashrc: prefix the command with . ~/.bashrc (or source)


    It pretty much depends on the use case, which way you will go.






    share|improve this answer













    You have got few possibilities:




    • Set the PATH in the server ~/.ssh/environment (needs to be enabled by PermitUserEnvironment yes in sshd_config).

    • Use full path to the binary

    • As you mentioned, manually source .bashrc: prefix the command with . ~/.bashrc (or source)


    It pretty much depends on the use case, which way you will go.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 24 '16 at 9:29









    JakujeJakuje

    16.5k53256




    16.5k53256








    • 1





      Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

      – sorontar
      Dec 25 '16 at 3:53














    • 1





      Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

      – sorontar
      Dec 25 '16 at 3:53








    1




    1





    Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

    – sorontar
    Dec 25 '16 at 3:53





    Manually sourcing ~/.bashrc is not necesary, it happens by default. It fact, it seems to me like a bad idea.

    – sorontar
    Dec 25 '16 at 3:53













    5














    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 iecho "'"$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).






    share|improve this answer






























      5














      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 iecho "'"$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).






      share|improve this answer




























        5












        5








        5







        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 iecho "'"$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).






        share|improve this answer















        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 iecho "'"$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).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 10 '17 at 15:15









        Community

        1




        1










        answered Dec 25 '16 at 3:51









        sorontarsorontar

        4,548929




        4,548929






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f332532%2fhow-to-set-path-when-running-a-ssh-command%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            宮崎県

            濃尾地震

            シテ島