linux执行指定程序的命令,linux 下使用指定的用户来执行命令
有時間,我們需要在開機的時候執(zhí)行一些特定的程序或都腳本,因為涉及到安全主面的問題,所以又不想用root來執(zhí)行,那怎樣辦呢。
經(jīng)過查看 su 的幫助提示,發(fā)現(xiàn):
[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:?
General?help?using?GNU?software:?
For?complete?documentation,?run:?info?coreutils?'su?invocation'
發(fā)現(xiàn)其中重要的一行: "-c ?執(zhí)行單行命令"
哈哈,突破點。 馬上到命行里試一下:
[root@redhat6?~]#?su?-?admin?-c?"id"
uid=500(admin)?gid=500(admin)?groups=500(admin)
[root@redhat6?~]#
輸出的時?admin?在執(zhí)行命令?"id"?顯示的結(jié)果?,而且執(zhí)行后并沒有切換到"admin"?的console?下?.
#################### 找到方法了 ################
所以,在開機的時候在 /etc/rc.lcal 里面添加一句命令就可以實現(xiàn):開機時,使用一個普通用戶來幫我們做某些操作了 ....
[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 都啟動后,才會執(zhí)行的.
總結(jié)
以上是生活随笔為你收集整理的linux执行指定程序的命令,linux 下使用指定的用户来执行命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 为什么编译内核,Linux内
- 下一篇: utm虚拟机安装linux,UTM: 在