376

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

I have full sudo privileges, 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 running under that user.

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

10 Answers 10

543

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.

(Starting from Ubuntu 19.10, -H is no longer needed as this is now the default behaviour. See: How does sudo handle $HOME differently since 19.10?)

-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

You can modify the /etc/pam.d/su file to allow su without password. See this answer.

If you modified your auth file to the following, any user that was part of group somegroup could su to otheruser without a password.

auth       sufficient pam_rootok.so
auth       [success=ignore default=1] pam_succeed_if.so user = otheruser
auth       sufficient   pam_succeed_if.so use_uid user ingroup somegroup

Then test from terminal

rubo77@local$ su otheruser -c 'echo "hello from $USER"'
hello from otheruser
13
  • 7
    Can you please add how to do it with just su too?
    – rubo77
    Oct 6, 2014 at 12:33
  • 2
    This asks me for a password :-(
    – IanVaughan
    Jul 20, 2015 at 12:39
  • 1
    @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 help.ubuntu.com/community/Sudoers
    – geirha
    Jul 21, 2015 at 12:54
  • 1
    I'm prompted for a password when running this as root. Any suggestions?
    – Nate
    Nov 28, 2015 at 0:26
  • 1
    @NamGVU keep in mind that order matters. If your sudoers has a rule further down that sets other permissions for your user, or a group you're a member of, that will override your NOPASSWD rule.
    – geirha
    Mar 28, 2019 at 7:12
169

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.

6
  • 8
    I needed to add sudo to the beginning otherwise it asked me for my password.
    – IanVaughan
    Jul 20, 2015 at 12:42
  • 2
    This definitely doesn't work without sudo. with sudo it works, for example: sudo su - www-data -c "touch /tmp/test"successfully created a file as www-data
    – rubo77
    Sep 24, 2016 at 8:41
  • 1
    To use "su" you need the root password, the point of "sudo" is to avoid that. If you have the root password using "su" as above should work fine. Jun 30, 2017 at 7:51
  • "unfortunately I'm not able to install ruby using rvm with this method": when I used this command, the resulting path was bare (e.g., /bin:/usr/bin), so my command (similar to your rvm) wasn't found.
    – PLG
    Nov 11, 2020 at 18:25
  • and what about switching to a specific group?
    – woodz
    Jan 17, 2021 at 12:27
12

The answers above are really useful to me but to answer the actual question...

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

Use:

ps -ef | grep <command-name>

The output should include your script and the actual user executing it. People on BSD-like systems, e.g. MAC can find similar information with:

ps aux | grep <command-name>
2
  • Linux users can use ps aux. Sep 20, 2018 at 20:08
  • 1
    @DeanHowell; True, but MAC users can not use "ps -ef" and the "ps aux" is BSD options, only available as a compatibility feature while the "ps -ef" is standardized Unix/POSIX options. Sep 22, 2018 at 10:26
11

Make sure you switch to root user with command sudo su and then use the command

su user -s <command>

For example: su www-data -s bin/magento cache:clean

2
  • This is the only solution that worked for me! Thanks! Oct 17, 2021 at 22:02
  • For me, it was easier: sudo su - <user> as in sudo su - <user> -s <command>
    – trebor
    Jul 11, 2023 at 18:52
3

I had the same problem. Just type in the command screen -dmS testscreen this will create a detached screen on your non-sudo user account and then you can log it and check if this screen is there by screen -ls.

3
sudo -u <user> <command>
0
2
root@localhost:/# su - johndoh -c "whoami"
johndoh
root@localhost:/# su - johndoh -c "echo \"hi $(whoami)\""
hi root
root@localhost:/# su - johndoh -c "echo \"hi \$(whoami)\""
hi johndoh
root@localhost:/#
1

You can use sudo to do this. First, you need to modify the /etc/sudoers file to add a rule that allows the hudson user to become the other user for the script in question. Let's assume you have a user1 account that needs to run /usr/local/bin/my_script.sh.

Run visudo to modify the sudoers file. Then, use the following sudo command to run the script from hudson.

It's very important that you use visudo to modify the sudoers file in order to check the file for errors before putting it in place. Also, I'd suggest a quick read through man sudoers in order to clearly understand what the above does before adding it to your configuration.

1
  • You should show the actual line(s) that would be inserted into sudoers file. Nov 11, 2020 at 18:51
1

I have created few users without password using

sudo adduser --gecos "" --disabled-password --no-create-home user1
sudo adduser --gecos "" --disabled-password --no-create-home user2

To confirm please check

% sudo cat /etc/shadow | grep user
user1:*:19022:0:99999:7:::
user2:*:19022:0:99999:7:::

I have written a shell script named testing.sh like:

#!/bin/bash

whoami
id

Now I am going to run this script as different users who do not have any password.

$ sudo runuser -u user1 -- bash testing.sh
user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
$ sudo runuser -u user2 -- bash testing.sh
user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)
1
  • Can you elaborate runuser and the double hyphen --?
    – rubo77
    Jan 31, 2022 at 0:00
0

I use "ssh" pointing to the same local machine. This enables me to run the shell script or command passwordless.

ssh target-user@localhost script-to-execute

First, I make sure the host-user saved his public key to the target user (in the same machine). Then I execute the ssh command I showed above.

The result, is a command/script run as the target user, without even entering a password.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .