首先介紹 runuser 這個指令

# runuser --help

Usage:
 runuser [options] -u <user> [[--] <command>]
 runuser [options] [-] [<user> [<argument>...]]

Run <command> with the effective user ID and group ID of <user>.  If -u is
not given, fall back to su(1)-compatible semantics and execute standard shell.
The options -c, -f, -l, and -s are mutually exclusive with -u.

Options:
 -u, --user <user>               username
 -m, -p, --preserve-environment  do not reset environment variables
 -g, --group <group>             specify the primary group
 -G, --supp-group <group>        specify a supplemental group

 -, -l, --login                  make the shell a login shell
 -c, --command <command>         pass a single command to the shell with -c
 --session-command <command>     pass a single command to the shell with -c
                                   and do not create a new session
 -f, --fast                      pass -f to the shell (for csh or tcsh)
 -s, --shell <shell>             run <shell> if /etc/shells allows it
 -P, --pty                       create a new pseudo-terminal

 -h, --help                      display this help
 -V, --version                   display version


For more details see runuser(1).

範例:
runuser -l mongod -c '/usr/bin/mongod -f /home/example/mongod-27020.conf'

runuser -l mongod -c '/usr/bin/mongod -f /home/example/mongod-27020.conf --shutdown'

透過這個做法,我們可以將開機後要執行的服務寫在同一支 script 中,方便重開機後自動執行,很方便喔

另外還有幾種作法,也是可以在root 的權限下,使用特定的使用者權限來執行程式,提供作參考
1. su - username
2. suid (set uid)
 

undefined

arrow
arrow
    文章標籤
    linux runuser
    全站熱搜

    helloworld 發表在 痞客邦 留言(0) 人氣()