linux 以指定用户运行命令,linux里面以指定用户运行命令 - Go语言中文社区

linux 以指定用户运行命令,linux里面以指定用户运行命令


一、chroot方式

[root@localhost ~]# chroot --userspec "nginx:nginx" "/" sh -c "whoami"

nginx

[root@localhost ~]# chroot --userspec "kibana:kibana" "/" sh -c "whoami"

kibana

以kibana用户启动kibana示例:

chroot --userspec "kibana:kibana" "/" sh -c "/usr/share/kibana/bin/kibana -c /etc/kibana/kibana.yml" >> /var/log/kibana/kibana.stdout 2>> /var/log/kibana/kibana.stderr &

# Run the program!

chroot --userspec "$user":"$group" "$chroot" sh -c "

cd "$chdir"

exec "$program" $args

" >> /var/log/kibana/kibana.stdout 2>> /var/log/kibana/kibana.stderr &

716067cbd3797f86081c1374b0735664.png

##二、daemon函数形式

参考https://stackoverflow.com/questions/17956151/how-to-run-a-command-as-a-specific-user-in-an-init-script

On RHEL systems, the /etc/rc.d/init.d/functions script is intended to provide similar to what you want. If you source that at the top of your init script, all of it's functions become available.

The specific function provided to help with this is daemon. If you are intending to use it to start a daemon-like program, a simple usage would be:

daemon --user=username command

If that is too heavy-handed for what you need, there is runuser (see man runuser for full info; some versions may need -u prior to the username):

/sbin/runuser username -s /bin/bash -c "command(s) to run as user username"

elasticsearch启动脚本示例:

#

# Source function library.

#

if [ -f /etc/rc.d/init.d/functions ]; then

. /etc/rc.d/init.d/functions

fi

cd $ES_HOME

echo -n $"Starting $prog: "

# if not running, start it up here, usually something like "daemon $exec"

daemon --user elasticsearch --pidfile $pidfile $exec -p $pidfile -d

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_36015213/article/details/116637311
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 00:24:07
  • 阅读 ( 1254 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