How to set global variable visible even for sudo commands?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have installed Maven on Ubuntu 16.04 following instructions.



I edited /etc/environmentand added path to maven/bin folder to $PATH variable.



Under my normal user everything works just fine, but when I try to run e.g. sudo mvn test it tells me that mvn is unknown command. Restart didn't help.



Error:



/bin/sh: 1: mvn: not found


I am also little perplexed by /bin/sh piece - I am using bash.



So where should I put path to maven binary to make even sudo commands see it? Thank you.










share|improve this question





















  • Related: What environment do I get with sudo?
    – steeldriver
    Aug 8 at 23:34














up vote
1
down vote

favorite












I have installed Maven on Ubuntu 16.04 following instructions.



I edited /etc/environmentand added path to maven/bin folder to $PATH variable.



Under my normal user everything works just fine, but when I try to run e.g. sudo mvn test it tells me that mvn is unknown command. Restart didn't help.



Error:



/bin/sh: 1: mvn: not found


I am also little perplexed by /bin/sh piece - I am using bash.



So where should I put path to maven binary to make even sudo commands see it? Thank you.










share|improve this question





















  • Related: What environment do I get with sudo?
    – steeldriver
    Aug 8 at 23:34












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have installed Maven on Ubuntu 16.04 following instructions.



I edited /etc/environmentand added path to maven/bin folder to $PATH variable.



Under my normal user everything works just fine, but when I try to run e.g. sudo mvn test it tells me that mvn is unknown command. Restart didn't help.



Error:



/bin/sh: 1: mvn: not found


I am also little perplexed by /bin/sh piece - I am using bash.



So where should I put path to maven binary to make even sudo commands see it? Thank you.










share|improve this question













I have installed Maven on Ubuntu 16.04 following instructions.



I edited /etc/environmentand added path to maven/bin folder to $PATH variable.



Under my normal user everything works just fine, but when I try to run e.g. sudo mvn test it tells me that mvn is unknown command. Restart didn't help.



Error:



/bin/sh: 1: mvn: not found


I am also little perplexed by /bin/sh piece - I am using bash.



So where should I put path to maven binary to make even sudo commands see it? Thank you.







ubuntu environment-variables java maven






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 8 at 23:28









lot

1083




1083











  • Related: What environment do I get with sudo?
    – steeldriver
    Aug 8 at 23:34
















  • Related: What environment do I get with sudo?
    – steeldriver
    Aug 8 at 23:34















Related: What environment do I get with sudo?
– steeldriver
Aug 8 at 23:34




Related: What environment do I get with sudo?
– steeldriver
Aug 8 at 23:34










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










When using sudo your $PATH is getting overridden for security reasons by a line like this in your /etc/sudoers file:



Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin


Example



Even though an entry such as this is present in /etc/environment:



$ cat /etc/environment
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


And it appears to be picked up:



$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


And we have a script in /tmp:



$ ls -l /tmp/blah.bash
-rwxrwxr-x 1 vagrant vagrant 20 Aug 8 20:57 /tmp/blah.bash


And this script will print whoami when run:



$ cat /tmp/blah.bash
#!/bin/bash

whoami


Sudo won't let us do it:



$ sudo blah.bash
sudo: blah.bash: command not found


Changing sudo's secure path



But if I edit my /etc/sudoers file:



$ sudo visudo
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/tmp


Now it works:



$ sudo blah.bash
root


Security



Think long and hard about why you're adding additional directories to sudo's secure path. This is set up like this for a very good reason. Do not take lightly the addition of extra paths for sudo commands!



