How to login as different user inside shell script and execute a set of commands?

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











up vote
1
down vote

favorite












My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.



example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.










share|improve this question





















  • Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use ssh otheruser@localhost with keypair for authentication.
    – Philippos
    Apr 26 '17 at 5:55














up vote
1
down vote

favorite












My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.



example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.










share|improve this question





















  • Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use ssh otheruser@localhost with keypair for authentication.
    – Philippos
    Apr 26 '17 at 5:55












up vote
1
down vote

favorite









up vote
1
down vote

favorite











My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.



example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.










share|improve this question













My requirement is to login as a different user which requires password authentication and execute some commands as that user inside a shell script which I run from my user account.



example: user1 is executing the script. Requirement is to login as user2 using password and execute set of commands using a single shell script.







bash shell-script






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 26 '17 at 4:32









Girish Sunkara

3132613




3132613











  • Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use ssh otheruser@localhost with keypair for authentication.
    – Philippos
    Apr 26 '17 at 5:55
















  • Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use ssh otheruser@localhost with keypair for authentication.
    – Philippos
    Apr 26 '17 at 5:55















Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use ssh otheruser@localhost with keypair for authentication.
– Philippos
Apr 26 '17 at 5:55




Is password authentication required? Solutions I can think of would require the password to be stored more or less unencrypted in the script, which should not be done. If possible, I would prefer to use ssh otheruser@localhost with keypair for authentication.
– Philippos
Apr 26 '17 at 5:55










2 Answers
2






active

oldest

votes

















up vote
4
down vote













You can use several ways:



  1. Using su. Via su you can exec the command on this way:

su user -c "command"



or



su - user -c "command"


the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user



  1. Using sudo:

sudo -u user "command"



With this command you execute it with or without password (your password)



  1. Using ssh:

With ssh you can exec the command with password for user, password for the key (if any)



ssh user@localhost "command"


P.S. There are also other possible methods which are rarely possible like rsh






share|improve this answer



























    up vote
    0
    down vote













    Using the command su [username] allows you to run a command in the context of that user, from the current location. Using the following command su - [username] switches you to that users home directory.
    If you omit the username you are switching to root.






    share|improve this answer




















    • And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
      – Philippos
      Apr 26 '17 at 5:58










    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%2f361327%2fhow-to-login-as-different-user-inside-shell-script-and-execute-a-set-of-commands%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    You can use several ways:



    1. Using su. Via su you can exec the command on this way:

    su user -c "command"



    or



    su - user -c "command"


    the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user



    1. Using sudo:

    sudo -u user "command"



    With this command you execute it with or without password (your password)



    1. Using ssh:

    With ssh you can exec the command with password for user, password for the key (if any)



    ssh user@localhost "command"


    P.S. There are also other possible methods which are rarely possible like rsh






    share|improve this answer
























      up vote
      4
      down vote













      You can use several ways:



      1. Using su. Via su you can exec the command on this way:

      su user -c "command"



      or



      su - user -c "command"


      the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user



      1. Using sudo:

      sudo -u user "command"



      With this command you execute it with or without password (your password)



      1. Using ssh:

      With ssh you can exec the command with password for user, password for the key (if any)



      ssh user@localhost "command"


      P.S. There are also other possible methods which are rarely possible like rsh






      share|improve this answer






















        up vote
        4
        down vote










        up vote
        4
        down vote









        You can use several ways:



        1. Using su. Via su you can exec the command on this way:

        su user -c "command"



        or



        su - user -c "command"


        the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user



        1. Using sudo:

        sudo -u user "command"



        With this command you execute it with or without password (your password)



        1. Using ssh:

        With ssh you can exec the command with password for user, password for the key (if any)



        ssh user@localhost "command"


        P.S. There are also other possible methods which are rarely possible like rsh






        share|improve this answer












        You can use several ways:



        1. Using su. Via su you can exec the command on this way:

        su user -c "command"



        or



        su - user -c "command"


        the difference is when you have dash you will get the environment of target user. With this command you will be asked for the password of target user



        1. Using sudo:

        sudo -u user "command"



        With this command you execute it with or without password (your password)



        1. Using ssh:

        With ssh you can exec the command with password for user, password for the key (if any)



        ssh user@localhost "command"


        P.S. There are also other possible methods which are rarely possible like rsh







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 26 '17 at 6:46









        Romeo Ninov

        4,47311625




        4,47311625






















            up vote
            0
            down vote













            Using the command su [username] allows you to run a command in the context of that user, from the current location. Using the following command su - [username] switches you to that users home directory.
            If you omit the username you are switching to root.






            share|improve this answer




















            • And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
              – Philippos
              Apr 26 '17 at 5:58














            up vote
            0
            down vote













            Using the command su [username] allows you to run a command in the context of that user, from the current location. Using the following command su - [username] switches you to that users home directory.
            If you omit the username you are switching to root.






            share|improve this answer




















            • And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
              – Philippos
              Apr 26 '17 at 5:58












            up vote
            0
            down vote










            up vote
            0
            down vote









            Using the command su [username] allows you to run a command in the context of that user, from the current location. Using the following command su - [username] switches you to that users home directory.
            If you omit the username you are switching to root.






            share|improve this answer












            Using the command su [username] allows you to run a command in the context of that user, from the current location. Using the following command su - [username] switches you to that users home directory.
            If you omit the username you are switching to root.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 26 '17 at 5:42









            Graham Harris

            111




            111











            • And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
              – Philippos
              Apr 26 '17 at 5:58
















            • And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
              – Philippos
              Apr 26 '17 at 5:58















            And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
            – Philippos
            Apr 26 '17 at 5:58




            And the password? Do you want to source it plain from the script? You'd need to sudo the script to avoid be promted for the password.
            – Philippos
            Apr 26 '17 at 5:58

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f361327%2fhow-to-login-as-different-user-inside-shell-script-and-execute-a-set-of-commands%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay