Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 102 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


       -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.

su can only switch user without providing a password if you are root. See Caleb's answer

share|improve this answer
Can you please add how to do it with just su too? – rubo77 Oct 6 '14 at 12:33
This asks me for a password :-( – IanVaughan Jul 20 '15 at 12:39
@IanVaughan, With default configuration (of sudo), you will get asked for a password unless you run it as root. You can configure sudo to "allow user A to run cmd C as user B without requiring a password". See – geirha Jul 21 '15 at 12:54
I'm prompted for a password when running this as root. Any suggestions? – Nate Nov 28 '15 at 0:26
@Nate, have you made some changes to the sudoers file. The default configuration allows root to run anything as anyone, without requiring a password. – geirha Nov 28 '15 at 9:37

If you want to use su instead of sudo, I believe you can use something like this:

su - <username> -c "<commands>"
  • - will simulate a login of the specified user
  • -c tells it that you want to run a command

ps. Unfortunately I'm not able to install ruby using rvm with this method, but that's probably not related.

share|improve this answer
I needed to add sudo to the beginning otherwise it asked me for my password. – IanVaughan Jul 20 '15 at 12:42

Your Answer


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.