Linux switch user and execute command immediately












5















I have a scenario where I have to switch to the different user and after that, I need to execute the some Linux command.



my command is something like this



( echo myPassword | sudo -S su hduser ) && bash /usr/local/hadoop/sbin/start-dfs.sh


but with this command, I switch to the user and the next command got triggered on the previous user.



Is there any I can accomplish this using shell script










share|improve this question


















  • 6





    Be aware that this command saves your password in cleartext in your bash history file.

    – spectras
    Sep 26 '17 at 8:47
















5















I have a scenario where I have to switch to the different user and after that, I need to execute the some Linux command.



my command is something like this



( echo myPassword | sudo -S su hduser ) && bash /usr/local/hadoop/sbin/start-dfs.sh


but with this command, I switch to the user and the next command got triggered on the previous user.



Is there any I can accomplish this using shell script










share|improve this question


















  • 6





    Be aware that this command saves your password in cleartext in your bash history file.

    – spectras
    Sep 26 '17 at 8:47














5












5








5


2






I have a scenario where I have to switch to the different user and after that, I need to execute the some Linux command.



my command is something like this



( echo myPassword | sudo -S su hduser ) && bash /usr/local/hadoop/sbin/start-dfs.sh


but with this command, I switch to the user and the next command got triggered on the previous user.



Is there any I can accomplish this using shell script










share|improve this question














I have a scenario where I have to switch to the different user and after that, I need to execute the some Linux command.



my command is something like this



( echo myPassword | sudo -S su hduser ) && bash /usr/local/hadoop/sbin/start-dfs.sh


but with this command, I switch to the user and the next command got triggered on the previous user.



Is there any I can accomplish this using shell script







shell-script ubuntu






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 26 '17 at 5:23









Akash SethiAkash Sethi

135116




135116








  • 6





    Be aware that this command saves your password in cleartext in your bash history file.

    – spectras
    Sep 26 '17 at 8:47














  • 6





    Be aware that this command saves your password in cleartext in your bash history file.

    – spectras
    Sep 26 '17 at 8:47








6




6





Be aware that this command saves your password in cleartext in your bash history file.

– spectras
Sep 26 '17 at 8:47





Be aware that this command saves your password in cleartext in your bash history file.

– spectras
Sep 26 '17 at 8:47










4 Answers
4






active

oldest

votes


















12














Try.



sudo -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh' 


or if you want pass with password.



echo TARGET_USER_PASS | sudo -S -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh'


see man sudo:




-H



The -H (HOME) option requests that the security policy set the HOME
environment variable to the home directory of the target user (root by
default) as specified by the password database. Depending on the
policy, this may be the default behavior.



-u user



The -u (user) option causes sudo to run the specified command as a
user other than root. To specify a uid instead of a user name, use #uid.
When running commands as a uid, many shells require that the '#' be
escaped with a backslash (''). Security policies may restrict uids to
those listed in the password database. The sudoers policy allows uids
that are not in the password database as long as the targetpw option is
not set. Other security policies may not support this.







share|improve this answer





















  • 3





    Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

    – user000001
    Sep 26 '17 at 10:53





















3














With su user -c "sh /path/command.sh" you can run a command as user.



I tested with this command:



 echo myPassword | sudo -S su - foobar -c "/usr/bin/watch -n 1 cat /etc/resolv.conf"


After that the watch -n was running as foobar.



So I think your command should work like that:



echo myPassword | sudo -S su - hduser -c "bash /usr/local/hadoop/sbin/start-dfs.sh"





share|improve this answer
























  • Your answer is also correct Thanks

    – Akash Sethi
    Sep 26 '17 at 8:17





















1














You could try this one:




pkexec - Execute a command as another user



pkexec [--user username] PROGRAM [ARGUMENTS...]







share|improve this answer
























  • tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

    – chloesoe
    Sep 26 '17 at 10:11



















0














Here a shorter version of αғsнιη's answer:



sudo -Hu user command
# example: sudo -Hu root fish





share|improve this answer










New contributor




joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    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%2f394461%2flinux-switch-user-and-execute-command-immediately%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    Try.



    sudo -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh' 


    or if you want pass with password.



    echo TARGET_USER_PASS | sudo -S -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh'


    see man sudo:




    -H



    The -H (HOME) option requests that the security policy set the HOME
    environment variable to the home directory of the target user (root by
    default) as specified by the password database. Depending on the
    policy, this may be the default behavior.



    -u user



    The -u (user) option causes sudo to run the specified command as a
    user other than root. To specify a uid instead of a user name, use #uid.
    When running commands as a uid, many shells require that the '#' be
    escaped with a backslash (''). Security policies may restrict uids to
    those listed in the password database. The sudoers policy allows uids
    that are not in the password database as long as the targetpw option is
    not set. Other security policies may not support this.







    share|improve this answer





















    • 3





      Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

      – user000001
      Sep 26 '17 at 10:53


















    12














    Try.



    sudo -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh' 


    or if you want pass with password.



    echo TARGET_USER_PASS | sudo -S -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh'


    see man sudo:




    -H



    The -H (HOME) option requests that the security policy set the HOME
    environment variable to the home directory of the target user (root by
    default) as specified by the password database. Depending on the
    policy, this may be the default behavior.



    -u user



    The -u (user) option causes sudo to run the specified command as a
    user other than root. To specify a uid instead of a user name, use #uid.
    When running commands as a uid, many shells require that the '#' be
    escaped with a backslash (''). Security policies may restrict uids to
    those listed in the password database. The sudoers policy allows uids
    that are not in the password database as long as the targetpw option is
    not set. Other security policies may not support this.







    share|improve this answer





















    • 3





      Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

      – user000001
      Sep 26 '17 at 10:53
















    12












    12








    12







    Try.



    sudo -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh' 


    or if you want pass with password.



    echo TARGET_USER_PASS | sudo -S -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh'


    see man sudo:




    -H



    The -H (HOME) option requests that the security policy set the HOME
    environment variable to the home directory of the target user (root by
    default) as specified by the password database. Depending on the
    policy, this may be the default behavior.



    -u user



    The -u (user) option causes sudo to run the specified command as a
    user other than root. To specify a uid instead of a user name, use #uid.
    When running commands as a uid, many shells require that the '#' be
    escaped with a backslash (''). Security policies may restrict uids to
    those listed in the password database. The sudoers policy allows uids
    that are not in the password database as long as the targetpw option is
    not set. Other security policies may not support this.







    share|improve this answer















    Try.



    sudo -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh' 


    or if you want pass with password.



    echo TARGET_USER_PASS | sudo -S -H -u TARGET_USER bash -c 'bash /usr/local/hadoop/sbin/start-dfs.sh'


    see man sudo:




    -H



    The -H (HOME) option requests that the security policy set the HOME
    environment variable to the home directory of the target user (root by
    default) as specified by the password database. Depending on the
    policy, this may be the default behavior.



    -u user



    The -u (user) option causes sudo to run the specified command as a
    user other than root. To specify a uid instead of a user name, use #uid.
    When running commands as a uid, many shells require that the '#' be
    escaped with a backslash (''). Security policies may restrict uids to
    those listed in the password database. The sudoers policy allows uids
    that are not in the password database as long as the targetpw option is
    not set. Other security policies may not support this.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 26 '17 at 10:34









    terdon

    129k32253428




    129k32253428










    answered Sep 26 '17 at 6:38









    αғsнιηαғsнιη

    16.6k102865




    16.6k102865








    • 3





      Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

      – user000001
      Sep 26 '17 at 10:53
















    • 3





      Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

      – user000001
      Sep 26 '17 at 10:53










    3




    3





    Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

    – user000001
    Sep 26 '17 at 10:53







    Why ... bash -c 'bash file.sh'? Couldn't you write it as ... bash file.sh?

    – user000001
    Sep 26 '17 at 10:53















    3














    With su user -c "sh /path/command.sh" you can run a command as user.



    I tested with this command:



     echo myPassword | sudo -S su - foobar -c "/usr/bin/watch -n 1 cat /etc/resolv.conf"


    After that the watch -n was running as foobar.



    So I think your command should work like that:



    echo myPassword | sudo -S su - hduser -c "bash /usr/local/hadoop/sbin/start-dfs.sh"





    share|improve this answer
























    • Your answer is also correct Thanks

      – Akash Sethi
      Sep 26 '17 at 8:17


















    3














    With su user -c "sh /path/command.sh" you can run a command as user.



    I tested with this command:



     echo myPassword | sudo -S su - foobar -c "/usr/bin/watch -n 1 cat /etc/resolv.conf"


    After that the watch -n was running as foobar.



    So I think your command should work like that:



    echo myPassword | sudo -S su - hduser -c "bash /usr/local/hadoop/sbin/start-dfs.sh"





    share|improve this answer
























    • Your answer is also correct Thanks

      – Akash Sethi
      Sep 26 '17 at 8:17
















    3












    3








    3







    With su user -c "sh /path/command.sh" you can run a command as user.



    I tested with this command:



     echo myPassword | sudo -S su - foobar -c "/usr/bin/watch -n 1 cat /etc/resolv.conf"


    After that the watch -n was running as foobar.



    So I think your command should work like that:



    echo myPassword | sudo -S su - hduser -c "bash /usr/local/hadoop/sbin/start-dfs.sh"





    share|improve this answer













    With su user -c "sh /path/command.sh" you can run a command as user.



    I tested with this command:



     echo myPassword | sudo -S su - foobar -c "/usr/bin/watch -n 1 cat /etc/resolv.conf"


    After that the watch -n was running as foobar.



    So I think your command should work like that:



    echo myPassword | sudo -S su - hduser -c "bash /usr/local/hadoop/sbin/start-dfs.sh"






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 26 '17 at 6:52









    chloesoechloesoe

    1956




    1956













    • Your answer is also correct Thanks

      – Akash Sethi
      Sep 26 '17 at 8:17





















    • Your answer is also correct Thanks

      – Akash Sethi
      Sep 26 '17 at 8:17



















    Your answer is also correct Thanks

    – Akash Sethi
    Sep 26 '17 at 8:17







    Your answer is also correct Thanks

    – Akash Sethi
    Sep 26 '17 at 8:17













    1














    You could try this one:




    pkexec - Execute a command as another user



    pkexec [--user username] PROGRAM [ARGUMENTS...]







    share|improve this answer
























    • tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

      – chloesoe
      Sep 26 '17 at 10:11
















    1














    You could try this one:




    pkexec - Execute a command as another user



    pkexec [--user username] PROGRAM [ARGUMENTS...]







    share|improve this answer
























    • tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

      – chloesoe
      Sep 26 '17 at 10:11














    1












    1








    1







    You could try this one:




    pkexec - Execute a command as another user



    pkexec [--user username] PROGRAM [ARGUMENTS...]







    share|improve this answer













    You could try this one:




    pkexec - Execute a command as another user



    pkexec [--user username] PROGRAM [ARGUMENTS...]








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 26 '17 at 8:26









    BuyduckBuyduck

    111




    111













    • tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

      – chloesoe
      Sep 26 '17 at 10:11



















    • tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

      – chloesoe
      Sep 26 '17 at 10:11

















    tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

    – chloesoe
    Sep 26 '17 at 10:11





    tested on my site. But if no home dir exists, then there is an error Error changing to home directory /home/foobar: No such file or directory. I created it, but perhaps there is a better solution without home folder

    – chloesoe
    Sep 26 '17 at 10:11











    0














    Here a shorter version of αғsнιη's answer:



    sudo -Hu user command
    # example: sudo -Hu root fish





    share|improve this answer










    New contributor




    joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      Here a shorter version of αғsнιη's answer:



      sudo -Hu user command
      # example: sudo -Hu root fish





      share|improve this answer










      New contributor




      joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        Here a shorter version of αғsнιη's answer:



        sudo -Hu user command
        # example: sudo -Hu root fish





        share|improve this answer










        New contributor




        joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        Here a shorter version of αғsнιη's answer:



        sudo -Hu user command
        # example: sudo -Hu root fish






        share|improve this answer










        New contributor




        joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited 2 hours ago









        Jeff Schaller

        39.3k1054125




        39.3k1054125






        New contributor




        joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 3 hours ago









        joseluisqjoseluisq

        1012




        1012




        New contributor




        joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        joseluisq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            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%2f394461%2flinux-switch-user-and-execute-command-immediately%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

            Entries order in /etc/network/interfaces

            新発田市

            Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)