lördag 24 januari 2015

Linux - Run Command As Another User













Avatar




Join the discussion…


































    Avatar














    Where does runuser come from? None of my boxes seem to have it.























    Avatar














    god doc :)
    runuser is good ...
    we can use it in script
    #!/sbin/runuser username























    Avatar














    A caution about using runuser: Any command executed with runuser gets
    placed in the background by default. This means multiple runuser
    commands ran one after the other will each run in parallel. Use the -l
    (minus el) option to force runuser to run the command inside a shel, and
    thus wait for it to finish.























    Avatar














    Just a tip: When issuing the runuser command I usually use the -c
    option, and I always use double quotes instead of single quotes around
    the command to be ran. This allows me to use single quotes within the
    double-quoted command to pass a quoted argument.
    For example, the following will not work because of nested single quotes:
    runuser -l fred -c 'find /foo/foobar -name '*.txt''
    (two single quotes at the end of the line)
    This example uses double quotes and it works fine:
    runuser -l fred -c "find /foo/foobar -name '*.txt'"
    (a double-quote before "find", single quotes around '*.txt' and a double quote at the end of the line)
    If
    you want to know WHY this works, the shell processes command lines with
    double quotes twice. The first pass lets the interpreter know that
    anything within double quotes is an argument and that any single quotes
    within the double qutes are to be taken literally.























    Avatar














    Best doc.























    Avatar














    I personnaly use the su - -c as an administrator, but the sudo is a very handy tool for end-users when correctly configured.























    Avatar














    I often use
    sudo -u USERNAME -i
    or
    sudo -u USERNAME -s

    to switch different user and then run my commands as that user.

















  • Avatar














    $ rpm -qf /sbin/runuser
    coreutils-8.4-19.el6.x86_64










Inga kommentarer: