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

linux 下使用指定的用户来执行命令

2014-11-20 11:38 417 查看
有时间,我们需要在开机的时候执行一些特定的程序或都脚本,因为涉及到安全主面的问题,所以又不想用root来执行,那怎样办呢。

经过查看 su 的帮助提示,发现:
[root@redhat6 ~]# su --help
Usage: 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.org
GNU 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 command
su - admin -c "xxxxxx" //最好使用绝对路径

#注: /etc/rc.local 是在所在的service 都启动后,才会执行的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux su高级用法