Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选

首页 / 操作系统 / Linux / Linux 下使用指定的用户来执行命令

有时候,我们需要在Linux开机的时候执行一些特定的程序或都脚本,因为涉及到安全主面的问题,所以又不想用root来执行,那怎样办呢。经过查看 su 的帮助提示,发现:[root@RedHat6 ~]# su --helpUsage: su [OPTION]... [-] [USER [ARG]...]Change the effective user id and group id to that of USER.   -, -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)  -m, --preserve-environment do not reset environment variables  -p                         same as -m  -s, --shell=SHELL            run SHELL if /etc/shells allows it      --help   display this help and exit      --version  output version information and exit A mere - implies -l. If USER not given, assume root.Report su bugs to bug-coreutils@gnu.orgGNU coreutils home page: <http://www.gnu.org/software/coreutils/>General help using GNU software: <http://www.gnu.org/gethelp/>For complete documentation, run: info coreutils "su invocation"
发现其中重要的一行:  "-c  执行单行命令"哈哈,突破点。 马上到命行里试一下:[root@redhat6 ~]# su - admin -c "id"uid=500(admin) gid=500(admin) groups=500(admin)[root@redhat6 ~]#输出的时 admin 在执行命令 "id" 显示的结果 ,而且执行后并没有切换到"admin" 的console 下 .#################### 找到方法了 ################所以,在开机的时候在 /etc/rc.lcal 里面添加一句命令就可以实现:开机时,使用一个普通用户来帮我们做某些操作了 ....[root@redhat6 ~]$ cat /etc/rc.local#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don"t# want to do the full Sys V style init stuff.touch /var/lock/subsys/local#input your commandsu - admin -c "xxxxxx" //最好使用绝对路径#注: /etc/rc.local 是在所在的service 都启动后,才会执行的.本文永久更新链接地址