Adding extra directories exposes you and your users to additional risks which you many not realize until it's too late.






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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f461404%2fhow-to-set-global-variable-visible-even-for-sudo-commands%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    When using sudo your $PATH is getting overridden for security reasons by a line like this in your /etc/sudoers file:



    Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin


    Example



    Even though an entry such as this is present in /etc/environment:



    $ cat /etc/environment
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


    And it appears to be picked up:



    $ sudo env | grep PATH
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


    And we have a script in /tmp:



    $ ls -l /tmp/blah.bash
    -rwxrwxr-x 1 vagrant vagrant 20 Aug 8 20:57 /tmp/blah.bash


    And this script will print whoami when run:



    $ cat /tmp/blah.bash
    #!/bin/bash

    whoami


    Sudo won't let us do it:



    $ sudo blah.bash
    sudo: blah.bash: command not found


    Changing sudo's secure path



    But if I edit my /etc/sudoers file:



    $ sudo visudo
    Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/tmp


    Now it works:



    $ sudo blah.bash
    root


    Security



    Think long and hard about why you're adding additional directories to sudo's secure path. This is set up like this for a very good reason. Do not take lightly the addition of extra paths for sudo commands!



    Adding extra directories exposes you and your users to additional risks which you many not realize until it's too late.






    share|improve this answer
























      up vote
      3
      down vote



      accepted










      When using sudo your $PATH is getting overridden for security reasons by a line like this in your /etc/sudoers file:



      Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin


      Example



      Even though an entry such as this is present in /etc/environment:



      $ cat /etc/environment
      PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


      And it appears to be picked up:



      $ sudo env | grep PATH
      PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


      And we have a script in /tmp:



      $ ls -l /tmp/blah.bash
      -rwxrwxr-x 1 vagrant vagrant 20 Aug 8 20:57 /tmp/blah.bash


      And this script will print whoami when run:



      $ cat /tmp/blah.bash
      #!/bin/bash

      whoami


      Sudo won't let us do it:



      $ sudo blah.bash
      sudo: blah.bash: command not found


      Changing sudo's secure path



      But if I edit my /etc/sudoers file:



      $ sudo visudo
      Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/tmp


      Now it works:



      $ sudo blah.bash
      root


      Security



      Think long and hard about why you're adding additional directories to sudo's secure path. This is set up like this for a very good reason. Do not take lightly the addition of extra paths for sudo commands!



      Adding extra directories exposes you and your users to additional risks which you many not realize until it's too late.






      share|improve this answer






















        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        When using sudo your $PATH is getting overridden for security reasons by a line like this in your /etc/sudoers file:



        Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin


        Example



        Even though an entry such as this is present in /etc/environment:



        $ cat /etc/environment
        PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


        And it appears to be picked up:



        $ sudo env | grep PATH
        PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


        And we have a script in /tmp:



        $ ls -l /tmp/blah.bash
        -rwxrwxr-x 1 vagrant vagrant 20 Aug 8 20:57 /tmp/blah.bash


        And this script will print whoami when run:



        $ cat /tmp/blah.bash
        #!/bin/bash

        whoami


        Sudo won't let us do it:



        $ sudo blah.bash
        sudo: blah.bash: command not found


        Changing sudo's secure path



        But if I edit my /etc/sudoers file:



        $ sudo visudo
        Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/tmp


        Now it works:



        $ sudo blah.bash
        root


        Security



        Think long and hard about why you're adding additional directories to sudo's secure path. This is set up like this for a very good reason. Do not take lightly the addition of extra paths for sudo commands!



        Adding extra directories exposes you and your users to additional risks which you many not realize until it's too late.






        share|improve this answer












        When using sudo your $PATH is getting overridden for security reasons by a line like this in your /etc/sudoers file:



        Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin


        Example



        Even though an entry such as this is present in /etc/environment:



        $ cat /etc/environment
        PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


        And it appears to be picked up:



        $ sudo env | grep PATH
        PATH=/sbin:/bin:/usr/sbin:/usr/bin:/tmp


        And we have a script in /tmp:



        $ ls -l /tmp/blah.bash
        -rwxrwxr-x 1 vagrant vagrant 20 Aug 8 20:57 /tmp/blah.bash


        And this script will print whoami when run:



        $ cat /tmp/blah.bash
        #!/bin/bash

        whoami


        Sudo won't let us do it:



        $ sudo blah.bash
        sudo: blah.bash: command not found


        Changing sudo's secure path



        But if I edit my /etc/sudoers file:



        $ sudo visudo
        Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/tmp


        Now it works:



        $ sudo blah.bash
        root


        Security



        Think long and hard about why you're adding additional directories to sudo's secure path. This is set up like this for a very good reason. Do not take lightly the addition of extra paths for sudo commands!



        Adding extra directories exposes you and your users to additional risks which you many not realize until it's too late.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 9 at 1:03









        slm♦

        238k65491662




        238k65491662



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461404%2fhow-to-set-global-variable-visible-even-for-sudo-commands%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            The Forum (Inglewood, California)

            Palaiologos