您的位置:首页 > 运维架构 > Shell

在linux shell脚本中root切换到普通用户执行脚本或命令

2016-06-01 10:36 1046 查看
需求:

    安装deb包,设置程序安装后启动,不需要root权限启动程序 | 不能用root权限启动

    其他开机启动选项

方法:root临时降低权限   

secUser=`who am i | awk '{print $1}'`
#echo $secUser

su $secUser -c <指令> > /dev/null 2>&1  &   #这一条命令的执行权限是临时的,只在本条语句起作用

id -u  #  结果为0,还是root用户权限
 


附: su --help

  -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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息