Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to run a script from the main ubuntu shell as a different user that has no password.

I have full sudo priviledges, so I tried this:

sudo su -c "Your command right here" -s /bin/sh otheruser

Then I have to enter my password, but I am not sure if that script is now really called under that user.

How can I affirm that the script is really running under that user now?

share|improve this question

1 Answer 1

up vote 22 down vote accepted

You can do that with su or sudo, no need for both.

sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"' 

The relevant parts of 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
    
Can you please add how to do it with just su too? –  rubo77 Oct 6 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.