su options - running command as another user

garbagecollector

I was wondering how to run a command as another user from a script.

I have the script's owner set as root. I also have the following command being run within the script to run the command as the hudson user:

su -c command hudson

Is this the correct syntax?

Oli

Yes. Here's the --help:

$ su --help
Usage: su [options] [LOGIN]

Options:
  -c, --command COMMAND         pass COMMAND to the invoked shell
  -h, --help                    display this help message and exit
  -, -l, --login                make the shell a login shell
  -m, -p,
  --preserve-environment        do not reset environment variables, and
                                keep the same shell
  -s, --shell SHELL             use SHELL instead of the default in passwd

And some testing (I used sudo as I don't know the password for the nobody account)

$ sudo su -c whoami nobody
[sudo] password for oli: 
nobody

When your command takes arguments you need to quote it. If you don't, strange things will occur. Here I am —as root— trying to create a directory in /home/oli (as oli) without quoting the full command:

# su -c mkdir /home/oli/java oli
No passwd entry for user '/home/oli/java'

It's only read mkdir as the value for the -c flag and it's trying to use /home/oli/java as the username. If we quote it, it just works:

# su -c "mkdir /home/oli/java" oli
# stat /home/oli/java
  File: ‘/home/oli/java’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 811h/2065d  Inode: 5817025     Links: 2
Access: (0775/drwxrwxr-x)  Uid: ( 1000/     oli)   Gid: ( 1000/     oli)
Access: 2016-02-16 10:49:15.467375905 +0000
Modify: 2016-02-16 10:49:15.467375905 +0000
Change: 2016-02-16 10:49:15.467375905 +0000
 Birth: -

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Running command via su as www-data user fails silently

From Dev

Having trouble running a command as another user on Ubuntu

From Dev

When the user of a shell is `root`, does running `su` on another user name ask for password of the other user?

From Dev

Run another command before running the command the user wants to run

From Dev

Why is there a different result when running as user1 and su - user1 -c "command"?

From Dev

Execute command if su to a certain user

From Dev

Export command not found in su - user

From Dev

Running forever within su as a different user

From Dev

Running forever within su as a different user

From Dev

User creation with mutiple command options

From Dev

limit user to only a single command: "su -"

From Dev

su another user through ssh with a local script

From Dev

Backticks with su -c in SSH Command without running command in it

From Dev

running sudo command for a user in centos

From Dev

combining options in find command from a letter to another

From Dev

CMake command-line help for user options

From Dev

executing history command as another user

From Dev

scripting a write command to another user

From Dev

How to have a user who can 'su' to another user but not allowed to 'su' root

From Dev

Enable user to execute one command as another user

From Dev

Pass commands as input to another command (su, ssh, sh, etc)

From Dev

Command behavior is different when executed by an other user (via su -c)

From Dev

How to run command as a different user when there are no sudo or su commands

From Dev

How to pass multiple parameters to su <USER> -c "command"

From Dev

Debian 10 su command to existing user as root without password

From Dev

How do I allow only one user to su to another account?

From Dev

How do I allow only one user to su to another account?

From Dev

Linux - Running commands when another command is run

From Java

Jenkinsfile user input while running sh command

Related Related

  1. 1

    Running command via su as www-data user fails silently

  2. 2

    Having trouble running a command as another user on Ubuntu

  3. 3

    When the user of a shell is `root`, does running `su` on another user name ask for password of the other user?

  4. 4

    Run another command before running the command the user wants to run

  5. 5

    Why is there a different result when running as user1 and su - user1 -c "command"?

  6. 6

    Execute command if su to a certain user

  7. 7

    Export command not found in su - user

  8. 8

    Running forever within su as a different user

  9. 9

    Running forever within su as a different user

  10. 10

    User creation with mutiple command options

  11. 11

    limit user to only a single command: "su -"

  12. 12

    su another user through ssh with a local script

  13. 13

    Backticks with su -c in SSH Command without running command in it

  14. 14

    running sudo command for a user in centos

  15. 15

    combining options in find command from a letter to another

  16. 16

    CMake command-line help for user options

  17. 17

    executing history command as another user

  18. 18

    scripting a write command to another user

  19. 19

    How to have a user who can 'su' to another user but not allowed to 'su' root

  20. 20

    Enable user to execute one command as another user

  21. 21

    Pass commands as input to another command (su, ssh, sh, etc)

  22. 22

    Command behavior is different when executed by an other user (via su -c)

  23. 23

    How to run command as a different user when there are no sudo or su commands

  24. 24

    How to pass multiple parameters to su <USER> -c "command"

  25. 25

    Debian 10 su command to existing user as root without password

  26. 26

    How do I allow only one user to su to another account?

  27. 27

    How do I allow only one user to su to another account?

  28. 28

    Linux - Running commands when another command is run

  29. 29

    Jenkinsfile user input while running sh command

HotTag

Archive